Last updated: 2004.05.20
Python is a nice little scripting language that is well designed. That means that its features and syntax actually make sense. Here are some notes on using Python. Additional information can be found at Python.org, the home of Python.
One of the coolest little Python software out there is the edna MP3 server. It really shows what can be done in Python. Edna also contains my first open source software work, even if it is a tiny tiny bit of a patch to the software !
This is a quick summary of language features:
int(string) -> int
float(string) -> float
str(int) -> str
str(unicode_str)->str
list(imatable_tuple)->list
Q: How do I write for x = 0 to 9?
A: for x in range(0,10): print x
List are represented by square brackets:
[1,2,3,6,7] is a list of integers
[ (a,1), (c,3)] is a list of tuples
To create/initialize a list, use the repeat operator on lists.
For example, initialize a list of length 10, with zeros:
L1 = [0] * 10
L1.extend(L2)
is the same as:
L1[len(L1):] = L2
L1 = [1,2,3]
L2 = L1[:]
note:
L2 = L1 just makes L2 an alias of L1, i.e. L1 and L2 are references
pointing to the same actual list object -- dangerous.
prove that by doing
print id(L1), id(L2)
you will see both id's are the same.
Use tuples if your objects can be immutable !
L1.sort()
sorts it in place.
The lambda function is a great tool for writing simple function in line. Typically you can use this with the map and filter functions. [tbw]
The filter function let you apply a functional filter to a list. The map function let you transform a list. [tbw]
a = '123456' a[start:end] a[0] = '1' # note: starts from zero a[5] = '6' a[0:6] = '123456' # note: end is non inclusive Cool stuff: negative values can be used in both start and end index: a[0:-1] = '12345' Leaving out index means beginning or end a[:-1] = '12345'
Reading text file is slow if you read the file line by line. The prefered method is to use the readlines() method to read the entire file into memory:
f = open("path/to/filename")
lines = f.readlines()
for line in lines:
# do something with line
line = line[:-1] # remove newline at the end
If the file is too large, you can use the optional size hint argument to tell readlines to read complete lines from the file, but not to use up all memory:
A dictionary is setup using the {} braces. dictionary = {'key1':'data1', 'key2','data2' }
One big problem with Python is that it is not as easy to setup to connect to real databases (like Oracle) then P**l. So let's focus on something that Python does well -- a simple object database using the shelve.py module. Or use the more elaborated ZODB, part of the ZOPE web application platform. ZODB is available as a standalone package for use outside of ZOPE.
This module lets you store Python objects in a disk based database. It uses the underlying anydbm.py module to store the data, after converting the object to a string using pickle.py.
i = 9
def f(n=i):
print 'in f(), n=',n
f(1)
f()
i=2
f()
This will produce:
in f(), n= 1 in f(), n= 9 in f(), n= 9 <=== not 2
This is because the default argument referencing the global "i" is only evaluated once when the parser parses the function definition. You have to set the default to None and test for None to get the more logical behaviour:
i = 9
def f(n=None):
if n==None:
n=i
print 'in f(), n=',n
f(1)
f()
i=2
f()
This will produce:
in f(), n= 1 in f(), n= 9 pin f(), n= 2
The xml library returns unicode strings, which may upset string comparison?
To specify a sequence/tuple/list of strings with a single member, don't forget the trailing comma, otherwise, due to the wonderful semantics of Python, you ended up passing in a list of characters in the case of tuples:
def i_want_a_list(mylist):
for e in mylist:
print e
i_want_a_list( ('this is wrong'))
i_want_a_list( ('this is right',))
i_want_a_list( ['this is ok'])
i_want_a_list( ('this is ok also',])
python is a great language, but why is it still so much less popular that the other P* languages (Perl, PHP)?
Q: How do I convert a string to a list? A: Use the split function to split the string. You can also specify the separator (defaults to whitespaces) by calling split with an addition argument specifing a single character for used as the separator. import string str = 'this is a string of words' mylist = string.split(str) print mylist Q: How do I parse command line arguments? A: Use the getopt module. It works like the Unix getopt function. You specify a list of options and getopt.getopt will parse them out and return the list in a list. You should catch the getopt.error exception to handle bad arguments: import sys, getopt # note: use argv[1:] to skip the name of the script # parses -d for debug, -f to specify a file. try: opts, args = getopt.getopt(sys.argv[1:],"df:") except getopt.error: print "usage: cmd -d -fQ: How do I use the calendar module? A: For some reason the calendar module is not documentd. Q: How do I know what exception was thrown? A: use: (t,v) = sys.exc_info[:2] print '%s %s' % (t,v) or traceback.print_exc() They get you to the sys.exc_type and sys.exc_value value. Q: How do I read a file and convert the content to a list, but drop the annoying end of line character? A: f = open('filepath') lines = map(lambda s: s[:-1],f.readlines()) return lines Q: How do I get a list of file names from a directory, and filter the file name? A: The filter functin is usefule here: names = dircache.listdir('/somedirectory/') # Now filter file names using regular expression # only take java source files names = filter(lambda f: re.match('^[a-z][A-Z].*\.java',f),names) Q: How do I run system commands? A: Use the commands module, which will return the output as well as the return code: (status,output) = commands.getstatusoutput('some command') Q: How do I setup Peer Web Server in NT to use Python to write cgi? A: Use regedit and enter the following string value: at: HKEY_LOCAL_MACHINE System CurrentControlSet Services W3SVC Parameters Script_Map Add: ".cgi" = "c:\Program Files\Python\Python.exe %s %s" Adjust path accordingly.