Python Tomboy Notes

Russell Bateman
2015
last update:

Each of these is a Tomboy note. Tomboy is
a PostIt™ or a Windows Sticky Note
but on Linux.

String operations
List operations
Dictionary operations
Tuples
JSON
Traceback
Load module
tempfile example
Unit testing
Argument parsing
time_t in Python
SQL with SQLAlchemy
SAX Parser Sample
Regular expressions

Python string operations
# String is empty or not
if string:
  print 'String not empty'
elif not string:
  print 'String is empty'

# Trim white space from beginning or end of string
string.lstrip()
string.rstrip()

# Truncate string
string = 'cats and dogs'
firstHalf  = string[ :4 ]                  # after column 4 ('cats')
secondHalf = string[ 4: ]                  # up to column 4 (' and dogs')

pos = string.find( 'x' )
string[ 0:pos+1 ]                          # after first 'x'

# Snip string
string[ 5: ]                               # lose up to column 5
string[  :-2 ]                             # lose last two characters

# Split string on character(s)
string = 'This=is=a=test'
stringList = string.split( '=' )
stringList = [ 'This', 'is', 'a', 'test' ]

# Strip file extension
import os.path
name, extension = os.path.splitext( filename )
ext = os.path.splitext( filename )[ 0 ]    # get extension

# Match beginning or end of string
string.startswith( 'match' )
string.endswith( 'match' )

# Find match in string
match in string:
True

# Java StringBuilder-like assembly
from cStringIO import StringIO
buffer = StringIO()
print >> buffer, 'This is %s string builder' % 'the best'
print >> buffer, ' of all time!'
print >> buffer, string
result = buffer.getvalue()

# Find match from beginning or end of string
string.find( match )
string.rfind( match )

# Prepend string to beginning
result = prefix +  string

# Convert number to string
result = prefix +  string

# Ensure string is numeric
string.isdigit()

# Make list from CSV string
list = string.split( ',' )

# Make CSV string from list
list = [ 'a', 'b', 'c' ]
stringList = ', '.join( list )

Python list operations
An array in Python is called a list, but that the identifier list is reserved.

array[ '1', '2', ... 'N' ]

# definition/initialization
array = []

# ontology
element in array

# position in array
position = array.index( element )

# assignment
array[ element ] = newelement

# adding
array.append( element )

# deleting
del array[ position ]

# traversal, [...] denoting optional functionality below
for element in array:
    print element
for index[, element] in range( len( array ) ):
    print index[, element]

Python dictionary operations
A hashmap is called a dictionary. dict is a reserved keyword.

dictionary[ 'key1' : 'value', 'key2' : 'value', ... 'keyN' : 'value' ]

# definition/initialization
dictionary = {}

# ontology
key in dictionary

# assignment
dictionary[ key ] = newvalue

# adding/creating
dictionary[ newkey ] = value

# deleting
del dictionary[ key ]
dictionary.pop( key )

# traversal
for key in dictionary:
    print key, dictionary[ key ]

Python tuples
t1      = ()
red     = ( 'red', )     # trick to get a single value
classes = ( 'physics', 'chemistry', 1997,  2000 )
mix     = red + classes
stuff   = [ 'this', 'that', 'and', 'the', 'other' ]
a       = ( 1, 2, 3 )
b       = a + ( 4, 5, 6 )
options = ()

Functions
cmp( t1, t2 )
len( t )
max( t )
min( t )
tuple( list )          # tuple from list
list( tuple )          # list from tuple

Examples
>>> classes[ 1 ]
'chemistry'

>>> mix
('red', 'physics', 'chemistry', 1997, 2000)

>>> list( mix )
['red', 'physics', 'chemistry', 1997, 2000]

>>> tuple( stuff )
('this', 'that', 'and', 'the', 'other')

>>> b
(1, 2, 3, 4, 5, 6)

>>> options += ( '--all', )
>>> options
('--all',)
>>> options += ( '--xpath', )
>>> options
('--all', '--xpath')

Python JSON
jsonDict   = json.load ( fp )           # —load dictionary from opened file
jsonDict   = json.loads( jsonString )   # —load dictionary from string
             json.dump ( fp, jsonDict ) # —dump dictionary to opened file
jsonString = json.dumps( jsonDict )     # —dump dictionary to string

Python traceback
try:
  do something
except:
  import traceback
  traceback.print_exc()

Sample code

import json

def doPoop():
  fp = open( 'file' )
  json.load( fp )

def doDoPoop():
  try:
    doPoop()
  except Exception as e:
    import traceback
    print( '* * * * * * * * * * * *' )
    print( e )
    print( '* * * * * * * * * * * *' )
    tb = traceback.print_exc()
    print( '* * * * * * * * * * * *' )
    print( traceback.format_exc() )
    print( '* * * * * * * * * * * *' )

Output

* * * * * * * * * * * *
[Errno 2] No such file or directory: 'file'
* * * * * * * * * * * *
Traceback (most recent call last):
  File "poop.py", line 7, in 
    doPoop()
  File "poop.py", line 4, in doPoop
    fp = open( 'file' )
IOError: [Errno 2] No such file or directory: 'file'

Python load module
def loadModule( path ):
    name, extension = os.path.splitext( os.path.split( path ) [ -1 ] )
    module = imp.load_source( name, path )
    return module

Python tempfile example
import os
import tempfile

( fd, path ) = tempfile.mkstemp()
os.write( fd, something )
os.close( fd )
# make use of the file then, ....
os.remove( path )

Python unit testing
import unittest

PRINT = True

class MyTestSuite(unittest.TestCase):
  @classmethod
  def setUpClass( SetsTest ):
      pass

  @classmethod
  def tearDownClass( SetsTest ):
      pass

  def setUp( self ):
    pass

  def tearDown( self ):
    pass

  @unittest.skip( "skipping" )
  def testCase( self ):
    pass

def printOrNot( thing ):
    if PRINT and thing:
        print thing

def printTestCaseName( functionName ):
    if PRINT:
        print 'Running test case %s...' % functionName


if __name__ == "__main__":
    unittest.main()

# vim: set tabstop=4 shiftwidth=4 expandtab:

Python argument parsing
Command line
script.py --zoo keeper --ticket cat dog mouse

Output (what will be in args)
Namespace(ticket=True, filenames=['cat', 'dog', 'mouse'], zoo='keeper')

Code
import argparse

try:
  parser = argparse.ArgumentParser( description='Parse arguments' )
  parser.add_argument( 'filenames', nargs='*' )           # multiple arguments
  parser.add_argument( '--zoo' )                          # prompting ( +argument)
  parser.add_argument( '--ticket', action='store_true' )  # ontological
  args = parser.parse_args()
  print( args )
except Exception as e:
  print( 'Oops: %s' % str( e ) )

Handy function
def optionPresent( args, option ):
    return option in args and args[ option ]

time_t in Python
>>> import time
>>> epochTime = 19320924
>>> gmTime    = time.gmtime( 19320924 )
>>> time.strftime( '%m/%d/%Y %H:%M:%S', gmTime )
'08/12/1970 14:55:24'

Python SQL with SQLAlchemy

(See SQL equivalents on the right hand.)

DATABASE_PATH = 'mysql:///database.db'

# getting started...
Engine        = sqlalchemy.create_engine( DATABASE_PATH )
DBSession     = sqlalchemy.orm.sessionmaker( bind=Engine )
Session       = DBSession()
Session.bind  = Engine

# insert object; ObjectClass has a tablename...
object = ObjectClass( __init__()'s arguments )
and/or fill out object
Session.add( object )                              INSERT INTO tablename ( fields... )
Session.commit()                                   VALUES( values... );

# query one (first) object...
qo = Session.query( ObjectClass )                  SELECT * from tablename
qf = qo.filter( ObjectClass.field == fieldvalue )  WHERE field = fieldvalue;
return qf.first()

# query multiple (list of matching) objects...
qo = Session.query( ObjectClass )                  SELECT * from tablename
qf = qo.filter( ObjectClass.field == fieldvalue )  WHERE field = fieldvalue;
return qf

SAX Parser Sample
import xml.sax

def getResult():
  result         = httpclient.get( uri )
  payload        = result.read()
  resultIdParser = CqPostResponsePayload()
  try:
    xml.sax.parseString( payload, resultIdParser )
  except Exception as e:
    print e.message
  return resultIdParser.getResultId()

class CqPostResponsePayload( xml.sax.ContentHandler ):
  '''
  Parse response payload, looks something like:
  <?xml version="1.0" encoding="utf-8"?><result_id>fWkcTS1a</result_id>
  '''
  def __init__( self ):
    self.result = StringIO()
    self.resultIdCharacters = ''
  def getResultId( self ):
    return self.result.getvalue().lstrip().rstrip()
  def startElement( self, tag, attributes=None ):
    if tag == 'result_id':
      self.resultIdCharacters = ''
    else:
      pass
  def endElement( self, tag ):
    if tag == 'result_id':
       # tie off the result_id...
      print >> self.result, self.resultIdCharacters
    else:
      pass
  def characters(self, content ):
    self.resultIdCharacters += content
--------------------------------------------------------------
def getValueOfTotalAttribute( line ):
    ''' Just the attributes. '''
    parser = HitsTagElementParser()
    try:
        xml.sax.parseString( line, parser )
    except Exception as e:
        print e.message
        return 0
    attributes = parser.getAttributes()
    return attributes

class HitsTagElementParser( xml.sax.ContentHandler ):
    def __init__( self ):
        self.attributes = {}
    def getAttributes( self ):
        return self.attributes
    def startElement( self, tag, attributes=None ):
        if tag != 'our-tag':
            return
        self.attributes = attributes
    def endElement( self, tag ):
        ''' We'll never hit this! '''
        pass
    def characters( self, content ):
        ''' We're uninterested in this. '''
        pass

File I/O
 # Print lines...
try:
  with open( pathname, 'r' ) as f:
    for line in f:
      print line
  #f.close()   --unnecessary since the with clause closes the file
expect Exception as e:
  print( 'I/O operation failed: %s' % e )

Regular Expressions
\d digits
\D nondigits
\w alphanumeric
\W nonalphanumeric
\s whitespace
\S nonwhitespace
\b boundary between word and nonword

\0 whole enchilada
\1 first pattern
\2 second pattern
   etc.


^-?\d+(,\d+)*(\.\d+(e\d+)?)?$
   —matches any (American) decimal number, signed, unsigned, with or
     without decimal point, with or without commas, with or without exponent.

1?[\s-]?\(?(\d{3})\)?[\s-]?\d{3}[\s-]?\d{4}
   —matches any 10-digit telephone number including with a 1 in front,
     whether separated by -, (), spaces or no.

<(\w+)
   —isolates an HTML element name.

(\w+)\.(jpg|png|gif)$
   —matches image filenames in a listing.

^([\w.+]*)@([\w.]+)
   —matches e-mail addresses up to and including things like
      [email protected].

http://regexone.com/