Thursday, May 2, 2013

Unicode tips for Python

  • To use non-Latin characters in regular expressions, use u'...' instead of r'...', even if you have to escape every backslash; e.g. the regex u'(?u)[०-९]\\s' matches a Devanagari digit followed by whitespace.
  • Remove zero-width joiners/non-joiners from Unicode text to get a normalized representation; otherwise words that are rendered the same in a browser/editor will be stored differently, and will not be equal on comparison; e.g. use the regex u'[\\u200D\\u200C]' and replace all matches with u'' (the empty string).

Friday, January 18, 2013

Shell script for scheduling jobs

Suppose you want to schedule many runs of myscript.py for different parameters, but never more than 8 at a time (e.g. because your machine has only 8 processor cores), then you can do:
 for param in `cat params.txt`; do  
   # wait if more than 7 jobs are running together  
   njobs=`ps aux | grep myscript | wc -l`  
   if [ $njobs -gt 8 ]; then          # 7 jobs + 1 grep  
     while [ $njobs -gt 8 ]; do  
       sleep 10  
     done  
   fi  
   python myscript.py param &     # schedule background job here  
 done  

Thursday, January 17, 2013

File compression in python

Normally, you can do
 f = open(fname, 'r')  
 data = f.read()  
 f.close()  
to read a file, and
 f = open(fname, 'w')  
 f.write(text)  
 f.close()  
to write a file.

To use gzip compression, do
 import gzip  
 f = gzip.open(fname, 'rb')  
 data = f.read()  
 f.close()  
to read a gzip-compressed file, and
 f = gzip.open(fname, 'wb')  
 f.write(data)  
 f.close()  
to write a gzip-compressed file.

To compress pickled objects, just use the file handle returned by gzip.open(); everything else remains the same.

(Courtesy Doug Hellmann.)

Sunday, November 18, 2012

Grep tips

To get all lines not beginning with a tab, do
grep -vP "^\t" abc.txt
-P turns on Perl-style regex (otherwise tab cannot be specified as \t). "^\t" matches lines beginning with a tab. -v inverts the selection.

For most cases though, using -E (extended regex) instead of -P works, and is better supported.

Friday, November 16, 2012

Using `less'

View text files using less, including very large files. It also opens gzip-ed files. Use less -S to truncate long lines (instead of wrapping around).

Wednesday, August 22, 2012

Information about your Linux system

Here are some commands that give information about your linux system.

To get  the number of CPU's/cores in the system, use
less /proc/cpuinfo

To see if your processor supports 64-bit OS, you can Google the processor name (got using /proc/cpuinfo). To see if your OS is 64-bit, use
uname -a
If it says x_86_64 somewhere in the output, then it is a 64-bit OS, else it is probably a 32-bit OS.

To see where (in which directories) a package is installed in Ubuntu, use
dpkg -L <packagename>

Friday, August 3, 2012

Displaying Unicode text in the terminal / console

If gnome-terminal has problems displaying Unicode (UTF-8) characters properly, try using konsole (from KDE). It worked for me.