Monday, July 25, 2011

Python Module Not Found

Ways to debug when a python code can't find a module:

Open a terminal and start python. Enter:

from imp import *
find_module(module)


See if the path name corresponds with where your module was supposed to be installed or if the path name directs to a __init__.py file


For Example, we were trying to install pyfacebook and our code won't load the module.


find_module(module) was resulting in the following output:
(<open file 'facebook.py', mode 'U' at 0xb752d4f0>, 'facebook.py', ('.py', 'U', 1))


However, the module was installed at (found using a find command):
/usr/lib/python2.7/site-packages/pyfacebook-1.0a2-py2.7.egg


Clearly, when the code was calling from facebook import *, the code was unable to locate __init__.py file and was crashing. To solve the problem, move the files to /usr/lib/python2.7/site-packages/ and see if a further run of find_module points to the required directory.

Friday, July 8, 2011

python sequence float vs int

Python has a nifty little way to generate a sequence of numbers:

sequence = range(0, 10, 1)

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

However, this doesn't work when the step value is float. For that purpose, use numpy's arange function:

import numpy as np
sequence = np.arange(0,1,0.1)

array([ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5,  0.6,  0.7,  0.8,  0.9])

For more interesting ways, including using generators, check: http://stackoverflow.com/questions/477486/python-decimal-range-step-value

Thursday, July 7, 2011

Python Non-ASCII character '\xc3'

During a python script execution, if you get the following error:

SyntaxError: Non-ASCII character '\xc3' in file text2term_topia.py on line 21, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

Add the following statement right at the begining, before all import statements.

# -*- coding: utf-8 -*-

Similar approach for errors with the term '\xe2'.



Friday, June 24, 2011

Transfer files using ssh

'scp' command to copy files and directories to the server: 

scp your_file_name login@remoteserver.com:path to copy files, 
and scp –r your_directory_name login@remoteserver.com:path to copy entire directories.

grep character count

Use grep to count the frequency of a character in a file:

grep o 'character' filename | wc -l

Wednesday, June 22, 2011

Difference between continue and pass in python

Pass is a "do-nothing" statement. Best used with a if-else or try-except condition

Continue means "jump to start of loop", ignoring rest of the statements in the loop

Code Output

for r in [1,2,3]:
   print(r)
   continue
   print(r,"\tagain")

1
2
3

for r in [1,2,3]:
   print(r)
   pass
   print(r,"again")



1
(1, 'again')
2
(2, 'again')
3
(3, 'again')



for r in [1,2,3]:
   if r%2!=0:
      print(r)
   else:
      pass


1
3


for r in [1,2,3]:
   if r%2==0:
      continue
   print(r)


Bad coding practice according to me but nevertheless this is possible.


1
3

Allow wget to overwrite files

Default behavior of wget is to make new files if files of same name are downloaded again, appended with a counter.

wget filename > filename
wget filename > filename.1
wget filename > filename.2

To overwrite the existing file with the new downloads without creating new files, the answer is:

On GNU Wget 1.12 built on linux-gnu:

Use wget -N filename