Python: Method order
*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()
*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()
#!/usr/bin/python
def mylist(a):
fruit=[]
fruit.append(a)
return fruitb=mylist(”donkey”)
print b
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
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))
import difflib
a=difflib.get_close_matches
a("Phil Collins", ["Phil Colins","PhilCollins","Phil Collins","Phil Collins - Genesis"])['Phil Collins', 'PhilCollins', 'Phil Colins']
find -depth -type d -empty -exec rmdir {} \;
# 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
#
Type "help", "copyright", "credits" or "license" for more information.
import Cfg #class you just created
c = Cfg.Cfg()
c.test()
donkeyfrom Cfg import Cfg
c = Cfg()
c.test
bound method Cfg.test of <Cfg.Cfg instance at 0xb7ce35ec>>
c.test()
donkey
import codecs
someFile=”donkeys.txt”
fileObj = codecs.open(someFile, “r”, “utf-8″ )
u = fileObj.read()
print u
>>> 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’
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():