Sunday, September 19, 2010

Flusing output in Python

When you use "print" statement to output messages to terminal during the execution of a python program, those messages might not get outputted promptly. This is true for time consuming programs that perform a lot of work in between print statements. To promptly flush the text to terminal invoke the python interpreter with "-u" option. For example, if your program is myprogram.py then do the following.

python -u myprogram.py
Alternatively, if you are using sys.stdout.write to print messages to the terminal, then you can do sys.stdout.flush() right after the sys.stdout.write() to do the same. This works even with file I/O.
file.flush() immediately writes the text that is in buffer. This is particularly useful when you are writing log files.

Thursday, September 9, 2010

emacs useful commands

Here are some useful tips from this site.


  1. C-s incremental search. Once you have reached the end of the buffer it will perform an overlap search from the start.
  2. Use fn+left arrow to move to the end of the current line.
  3. Use fn+right arrow to move to the start of the current line.
  4. Use fn+up arrow to move to the start of the buffer.
  5. Use fn+down arrow to move to the end of the buffer. (These are tested on Aquamacs)
  6. M-x grep is extremely useful. It will bring the grep -n -e command to which you must append the filename and the 'string to match' and hit return. The grepped results will be shown in a separate buffer which you can click to move to the appropriate position in the source file.
  7. M-x shell opens a remote/local shell on a separate buffer.
  8. If a file is in version control then emacs shows that in the bottom buffer bar. (e.g. SVN).
  9. C-x v = shows the modifications that you have done since the last commit to the current file.
  10. C-x v v will perform the next appropriate version control action to the current file. If it is a commit then you will get a buffer to write a commit note and then hit C-c C-c to make the commit.
  11. To enter/exit from full screen mode use: command+shift and return.

Thursday, September 2, 2010

bash script to convert encoding of all files in a directory

The following bash script calls iconv on each text file in the input directory and converts them from shift-jis (sjis) to Unicode UTF-8 (utf-8).


#! /bin/bash

inputDir="danu-summaries"
outputDir="danu"

for file in ../$inputDir/*; do
    if [ -f $file ]; then
fname=`echo "$file" | cut -d '/' -f3`
echo $fname
iconv -f sjis -t utf-8 ../$inputDir/$fname > ./$outputDir/$fname
    fi
done

Continuously monitor GPU usage

 For nvidia GPUs do the follwing: nvidia-smi -l 1