Archive

Archive for May, 2009

Python: Method order

May 29th, 2009

*NOTE: def() method/function must be defined before it is called. If the def is below where it is called will get a ‘NameError: name ‘donkey’ is not defined’

def donkey():

print “hello”

a=donkey()

Uncategorized

Python: Return a list or dictionary from a method

May 29th, 2009
#!/usr/bin/python
def mylist(a):
fruit=[]
fruit.append(a)
return fruit

b=mylist(”donkey”)
print b

Uncategorized

Get zipfile contents info. python.

May 28th, 2009
import zipfile

a=zipfile.ZipFile('/path/to/zipfile.ZIP')
for x in a.namelist():
...     if x.endswith('.zip'):
...             info=a.getinfo(x)
...             print "%s %s" % (info.filename, info.file_size)
...

123.zip 4563
254.zip  5435
donkey.zip 8979

Uncategorized

Find occurence of items in list.

May 27th, 2009

letterDict = {}
all_letters_list =
['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r'
,'s','t','u','v','w','x','y','z']
test_string = “in mississippimississippimississippimississippi pigs can
fly like the wind”

# saving the dict
for letter in test_string:
letterDict[letter] = letterDict.get(letter, 0) + 1

print letterDict

=========

print ‘-’ * 60
print ‘Examine the line below to see how it worked ‘
print ‘ 1 - inserting:  letterDict[letter] = letterDict.get(letter, 0) +
1  - self initiates with value of 0 and increments +1′
print ‘ 2 -  reading :  letterDict.get(testLetter, 0)  - when reading -
insert a default count of 0 for those letters not found.’
print ‘-’ * 60
print ‘test_string = ‘, test_string
print
print ‘letterDict = ‘, letterDict
print
print ‘Histogram (count) of letters in test string’

# reading the dict, but inserting default 0 for letters not found.
for testLetter in all_letters_list:
print ‘%s - %2s’ % (testLetter, letterDict.get(testLetter, 0))

Uncategorized

Python: Get similar matching strings

May 22nd, 2009
import difflib
a=difflib.get_close_matches
a("Phil Collins", ["Phil Colins","PhilCollins","Phil Collins","Phil Collins - Genesis"])

['Phil Collins', 'PhilCollins', 'Phil Colins']

Uncategorized

Delete all empty directories in current path.

May 21st, 2009

find -depth -type d -empty -exec rmdir {} \;

Uncategorized

Resolving Drive that won’t unmount.

May 20th, 2009

# umount /dev/sdb1
umount: /dev/sdb1: device is busy
umount: /dev/sdb1: device is busy
# fuser -m /dev/sdb1
/dev/sdb1: 3322

(3322 is id of the process that uses the device. if you like to know what it is, you may try this:

# ps aux | grep 3322)

# kill -9 3322
# umount /dev/sdb1
#

Uncategorized

Creating and calling a class - python

May 14th, 2009
Type "help", "copyright", "credits" or "license" for more information.
import Cfg #class you just created
c = Cfg.Cfg()
c.test()
donkey

from Cfg import Cfg
c = Cfg()
c.test
bound method Cfg.test of <Cfg.Cfg instance at 0xb7ce35ec>>
c.test()
donkey

Uncategorized

Read text file with ‘\xeff’ garbage data to utf-8

May 8th, 2009

import codecs
someFile=”donkeys.txt”
fileObj = codecs.open(someFile, “r”, “utf-8″ )
u = fileObj.read()
print u

Python

Grab last element of a list/string

May 5th, 2009

>>> path=’/donkey/file.cfg’
>>> var=path.split(’/')
>>> print var
['', 'donkey', 'file.cfg']
>>> var[-1]
‘file.cfg’

or can do

>>>varv.pop()
‘file.cfg’

#note you must assign path.split(’/') to a string or path.string will
just print out the last letter of the 3rd element which is ‘g’

Uncategorized

Removing garbage data from string

May 4th, 2009

Many times when you parse a file using split() or the csv module you return strings that have garbage characters in the line. Take the below string for example…

>>> badstring=’\xef\xbb\xbf#Some information’
>>> print badstring
#Some information
>>> if badstring.startswith(’#'):
…     print “Hello”

>>>

The above doesn’t print ‘Hello’ becuase of the garbage data. So we need to create a method to remove the junk chars.

Here is what we do….

def remove_garbage():

Uncategorized