Programming Python with bpython – now THAT is neat!

The About page of bpython made the point clear: put what you normally get in a cumbersome IDE into a tiny package and use it in terminal. Here’s the “official” list of features:

  • In-line syntax highlighting.
  • Readline-like autocomplete with suggestions displayed as you type.
  • Expected parameter list.
  • Rewind.
  • Pastebin code/write to file.
  • Flush curses screen to stdout.

Python has always been a neat language encouraging high productivity. This tool, simple as it is, has pushed the ease of use of Python to a new level. You should definitely try it out and kudos to Bob Farrell!

July 28, 2010 at 2:54 pm Leave a comment

Doskey: creating aliases under Windows command prompt

I’m tired of typing “ls” on an Windows command prompt and getting bounced back with an error. Aliases are truly a necessity. To simulate the behavior of “alias” on Windows, do the following:

1. Create a shortcut for cmd.exe, right-click on the shortcut and choose “properties” and proceed as follows.

2. Change the “start in” directory to your personal choice, such as “%HOMEDRIVE%%HOMEPATH%”

3. Create a batch file, such as “set_alias.bat”, in the “start in” directory. The file should contain the doskey command that loads the alias definitions. A sample file would contain this line:

doskey /MACROFILE=aliases.txt

4. Edit all your aliases.txt file, or other files that contain the Macros. For example, aliases.txt could look like this:

ls=dir $*
cp=copy $*
python2=C:\Python26\python26.exe $*
python3=C:\Python31\python31.exe $*

$* allows you to take an arbitrary number of arguments and pass that to the program (up to 9?)

5. At the end of “Target” line in the shortcut properties, add your batch script with “/K” option to run upon launching. The Target line should look similar to this:

C:\Windows\System32\cmd.exe /K set_alias.bat

Done! Now cruise along :)

June 30, 2010 at 6:02 pm 1 comment

CUDA new features (well, kinda new)

Support of broadcast in shared memory (working on half a warp each time, one to 1-16 threads).

A memo of Compute capability updates
1.0
…(PRE 8800 GTS)
1.1 atomic functions on 32-bit words in global memory
…(PRE GT200)
1.2 Atomic functions operating in shared memory, atomic functions operating on 64-bit words in global memory
1.2 Warp vote functions
1.2 16K local memory
1.2 32 active warps per SM, 1024 active threads per SM
1.3 Double precision floating-point numbers
…(Current Frontier)

April 21, 2009 at 7:12 pm Leave a comment

LaTeX Tips from Practice (3)

How to display source code with keyword highlighting and proper comment color

Originally posted at here

Include the package needed with the following line:

\usepackage{listings}

A template of quoting source code with syntax highlighting is as follows:

\usepackage{color}
\usepackage{listings}
\definecolor{Brown}{cmyk}{0,0.81,1,0.60}
\definecolor{OliveGreen}{cmyk}{0.64,0,0.95,0.40}
\definecolor{CadetBlue}{cmyk}{0.62,0.57,0.23,0}
\begin{document}

\lstset{language=VBScript,frame=ltrb,framesep=5pt,basicstyle=\normalsize,
keywordstyle=\ttfamily\color{OliveGreen},
morekeywords={one,two,three},
commentstyle=\color{Brown},
stringstyle=\ttfamily,
showstringspaces=ture}

\begin{lstlisting}

\end{lstlisting}

Adjust accordingly to your preferred language, style and color.

April 15, 2009 at 11:39 pm Leave a comment

Customize syntax highlighting for CUDA in Kate/KDevelop

CUDA is a minimal extension of C/C++ to support programming on newer NVIDIA GPUs. It introduces dozens of new keywords that are frequently used in CUDA programs. Adding syntax highlighting in KDevelop (Kate) only requires several simple steps:

1. Get keywords from “usertype.dat” provided by any Windows version of CUDA SDK, this is a text file. It should be found under $(NVIDIA_CUDA_SDK)\doc\syntax_highlighting.

2. Under your Linux account, cd ~/.kde/share/apps/katepart/syntax/, there should be two xml files regarding C/C++, cpp.xml and c.xml. Make a copy of both and name them properly, e.g. cpp_cuda.xml & c_cuda.xml.

3. Perform a replace operation on each keyword in “usertype.dat” so they match the keyword format in target xml files, and then copy them into the keyword lists of target files.

4. Modify xml header, which includes language description, file extensions and other information. You probably want to add “*.cu” to the file extension list. Remember to distinguish them from the original C/C++ language tag. Save the xml files after you are done.

5. Open Kate/KDevelop, under Tools->Highlighting->Sources you should see two new entries below C++ and C, respectively.

Enjoy.

June 6, 2008 at 11:41 am 3 comments

Starting a subprocess from Python

subprocess is a convenient way to fork a new process on Windows/Unix/etc. It obsoletes previous methods such as commands (which does not support Windows) and os.system()/os.popen*/os.spawn*

Here is an example:
import subprocess
cmd_lst = ['exec_file', 'arg1', 'arg2', ...]
subprocess.call(cmd_lst) #return the value returned by the callee
subprocess.check_call(cmd_lst) #return if the callee exits with 0, otherwise raise an error

May 5, 2008 at 2:35 pm Leave a comment

Surface by Microsoft

Happened to run into the link http://www.microsoft.com/surface, this is a product that’s been announced for over a year. Very impressive.

This somewhat proves, again, that computer science is neither about computer nor science, it is math and art.

April 2, 2008 at 1:48 am Leave a comment

Shot their own feet

Live Mail filtered Microsoft Survey invitation as junk. I lost a teeny-tiny chance to win in a sweepstakes. Oh well… Do we have to take training courses in “how to compose emails in a junk-unlikely style” someday in the future?

March 4, 2008 at 9:28 pm Leave a comment

Cygwin group/passwd setting

In case anything goes wrong with user accounts, carry on the following steps:

mkpasswd -l -c > /etc/passwd

mkgroup -l -c > /etc/group

mkpasswd -d -u <username> /etc/passwd (optional)

mkgroup -d | grep <username> /etc/group (optional)

February 17, 2008 at 8:20 pm 1 comment

How to- Tunnel Traffic through Firewall and Remote Desktop

My department has rigid access rules, which prevail in today’s internet jungle. Sadly enough, connecting to one’s working environment is not as easy as it used to be. It took more than a breath of time and understanding to configure all the access methods I need. Though all stated here are trivial, I feel keeping a memo quite necessary, especially when all the department guidelines themselves are within the firewall.

Scenario
a) IP address space protected by a filewall
b) Access server sitting just on border of the firewall, only tunneling allowed
c) Well-known department servers inside the filewall
d) Office computer, with domain/username account and domain-affiliated NFS (OS: Windows XP SP2)
e) Home computer, OS: Windows Vista (yes, I know Linux will make remote access easier, but what about drivers of all the cool new hardware I paid big money for?)

Software on client side: OpenSSH (on Cygwin) or PuTTY or SSH Secure Shell, WinSCP

Goal 1: Set up tunnels through a border server
Edit the configuration file for OpenSSH (or do similar stuff to other clients), open the file ~/.ssh/config and put the following lines in:
host name_it_yourself
hostname hostname_domainname
user username
ForwardAgent yes
LocalForward custom_port1 hostname1:port1 #list as many as u wish
LocalForward custom_port2 hostname2:port2
IdentityFile ~/.ssh/key_for_host/id_dsa #where to store private key if pubkey is used

kick-start everything else with ssh name_it_yourself and leave it running in the background. From now on, all remote access appear as reaching for some local customized port. All the ugly things (almost all) are taken care of by the tunneling mechanism.

Goal 2: Connect to well know server inside firewall
Specify a portforwarding rule in 1, and then create a new profile (i.e. adding a new paragraph in OpenSSH config file) looking like this:
host name_it_yourself
hostname localhost # since we have already tunneled it
user username
ForwardX11 yes
port custom_port1
IdentityFile ~/.ssh/id_file

Login by running ssh name_it_yourself

Goal 3: SSH to office computer
A SSH server is required on the remote end, OpenSSH on Cygwin is one of the easiest way to do this. Set OpenSSH to service mode so that sshd.exe is lauched at booting.
On the client side, add another profile for office computer similar to 2. Remember to set up a tunnel in 1. With better control of the server end, public key authentication can be used to save password typing. Use ssh-keygen under Cygwin for a pair of keys:
ssh-keygen -f ~/.ssh/key_file -t rsa #specify where to put keys and which authentication protocol to use
Keep the private key safe and sound, probably protecting it by a password.
Distribute the pub key file on office computer by concatenate it to the SSH authorized key file (change path of the pub key accordingly):
cat ~/key_file.pub >> ~/.ssh/authorized_keys 

Note: If you are using PuTTY for tunneling, remember that pub key pairs generated by PuTTY is not 100% compatible with OpenSSH. Minor modification required, or simply stick to ssh-keygen and import it in PuTTY.

Goal 4: FTP-like file access
WinSCP is a good candidate, supporting SCP and SFTP. Create a new profile in WinSCP, if pubkey is used, private key file should be loaded. WinSCP uses PuTTY style private key, and OpenSSH keys are alien to it. Therefore, a private key generated in a UNIX environment should be imported using PuTTYgen.exe and saved in PuTTY style (.ppk) before using.

Goal 5: Remote desktop to office computer
Add a tunnel to the remote RDP port by adding a line to profile in 1:
LocalForward local_port office_computer_domain:RDP_port
Make sure remote desktop is allowed on remote OS and the user is granted access. Run remote desktop on local machine and access localhost:local_port.
Note On Windows XP SP2, access to 127.0.0.1: is not allowed (loopback), use 127.0.0.2 instead. On Windows XP/2003, RDP listens on port 3389, while Windows Vista listens on 3390. Therefore, one cannot map these port as the local_port.

September 25, 2007 at 9:18 pm 1 comment

Older Posts


Categories

  • Personal

  • Feeds


    Follow

    Get every new post delivered to your Inbox.