Monday, November 25, 2013

Very Quick File Server

Just cd into the desired directory, and run python -m SimpleHTTPServer. You get the message "Serving HTTP on port 8000 ...". Now go the browser and type localhost:8000, and start browsing files/folders in that directory. Share the link http://<your IP addr>:8000 to let your friends browse as well.

[Tested on Ubuntu 10.04 (64-bit) with Python 2.6.5.]

Wednesday, October 16, 2013

Piping stderr

To pipe stderr (instead of stdout) use `|&' instead of `|'. This works from Bash 4 onwards. It is shorthand for `2>&1 |', which redirects bytes sent to stderr (2) to stdout (1), and then pipes it (|).

[Courtesy stackoverflow.]

Saturday, October 5, 2013

Simple Java development setup

A simple setup for small Java development projects could be:
  • Create the source directory ($PROJ_HOME) which will contain all .java files.
  • Create the directory $PROJ_HOME/proj_name which will contain all the .class files.
  • Add $PROJ_HOME to the CLASSPATH.
  • During development, keep a terminal open with working directory $PROJ_HOME, and run "javac *.java -d ." to compile all source files and store the class files in proj_name.
  • Run a class from anywhere using java proj_name.ClassName [args].
In addition:
  • Store metadata files etc. required by the code in $PROJ_HOME/meta. If they need to be passed by command line arguments, you can pass $PROJ_HOME/meta/datafile.txt as an argument wherever you are running the class.
  • Store sample test data files to quickly test the code in $PROJ_HOME/test.

Saturday, September 7, 2013

Enabling Indic transliteration on web pages

Enable typing in Indian languages in web pages using Pramukh IME. This javascript library is very easy to integrate into your website, and supports 20 Indian languages.

Also, it is open source; so you can add new languages, or change the keyboard layout for existing ones.

Tuesday, August 6, 2013

Rendering math in blogs

To render LaTeX-style mathematical formulae in blogs, use MathJax, as shown here. Once set up, you can do
  • \( <math text> \) for inline math
  • \[ <math text> \] for equations

Friday, August 2, 2013


To generate a HTML version of a TeX document:
  • First ensure that all metadata files are up-to-date (e.g. if you have references in the tex file, run latex; bibtex; latex; latex to ensure that the .bbl file is ok).
  • Run latex2html -split +1 -show_section_numbers -no_footnode myfile.tex. This will create a directory myfile containing myfile.html (among other things).
    • -split +1 causes all subsections within a section to be in a single page (-split 0 will cause the whole document to be in a single page)
    • -no_footnode causes footnotes to be included in the same page where they are referenced.
    • See man latex2html for more options.

Wednesday, July 3, 2013

Random reordering of lines in a file

To generate a random reordering or shuffling of lines in a file, use
shuf inp.txt > out.txt

Monday, June 24, 2013

Python tips

To compile a python script without executing it, do
python -m py_compile
[Courtesy: stackoverflow]

Wednesday, June 5, 2013

Installing Python regex on Ubuntu 12.04

To install the alternative regular expression module regex (which has richer Unicode support than re) for Python:
  • Install python-dev from the Software Center.
  • Download the archive, extract, and run sudo python install.

Monday, June 3, 2013

Downloading a blog on blogspot

This lists the latest N posts from the blog. Instead of N, type the number of posts. If your blog has less than 1000 posts, you can save this page:

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 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  
   python param &     # schedule background job here  

Thursday, January 17, 2013

File compression in python

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

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

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

(Courtesy Doug Hellmann.)