...

Python Language Quick Reference

Commonly used idioms, for my quick reference...


Resources:


print:

x = 'abcd' # abcd
x = "abcd" # abcd
x = "ab'c'd" # ab'c'd
x = 'ab"c"d' # ab"c"d
x = """abcd
efgh""" # """ is like the <pre> tag, maintains line breaks
print 'abcd'
i = 56
print i,'abcd'
print '@' * 5 # print @@@@@
print "%d is lte to %d" % ( number1, number2 )
print '{0:4d} {1} to {2}'.format(42,'answer','everything')

\ # at end of line is line continuation character

input, casting:

x = raw_input('enter value:')
y = int('45')
z = float('4.0')

datatypes:

# all python data types have the 3 properties: value, location, type
id(y) # location in memory
type(y) # typeof

operators:

/ # true division operator
// # floor (integer) division operator
** # exponent operator

control structures:

if number1 <= number2:
 print "%d is lte to %d" % ( number1, number2 )

if 0:
 print "0 is false, any non-zero is true"

if item:
 print "empty or 0 length string, tuple, list dict evaluates to false"

if 'hat' in 'Manhattan':
 print 'contains equivalent'

if grade >= 60:
 print "Passed"
else:
 print "Failed"

if grade >= 90:
 pass # pass keyword
elif grade >= 60:
 print "D"
else:
 print "F"

while product <= 1000:
 product = 2 * product

for counter in range(5): # 0,1,2,3,4
 print counter

for counter in range(1,6,2): # 1,3,5
 print counter
    
for x in range(1, 11):
 if (gender == "Female") and (x == 5): # and, or, not
     continue
 print x
    
for x in xrange(len(list1)):
 print 'xrange does not load whoel range in memory, more efficient'

for index, value in enumerate(list_of_values):
 print index, value # like iterator

importing:


import math
print math.sqrt(900) # import module and use a function from it

from math import sqrt, sin # or from math import * to import everything
print sqrt(4) # dir() would have shown sqrt and sin in session scope

import math as importedMathModule
print importedMathModule.sqrt(4)

from math import sqrt as importedSqrt
print importedSqrt(4)

Put this following code into into it's own file, it can then be imported by another program, or run standalone. Python looks for import first in current directory, then in python path (sys.path).

#!/usr/bin/python
def moduleFunction(Str):
 return (Str)
if (__name__=="__main__"):
 print 'this code runs only when this file is executed standalone'
else:
 print 'and this code would run only when this module is imported'

The import statement will only load a module once.
dir() shows the names of the data in session scope.
dir(name) show the values of the data named name.
(good for seeing the functions available in scope in imported modules, for eg.)


functions:


# user defined function
def squarer(y):
 return y * y
 #return # this returns None (null, false)
#
print squarer(4) # using a user defined function

x = 1 # global variable
#
def a():
 x = 25 # local variable x, shadows the global x
 return
#    
def b():
 global x
 x += 10 # this manipulates the global x

def func(arg0, arg1 = 4): # use of default arguments
 return arg0 * arg1
func1(2) # 8
func1(2,3) # 6
func1(2, arg1=5) # 10

passing by value / reference:

## passing by value , passing by reference
mutable objects are passed by reference to functions
 function(list1) # list is passed by reference, operations on list modify the original
immutable objects are passed by value to functions
 passing in a slice does not modify the original, because slice creates an independent copy
 passing in a single element from the list does not modify the original

data structures:


sequence -> string immutable
      -> list   mutable, conventionally for homogeneous data
      -> tuple  immutable,  conventionally for heterogeneous data
      
map -> dictionary  mutable,  unordered hash map

# packing sequences
stringSequence = "abcdefgh" # string sequence
list1 = [] # empty list
list1 = [0]*5 # create and  initialize list to [0,0,0,0,0]
list2 = [2,4,6] # packing a list with elements
tup1 = () # empty tuple
tup2 = (1,3,5) # packing a tuple with elements
tup2 = 1,3,5 # or, packing a tuple with elements
tup3 = 7, # tuple with 1 element (tup3 = 7, would have made it an integer)
list[3] = '4th item' # setting value in a mutable sequence (list)
dlist = list1[:] # dlist is now a deep copy of list1
[1,2] + [3,4] # cat'ting a list [1, 2, 3, 4]
a = [1,2,3], b = [4,5,6], a.extend(b) # a is now [1, 2, 3, 4, 5, 6]
if 3 in list1: # true, if 3 is present in list1
list1.pop(5) # remove the item in the list at the index i and return it. 
list1.pop() # remove the the last item in the list and return it.
list1.apend('a') # append the item 'a' to the end of list1. 
list1.sort() # sorts list1 inplace
sorted(list1) # returns a sorted copy of list1
[1,2] == [1,2] # comparing lists, in this case evaluates to True

# unpacking sequences
a, b, c = tup3 # a=1, b=3, c=5

len(list1) # gives length
list1[0] # retrieves first element in the list
list1[-1] # retrieves the last element in the list

for item in list1: # iterating over sequence
for counter in range(len(list1)): # iterating over sequence
 list1[counter]

for value in range( 1, 11 ): # setting values over a mutable sequence
 list1 += [value] # assignment also needs to be a list

x = 2
y = 3
x, y = y, x # trick to swap values of x and y, by packing/unpacking into tuple

# slicing sequences
# sequence[i:j+1] gives slice from ith through jth elements

x = 'abcd'
x[:]   # 'abcd'
x[:4]  # 'abcd'
x[0:]  # 'abcd'
x[3:3] # ''
x[0:1] # 'a'
x[1:3] # 'bc'

# dictionary
dictionary = {key1:value1, key2:value2}
keys must be immutable (string, number, tuple, cannot use list)
dict1 = {} # empty dictionary
dict1 = {'a':'A', 'b':'B', 'c':'C'}
dict1["b"] # "B"
dict1["b"] = "Bee" # add/update key value pair into dictionary
del dict1["b"] # removes key value pair with "b" as key

# list methods
append(item)      Inserts item at the end of the list.
count(element)    Returns the number of occurrences of element in the list.
extend(newList)   Inserts the elements of newList at the end of the list.
index(element)    Returns the index of the first occurrence of element in the list. (ValueError exception)
insert(index, item)  Inserts item at position index.
pop(index) , pop()   Removes and returns item at position index, or last element if pop()
remove(element)   Removes the first occurrence of element from the list. .
reverse()         Reverses the contents of the list in place (does not create copy)
sort()            Sorts the content of the list in place. (can use comparator function)

# useful builtins for lists
any, all, map, reduce, zip, max, min, sum, set

# list comprehensions (loads whole list into memory, not good for huge lists)
numbers = [1,2,3,4,5] # mapping
squares = [number*number for number in numbers] # now, squares should have [1,4,9,16,25]
# filtering
numbers = [1,2,3,4,5] 
nums_lt_4 = [number for number in numbers if number < 4] #nums_lt_4 is [1,2,3]
# filtering, then mapping
numbers = [1,2,3,4,5] 
squares = [number*number for number in numbers if number < 4] # squares is now [1,4,9]


# dictionary methods
clear()           Deletes all items from the dictionary.
copy()            Creates and returns a shallow copy of the dictionary
get(key)          Returns the value associated with key, None if not found
get(key, default) Returns the value associated with key, default if not found                
has_key(key)      Returns 1 if key is in the dictionary; returns 0 if not found
items()           Returns a list of tuples that are key-value pairs.
keys()            Returns a list of keys in the dictionary.
popitem()         Removes and returns arbitrary key-value pair as tuple. (KeyError exception, for {})
setdefault(key)   Like get. If key is not in the dict, insert key:None
set_default(key,d)    Like get. If key is not in the dict, insert key:d
update(newDict)   Merges key-value pairs from newDictionary to the current dictionary
values()          Returns a list of values in the dictionary.
iterkeys()        Returns an iterator of dictionary keys.
iteritems()       Returns an iterator of key-value pairs.
itervalues()      Returns an iterator of dictionary values.


# shallow and deep copy of dictionary
dict.copy() does shallow copy, elements in the copy still reference the original
deepcopy(dict) gives copy that is fully independent of orginal (import deepcopy from copy)

# double subscripted sequences (array of arrays)
table1 = [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] # list of lists
table2 = ( ( 1, 2 ), ( 3, ), ( 4, 5, 6 ) ) # tuple of tuples

for row in table1: # accessing using nested loop
 for item in row:
     print item,

table1[0][1] # getting using indices. gets 2nd element in 1st list (2). for list and tuples
table1[0][1] = 20 # setting using indices. only for mutable list

exceptions:


valid forms
try and one or more except
try and one or more except and else
try and finally

try:
 result = number1 / number2
except ValueError:
 print "ValueError"
except ZeroDivisionError:
 print "ZeroDivisionError"
except IOError, message:
 print >> sys.stderr, "error occured:", message
 sys.exit( 1 )
except:
 print "catch-all exception"
else:
 # execute from here if no exceptions

try:
 raise Exception
finally:
 # will execute, even if the corresponding try does not throw exception, or issues a return



#!/usr/bin/python

try:
   fh = open("testfilex", "r")
   fh.read()
except RuntimeError, argument:
   print "Error: can\'t find file or read data", argument

print "the end"



try:
   print "operation that can raise an Error"
except:
   print "caught all exceptions"

try:
    print "operation that can raise an Error"
except Exception, arg:
   print "caught all exceptions with message", arg

try:
    print "operation that can raise an Error"
except KeyboardInterrupt:
    print "keyboard interrupt (ctrl-C) caught"
except Exception, arg:
   print "caught all other exceptions, with message", arg


try:
   print "operation that can raise an IOError"
except IOError:
   print "caught IO Error"

try:
   print "operation that can raise an IOError or ValueError"
except IOError:
   print "caught IO Error"
except ValueError:
   print "caught Value Error"

try:
   print "operation that can raise an IOError or ValueError"
except IOError, ValueError:
   print "caught IO Error, or Value Error"



try:
   print "operation that can raise an IOError"
except IOError:
   print "IO Error happened"
else:
   print "this block executes only if no exception was raised from the try block"

print "this block executes whether the an exeception was raised or not"


program organization:

    


#program illustrating usage with classes

#!/usr/bin/python
import math
class Classy:
    """A simple example class"""
    i = 12345
    def __init__(self,x,y): # for no-arg constructor use def __init__(self):
         self.x = x
         self.y = y
         self.z = 567
    def f(self):
         return 'classy!!!'
    def g(self,z):
        return 'classyg ' + z
    def h(self):
        self.i # use self to access other members of the class
        self.f() # use self to access other members of the class

def main():
    print 'running main'
    classy = Classy(8,9)  # this is how an instance of Classy is created
    # classy = Classy() # if invoking using the noarg constructor (the self is implicit)    
    print classy.__doc__   # A simple example class
    print classy.__file__ # prints name of file containing classy definition
    print classy.f() # classy!!!, this is how a method  of the instance is invoked
    print classy.i # 12345, all members are public in python
    classy.i = 23456 # can also set these public members
    print classy.i # 23456
    print classy.z # z is also a top level member of Classy
    classy.x = 678 # x is also a top level member of Classy, which can be set   
    classy.e = 'tacking on' 
    """ can also create members on instances, but they are only available to this instance, not like a prototype. be careful of clashing with already defined with same name
    """

    Classy.f(classy) # equivalent to classy.f()
    Classy.g(classy, 'gee') # equivalent to classy.('gee')
    fref = classy.f # saving reference to method instance classy.f()
    print fref() # invoking the method ref
    gref = classy.g # saving reference to method instance classy.g(z)
    print gref('ghee') # invoking the method ref, which takes an arg

if (__name__=="__main__"):
 main()
Data attributes
  • Variable owned by a particular instance of a class, each instance has its own value for it.
  • Data attributes are created and initialized by an __init__() method.
  • Simply assigning to a name creates the attribute.
  • Inside the class, refer to data attributes using self, eg, self.full_name
Class attributes
  • Owned by the class as a whole, all class instances share the same value for it.
  • Called “static” variables in some languages.
  • Class attributes are defined within a class definition and outside of any method.
  • Usually access class attributes using self.__class__.name notation.
class sample:
    x = 23
    def increment(self):
        self.__class__.x += 1
>>> a = sample()
>>> a.increment()
>>> a.__class__.x
24
class teacher:
    “A class representing teachers.”
    def __init__(self,n):
        self.full_name = n
    def print_name(self):
        print self.full_name

class counter:
    overall_total = 0  # class attribute
    def __init__(self):
        self.my_total = 0  # data attribute
    def increment(self):
        counter.overall_total = counter.overall_total + 1
        self.my_total =  self.my_total + 1
>>> a = counter()
>>> b = counter()
>>> a.increment()
>>> b.increment()
>>> b.increment()
>>> a.my_total
1
>>> a.__class__.overall_total
3
>>> b.my_total
2
>>> b.__class__.overall_total
3

Class Inheritance:

class AO(): x = 0
class BO(AO): x = 1 # B extends A
class CO(AO): x = 2
class DO(BO,CO): pass # Multiple inheritance is supported)
ao = AO()
bo = BO()
co = CO()
do = DO()

>>> ao.x
0
>>> bo.x
1
>>> co.x
2
>>> do.x
1
>>>

To redefine a method of the parent class, include a new definition using the same name in the subclass. To execute the method in the parent class in addition to new code for some method, explicitly call the parent’s version of method parentClass.methodName(self,a,b,c) """ The only time you ever explicitly pass ‘self’ as an argument is when calling a method of an ancestor """

Class Student:
    def __init__(self,n,a):
        self.full_name = n
        self.age = a
    def get_age(self):
        return self.age
Class Cs_student (student):
    “""A class extending student.”""
    def __init__(self,n,a,s):
        student.__init__(self,n,a) # like super, call __init__ for student
        self.section_num = s
    def get_age(): # Redefines get_age method entirely
        print “Age: ” + str(self.age)

Built-in attributes define information that must be stored for all classes. All built-in members have double underscores around their names:

Some redefinable builtins

__init__ # The constructor for the class
__cmp__ # Define how == works for class
__len__ # Define how len( obj ) works
__copy__ # Define how to copy a class
__repr__ # to determine what to display as output, like toStirng

These attributes exist for all classes.

__doc__ #Variable for documentation string for class
__class__ # Variable which gives you a reference to the class from any instance of it
__module__ # Variable which gives a reference to the module in which the particular class is defined

Private data and methods

  • Any attribute/method with two leading under-scores in its name (but none at the end) is private and can’t be accessed outside of class.
  • Note: Names with two underscores at the beginning and the end are for built-in methods or attributes for the class.
  • Note: There is no ‘protected’ status in Python; so, subclasses would be unable to access these private data either.

Program Skeletons:


Simple progam

#!/usr/bin/python

from time import sleep

def method1():
    print 'method 1'

def main():
    try:
        while True:
            sleep(1.5)
            method1()
    except KeyboardInterrupt:
        print '   caught CTRL-C...'
    print 'done...'

if __name__=="__main__":
    main()

Simple progam in a class

#!/usr/bin/python

from time import sleep

class Runner():

    def __init__(self):
        print "initting ", self.__class__.__name__, __file__

    def run(self):
        try:
            while True:
                sleep(1.5)
                print 'beep...'
        except KeyboardInterrupt:
            print '   caught CTRL-C...'
        print 'done...'

if __name__=="__main__":
    r = Runner()
    r.run()

Running a method in a thread. Running it as a daemon thread will stop thread when program exits. This pattern is good for thread that don't need to be explicitly stopped, and is required to run as long as the main process lives.
Note that in this pattern, there is no way to interrupt the thread when it enters the "sleep" portion in it's code. They are shutdown abruptly, and their resources (such as open files, database transactions, etc.) may not be released properly. For better control over being able to stop the thread, see the following threading patterns that use Event.wait(), or use a pattern with Queues.
See threading reference.

#!/usr/bin/python

import threading
from time import sleep

def method1(arg1):
    while True:
        sleep(1.5)
        print 'method 1', arg1

def main():
    thread1 = threading.Thread(target=method1, args=('arg1',))
    thread1.setDaemon(True) 
    thread1.start()
    print 'thread1 started...'

    try:
        while True:
            pass
    except KeyboardInterrupt:
        print '   caught CTRL-C...'
    print 'done...'

if __name__=="__main__":
    main()

Using a threading.Event object to control threads.
Note the use of Event.wait(), in method1. Use this if you need to sleep inside a thread because wait() is interruptible, while time.sleep() is not, and the thread will be blocked while it sleeps.

#!/usr/bin/python

import threading
from time import sleep

def method1(arg1, thread_event):
    while (not thread_event.is_set()):
        thread_event.wait(1.75) # interruptible sleep equivalent
        print 'method 1'
    print 'thread stopped'

def main():
    thread_event = threading.Event()
    thread1 = threading.Thread(target=method1, args=('arg1',thread_event))
    thread1.start()
    print 'thread1 started...'

    loop = True
    while loop:
        input_str = raw_input('enter x to stop thread:')
        if 'x' == input_str:
            print 'stopping thread'
            thread_event.set()
            loop = False
        else:
            print 'entered', input_str
    print 'done...'

if __name__=="__main__":
    main()

Using Queues to thread-safely communicate between threads. Also see how to use threads on classes.
See queue reference.

#!/usr/bin/python

import threading
from Queue import Queue, Full
from time import sleep

class WorkerThread(threading.Thread):
    def __init__(self, queue):
      threading.Thread.__init__(self)
      self.queue = queue
      print 'starting', self.__class__.__name__

    def run(self):
        while True:
            data = self.queue.get() # blocking get
            print 'working on data', data # do some work on gotten data
            sleep(1.5) # simulating processing time
            self.queue.task_done() # signals to queue job is done
            pass

class ManagerThread(threading.Thread):
    def __init__(self, queue):
      threading.Thread.__init__(self)
      self.queue = queue
      print 'starting', self.__class__.__name__

    def run(self):
        while True:
            for letter in range(ord('A'), ord('Z')+1):
                data = chr(letter).upper()
                sleep(1.2)
                print 'putting data', data
                try:
                    self.queue.put(data, False) # put_nowait, non-blocking
                except Full: 
                # by catching and ignoring the Full error, we're willing to discard data  
                #  that was not consumed by the WorkerThread.get() in a timely manner
                    print 'queue full, discarding and not putting data', data


def main():
    queue1 = Queue(maxsize=2) # FIFO queue. set maxsize <= 0 for infinite sized queue.

    consumer_thread = WorkerThread(queue1)
    consumer_thread.setDaemon(True)
    consumer_thread.start()

    producer_thread = ManagerThread(queue1)
    producer_thread.setDaemon(True)
    producer_thread.start()

    queue1.join() # wait on the queue until everything has been processed

    try:
        while True:
            sleep(3.0)
            print 'beep...'
    except KeyboardInterrupt:
        print '   caught CTRL-C...'
    print 'done...'

if __name__=="__main__":
    main()

Using threading.Timer to kick off code after an interval.

#!/usr/bin/python

import threading
from time import sleep

def method1(arg1):
    print 'method 1', arg1

def main():
    timer1 = threading.Timer(5.0, method1, args=('arg1',))
    print 'starting timer'
    timer1.start()

    loop = True
    while loop:
        input_str = raw_input('enter x before 5 seconds to stop timer:')
        if 'x' == input_str:
            print 'stopping timer'
            timer1.cancel()
            loop = False
        else:
            print 'entered', input_str
    print 'done...'

if __name__=="__main__":
    main()

todo:


exceptions
list sort function
  sort([compare-function]) Sorts the content of the list in place. The optional parameter compare-function is a function that specifies the compare criteria. The compare-function takes any two elements of the list (x and y) and returns -1 if x should appear before y, 0 if the orders of x and y do not matter and 1 if x should appear after y.

iterators  
classes and objects