Contents:
Links to Posts  IO   Data Structures  Strings  Dirs+Files Admin, os, sys Control Flow Introspection Big Templates Modules Parsing


N.B. Find: “looping enumerate(d) sequences”
N.B. Please research “Markdown Implicit Anchor”

Links to Posts Back to Contents


IO Back to Contents

Logging

import logging
logging.basicConfig(filename='example.log',level=logging.DEBUG) # DEBUG => print ALL msgs
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')

Loop over Dir Content

import os, fnmatch, shutil
for filename in os.listdir('.'):
if fnmatch.fnmatch(filename, '*.txt'):
if not os.path.isdir(filename): shutil.copy(filename, 'example_dir')

stdout write

import sys
sys.stdout.write('hello'+'n')

tee stdout to file

# from <a href="http://stackoverflow.com/questions/616645/how-do-i-duplicate-sys-stdout-to-a-log-file-in-python">stackOflow</a>
class Logger(object):
def __init__(self, path2logFile):
self.terminal = sys.stdout
self.log = open(path2logFile, "a")
def write(self, message):
self.terminal.write(message)
self.log.write(message)
self.flush()
def close(self):
self.log.close()
def flush(self):
self.terminal.flush()
self.log.flush()
# usage
# run 1
logger = Logger('run1.log')
sys.stdout = logger
print 'stuff to run1.log & sys.stdout'
print 'more stuff to run1.log & sys.stdout'
sys.stdout = stdoutorig # restore stdout BEFORE closing the log file
logger.close(); logger = None

# run 2
logger = Logger('run2.log')
sys.stdout = logger
print 'stuff to run2.log & sys.stdout'
print 'more stuff to run2.log & sys.stdout'
sys.stdout = stdoutorig # restore stdout BEFORE closing the log file
logger.close(); logger = None

redir stdout to file

import sys
stdoutorig = sys.stdout
sys.stdout = f = open('run.log', 'w', encoding="utf-8")
print 'hello'
sys.stdout.write('hello'+'n')
f.close()
sys.stdout = stdoutorig

redir stdout to StringIO()

# capturePrint.py

def capturePrint(executableStrThatPrints):

import sys, StringIO

# redir sys.stdout
stdoutorig = sys.stdout
sys.stdout = reportSIO = StringIO.StringIO()

eval(executableStrThatPrints)
reportStr = reportSIO.getvalue()

# restore sys.stdout so we can print
sys.stdout = stdoutorig

return reportStr

def testCapturePrint():
from music21 import *
global sBach
sBach = corpus.parse('bach/bwv7.7')
x = capturePrint("""sBach.show('text')""")
print x

if __name__ == '__main__':
testCapturePrint()

readFile2List 1 line

f = open('c:/test.txt', 'r', encoding="utf-8"); l = f.read().splitlines(); f.close()

readFile2String

f = open('c:/', 'r', encoding="utf-8") #on windows add 'b'==binary
s = f.read()
f.close()

readline

>>>print f.readline()
this is the first line
>>>print f.readline()
this is the second line

readLines

f = open('c:/', 'r', encoding="utf-8") #on windows add 'b'==binary
for line in f:
# process(line)
f.close()

writeString 1 liner

f = open('c:/', 'w', encoding='utf-8'); f.write('This is a test\n'); f.close()

writeString

f = open('c:/', 'w', encoding="utf-8") #on windows add 'b'==binary
f.write('This is a test\n') # newline is NOT automatically added!
f.close()

Print to file

python

f = open('out.txt','w', encoding="utf-8")
print >>f, xml_body

rcw – Read Compute Write – filter – file 2 file

linecountmax = 1000
linecount = 0
blanks = 0
inf = open(INFILEPATH, 'r')
outf = open(OUTFILEPATH,'w')

for line in inf:
linecount += 1
if linecount > linecountmax: break
oline = line
if len(line) == 0:
blanks += blanks
# In here put in filtering criteria and action
continue
outf.write(line)

readFile2String

f = open('c:/', 'r') #on windows add 'b'==binary
s = f.read()
f.close()

readFile2String 1 liner

f = open('c:/test.txt', 'r'); s = f.read(); f.close()

readFile2String

f = open('c:/', 'r') #on windows add 'b'==binary
s = f.read()
f.close()

readline

>>>print f.readline()
this is the first line
>>>print f.readline()
this is the second line

readLines

f = open('c:/', 'r') #on windows add 'b'==binary
for line in f:
# process(line)
f.close()

Print to file

<br /># python3
print("string to print", file=open("print.txt", "a"))

# python2
f = open('out.txt','w')
print >>f, xml_body

Print Left Justified

print "%-15s:  [ "%("'"+chordName+"'"), # Left Justified
for p in pitches:
print "%4s,"%(p),
print " ],"

Pretty Print

from pprint import pprint as pp
pp(staves, width=30, depth=2)        # output to stdout

test_outfile = open(test_out_file_path, 'w')
pp(staves, stream=test_outfile, width=30, depth=2)        # output to test_outfile

CSV Reformat

# Derived From: http://www.linuxjournal.com/content/handling-csv-files-python
import csv
ifile  = open('db_daily_reflection.csv', 'rb')
reader = csv.reader(ifile)

items = []
for row in reader:
items.append(row)

ifile.close()
raw_input('hit return to quit')

JSON Parse – loads & Stringify – dumps

<br /># in file model.json
{"first_name": "Guido", "last_name":"Rossum"}

// parse (loads)
f = open('model.json', 'r'); jsonstr = f.read(); f.close()
my_model_json_dict = json.loads(jsonstr)
print(my_model_json_dict.keys())
OUTPUT: dict_keys(['first_name', 'last_name'])

// stringify (dumps)
my_model_str = json.dumps(parsed_json)
f = open('my_model.json', 'w'); f.write(my_model_str); f.close()
OUTPUT: '{"first_name": "Guido", "last_name": "Rossum"}'


Data Structures Back to Contents

slice notation see

x[start:end] # items start through end-1
x[start:]    # items start through the rest of the list
x[:end]      # items from the beginning through end-1
x[:]         # a copy of the whole list

x[start:end:step] # start through not past end, by step

x[-1]    # last item in the list
x[-2:]   # last two items in the list
x[:-2]   # everything except the last two items

+---+---+---+---+---+
| H | e | l | p | A |
+---+---+---+---+---+
0   1   2   3   4   5
-5  -4  -3  -2  -1

# reverse of x
>>> x = [1,2,3,4,5,6]
>>> x[::-1]          # reverse of x
[6,5,4,3,2,1]

# slice assignment
>>> r=[1,2,3,4]
>>> r[1:1]
[]
>>> r[1:1]=[9,8]
>>> r
[1, 9, 8, 2, 3, 4]
>>> r[1:1]=['blah']
>>> r
[1, 'blah', 9, 8, 2, 3, 4]

# list concatenation
>>> a = [1,2,3,4,5]
>>> b = [6,7,8,9,0]
>>> a + b
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
>>> a + b[:3]
[1, 2, 3, 4, 5, 6, 7, 8]

# c   is a shallow copy of   a
>>> a = [1, 2, 3, 4]
>>> b = a
>>> c = a[:]
>>> b[2] = 10
>>> c[3] = 20
>>> a
[1, 2, 10, 4]
>>> b
[1, 2, 10, 4]
>>> c
[1, 2, 3, 20]

dict from k v lists

dict(zip(keyList, valList))

dict from k v strings

dict(zip(keyStr.split(' '), valString.split(' ')))

dict from obj attribs

dict((name, getattr(ob, name)) for name in dir(ob) if not name.startswith('__'))

map cube(x)

seq = range(10)
def cube(x): return x*x*x
map(cube, seq)

map add(x,y)

seq = range(10)
def add(x, y): return x+y
map(add, seq, seq)

reduce

seq = range(10)
def add(x, y): return x+y
reduce(add, seq)

list2string

s = ''.join(L)

list creations

L = s.split('#')
iota=range(5)
zeros=[0]*5
foos=['foo']*8
L=[0,1,2] + [3,4]
four=L[-1]
three=L[-2]
zero_1=L[:2]
two_3_4=L[2:]
one_2_3=L[1:4]
del(L[1])
listoflists=[ [0]*4 ] *5
listoflists=[[0]*4 for i in range(5)]

Here’s how i initialize a list of lists. Rows vary slowest.

nrows = 3; ncols = 5

l_of_ls = [[0]*ncols for i in range(nrows )]

for rix, r in enumerate(l_of_ls):
    for cix, c in enumerate(r):
        print rix, cix, 'val = ',c

RESULT

0 0 val =  0
0 1 val =  0
0 2 val =  0
0 3 val =  0
0 4 val =  0
1 0 val =  0
1 1 val =  0
1 2 val =  0
1 3 val =  0
1 4 val =  0
2 0 val =  0
2 1 val =  0
2 2 val =  0
2 3 val =  0
2 4 val =  0

Also Worth Noting for Indexing Purposes

for rix in range(nrows):
    for cix in range(ncols):
        print l_of_ls[rix][cix],
    print
        
result = '''
    0 0 0 0 0
    0 0 0 0 0
    0 0 0 0 0  
'''   

list comprehension [simple]

vec = [2, 4, 6]
[3*x for x in vec]

list comprehension [if]

vec = [2, 4, 6]
[3*x for x in vec if x > 3]

list comprehension [->list]

vec = [2, 4, 6]
[[x,x**2] for x in vec]

list comprehension

2d-list / matrix shape function

def shape(matrix):
nrow = len(matrix)
ncol = len(matrix[0])
return [nrow,ncol]

transpose / invert matrix (2d) List[-> 2d list]

matrix = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
]
transposed = [[row[i] for row in matrix] for i in range(len(matrix[0]))]
transposed
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
shape(matrix)  # see above 2d-list / matrix shape function
[3, 4]
shape(transposed)
[4, 3]

list comprehension of dict [->list]

d = {1:'a',2:'b',-4:'c'}
[d[k] for k in d]
# ['a', 'b', 'c']

list comprehension [->dict]

l1=1,2,3,4,5; l2=6,7,8,9,0
dict([(k, v) for k,v in zip(l1,l2)])
# {1: 6, 2: 7, 3: 8, 4: 9, 5: 0}

list comprehension [v1, v2]

vec1 = [2, 4, 6]
vec2 = [4, 3, -9]
[x*y for x in vec1 for y in vec2]
# rightmost for varies first
# [8, 6, -18, 16, 12, -36, 24, 18, -54]

list comprehension [->tuples l1, l2]

l1=1,2,3; l2=6,7,8
[{x:y} for x in l1 for y in l2]
# rightmost for varies first
# [{1: 6}, {1: 7}, {1: 8}, {2: 6}, {2: 7}, {2: 8}, {3: 6}, {3: 7}, {3: 8}]

list comprehension [nested]

mat = [
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9],
      ]
[[row[i] for row in mat] for i in range(len(mat[0]))]
# Above list comprehension inverts the matrix
# :)To avoid apprehension when nesting list comprehensions,
#    read them from right to left. :)

dups in a list

dups = set([x for x in l if l.count(x) > 1])

zip

x = ['1', '2', '3']
y = ('4', '5', '6')
z = "789"
zip(x, y, z)
# [('1', '4', '7'), ('2', '5', '8'), ('3', '6', '9')]
# returns a list of tuples, where the i-th tuple contains the i-th
#   element from each of the argument sequences or iterables.

OrderedDict

from collections import OrderedDict
validators = OrderedDict()

validators['IS_ALPHANUMERIC']="Checks that a field value contains only characters in the ranges a-z, A-Z, or 0-9."

validators['IS_DATE']="Checks that a field value contains a valid date in the specified format."

validators['IS_DATE_IN_RANGE']="Works like the IS_DATE validator. Allows specifying a range."

for k, v in validators.items():
    print k, '=', v, '\n'
OUTPUT:
IS_ALPHANUMERIC = Checks that a field value contains only characters in the ranges a-z, A-Z, or 0-9.

IS_DATE = Checks that a field value contains a valid date in the specified format.

IS_DATE_IN_RANGE = Works like the IS_DATE validator. Allows specifying a range.

sorted dict print formatted

cnid = chordNameIntervalDict= {
    'maj':    (0,  4,  7),
    'min':    (0,  3,  7),
    'aug':    (0,  4,  8),
    'dim':    (0,  3,  6),
    'sus2':   (0,  2,  7),
    'sus4':   (0,  5,  7),
    '6':      (0,  4,  7,  9),
    'm6':     (0,  3,  7,  9),
    '7':      (0,  4,  7, 10),
    'maj7':   (0,  4,  7, 11),
    'm7':     (0,  3,  7, 10),
    'dim7':   (0,  3,  6, 10),
    }

#make the keys be the vals and the vals the keys
icnd = intervalChordNameDict = dict([(cnid[k],k) for k in cnid.keys()])

chordNameL = 'maj,min,aug,dim,sus2,sus4,6,m6,7,maj7,m7,dim7'.split(',')

print 'cnid'
for k in chordNameL:
    print ('%7s %s')%(str(k),str(cnid[k]))

print 'icnd'
for k in sorted(icnd):
    print ('%-16s %s ')%(k, icnd[k])

Strings Back to Contents

concat strings

''.join([xStr, yStr])

concat strings [s = concat(s,x)]

xStr = ''.join([xStr, yStr])

for lines in string

keepends = True
for line in str.splitlines(keepends):
    process (line)

string.replace

s = 'spam bacon ham spam ham bacon spam'
s.replace('spam', 'eggs',1) #one
s.replace('spam', 'eggs')   #all

string2list

L = list(s)

string2list [split]

s = 'answerer, client, linesperson, driver'
L = s.split(', ')
print '\n'.join(L)
answerer
client
linesperson
driver

list2string

s = ''.join(L)

format see “format examples”

menuTemplStr = "(T('{0}'), False, URL('default', '{0}'), []),\n"
print menuTemplStr.format('answerer')
(T('answerer'), False, URL('default', 'answerer'), []),

s = 'answerer, client, linesperson, driver'
v = [menuTemplStr.format(itm) for itm in s.split(", ")]
for i in v:
    print i
(T('answerer'), False, URL('default', 'answerer'), []),

(T('client'), False, URL('default', 'client'), []),

(T('linesperson'), False, URL('default', 'linesperson'), []),

(T('driver'), False, URL('default', 'driver'), []),

enum_string 2 dict

enum_string = es = "state, county, city_state, city_name, city_url"
ed = dict(zip(es.split(", "), range(len(es))))
print ed
{'county': 1, 'city_name': 3, 'state': 0, 'city_url': 4, 'city_state': 2}

Thousands Comma Separator

New in 2.7 when formatting an integer, include the comma after the width:

>>> '{:20,d}'.format(18446744073709551616)
'18,446,744,073,709,551,616'

Directories + Files Back to Contents

File Admin

This is all over the place see:
Directory Manipulations – Dive Into Python
fnmatch – file name match
glob
shutil
os.path
Catch All == docs.python 10. File and Directory Access

Join Paths

mid_path =  os.path.abspath(os.path.join(nwctxtDirPath, '..', 'mid'))

Delete Dir (Tree)

import shutil
shutil.rmtree(dirpath)

Delete File

import os
os.remove(filepath)

create a clean directory

# make a cleared out place to store stuff
import shutil
if os.path.isdir(dirpath):
shutil.rmtree(dirpath)
os.mkdir(dirpath)

pwd

import os
os.path.abspath(os.curdir)

print all *.log files in dir

import glob, os
for filepath in glob.glob(os.path.join(myindirpath, "*.log")):
print filepath

sys.path.append (pythonpath)

import sys
if not('.' in sys.path): sys.path.append('.')

copy all .txt files in ‘.’ into ‘./example_dir’

import os, fnmatch, shutil
for filename in os.listdir('.'):
if fnmatch.fnmatch(filename, '*.txt'):
if not os.path.isdir(filename): shutil.copy(filename, 'example_dir')

Split Path Components – os.path

filepath = r'C:\1d\go\nonselenium\db_init_pj\db_init.py'
dirpath, filename = os.path.split(filepath)
fn_base, fn_ext = os.path.splitext(filename)
drive, pathbase = os.path.splitdrive(dirpath)

print "OUTPUT:\n filepath = %s\n dirpath = %s\n drive = %s\n pathbase = %s\n filename = %s\n fn_base = %s\n fn_ext = %s\n"%(filepath, dirpath, drive, pathbase, filename, fn_base, fn_ext)
OUTPUT:
filepath = C:\1d\go\nonselenium\db_init_pj\db_init.py
dirpath = C:\1d\go\nonselenium\db_init_pj
drive = C:
pathbase = \1d\go\nonselenium\db_init_pj
filename = db_init.py
fn_base = db_init
fn_ext = .py

Copy Over Dir Tree

import os, shutil

def update_input():
source_path = 'F:\\1d\\pythonPjs\\ScramblePj\\Scramble'
dest_path = 'F:\\1d\\pythonPjs\\cxfreezePjs\\ScrambleFrzPj\\ScrambleFrz\\input\\Scramble'

if os.path.exists(dest_path):
shutil.rmtree(dest_path)

# This copy leaves svn directories out of the destination
shutil.copytree(source_path, dest_path, ignore=shutil.ignore_patterns('*.svn'))

if __name__ == '__main__':
update_input()
raw_input('hit return to quit')

cd ls isdir filesize

import os
os.chdir(r"C:\1j\1podcasts")
dirlisting = os.listdir('.')
for i in dirlisting:
subpath = './%s'%(i)
if os.path.isdir(subpath):
sublisting = os.listdir(subpath)
for s in sublisting:
size = os.path.getsize(subpath+'/'+s)
print '%35s   %20s %s'%(i, size, s)

Dir Tree Walk

# Notes for the code
'''
os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])

Generates the file names in a directory tree by walking the tree either top-down or bottom-up.

For each directory in the tree rooted at directory top (including top itself),
it yields a 3-tuple</strong> (dirpath, dirnames, filenames).

The following example displays the number of bytes taken by non-directory file
in each directory under the starting directory, except that it doesn’t look under any CVS subdirectory.
'''
import os
from os.path import join, getsize
for dirpath, dirs, files in os.walk('python/Lib/email'):
print dirpath, "consumes",
print sum(getsize(join(dirpath, name)) for name in files),
print "bytes in", len(files), "non-directory files"
if 'CVS' in dirs:
dirs.remove('CVS')  # don't visit CVS directories

Split Path Components (Old Way) – See stack.oflow

drive,path_and_file=os.path.splitdrive(path)
dirpath,file=os.path.split(path_and_file)
filename_and_ext_list = file.split(os.extsep)
def split_folders(dirpath):
path = dirpath
folders=[]
while 1:
path,folder=os.path.split(path)
if folder!="":
folders.append(folder)
else:
if path!="":
folders.append(path)
break
r = folders[::-1]  # reverses folders
return r
folders = split_folders(dirpath)

Admin, os, sys Back to Contents

Python Bash command – Checks if file exists

python -c "import os.path; print os.path.isfile('/path_to/file.xxx')"

Run Shell Commands – See Theory subprocess [NOT commands]

Reality This works!
cmd = 'E:\\Python27\\Scripts\\cxfreeze.bat Scramble\\bin\\cgi\\ende.py --target-dir Scramble\\dist-cgi\\bin\\cgi'
print cmd
os.system(cmd)

Theory
>>> retcode = subprocess.call(["ls", "-l"]) # WAIT 2 Complete - no output

>>> subprocess.check_output(["ls", "-l", "/dev/null"]) # WAIT 2 Complete - returns output
'crw-rw-rw- 1 root root 1, 3 Oct 18  2007 /dev/null\n'

See also: Replacing Older Functions... and Convenience Functions

Timestamp

## joes ts for windows
import time, os, sys
mytimestamp = time.strftime('%y_%m_%d___%H.%M.%S___%a', time.localtime()).replace('.','_')
print(mytimestamp)
# out: 19_12_18___09_25_31___Wed

## time & formatted
# for formatting examples see http://strftime.org/
import time
time.strftime('%y%m%d_%H.%M.%S_%a', time.localtime())
#out: '150217_08.21.18_Tue'

## datetime
import datetime
str(datetime.datetime.now()).replace(':', '.').replace(' ', '_')
#out: 2011-05-30_11.32.56.375000
[no colons or spaces -> good 4 file names]

## 2 Timestamps + duration
import datetime

dt_begin = datetime.datetime.now()
# DO YOUR STUFF IN HERE
dt_end = datetime.datetime.now()

print "End Timestamp: %s"%(str(dt_end).replace(':', '.').replace(' ', '_'))
print "Begin Timestamp: %s"%(str(dt_begin).replace(':', '.').replace(' ', '_'))
print " (End - Begin): %s"%(dt_end - dt_begin)

OUTPUT:
End Timestamp: 2015-09-09_07.45.25.953000
Begin Timestamp: 2015-09-09_07.32.37.656000
(End - Begin): 0:12:48.297000

time delta

from time import time
t0 = time()
print "t0 = %s"%(t0)

def genChordName2PianokeyOctavesDict():
    for chName, pkTup in chordName2pianoKeyDict.iteritems():
        print "loop begin time()-t0 = %s"%(time()-t0)
        pkL = list(pkTup)

        #lower Octaves
        lowerL = list(pkL)
        while True:
            print "lower while begin time()-t0 = %s"%(time()-t0)
            lowerL = [pk-12 for pk in lowerL]
            if min(lowerL)  127:
                break
            chordName2PianokeyOctavesDict[chName] = higherL
        print "loop end time()-t0 = %s"%(time()-t0)

chordName2PianokeyOctavesDict = OrderedDict({})        
genChordName2PianokeyOctavesDict()

Control Flow Back to Contents

Conditional Expressions

x = true_value if condition else false_value

looping dicts

for k, v in knights.iteritems():
    print k, v

looping enumerate(d) sequences

for i, v in enumerate(Seq):
    print i, v

while True … break

while True:
    if min(lowerL) < 0:
        break

continue

for num in range(2, 10):
    if num % 2 == 0:
        print "Found an even number", num
        continue
    print "Found a number", num

try except

try:
    f = open('myfile.txt')
    s = f.readline()
    i = int(s.strip())
except IOError as e:
    print("I/O error({0}): {1}".format(e.errno, e.strerror))
except ValueError:
    print("Could not convert data to an integer.")
except:
    print("Unexpected error:", sys.exc_info()[1])
    raise

raise an exception

def getFolderName(dt_element):
    if isFolder(dt_element):
        return dt_element.getchildren()[0].text
    else:
        raise Exception('Element is NOT a folder.')

yield

def genBarItems(self):
    rval = ()
    for staffIx, staff in enumerate(self.staves):
        BARS = 1
        for barIx, bar in enumerate(staff[BARS]):
            for itIx, it in enumerate(bar):
                rval = (staffIx, barIx, itIx, it)
                yield rval

Introspection Back to Contents

type checking

see stackOverflow: Python canonical type checking

# checking a music21 type
from music21  import chord

if type(itm) is chord.Chord:
    ch = itm
    ...

A function to aid introspection

# from: http://www.diveintopython.net/power_of_introspection/index.html#apihelper.divein
#          Example 4.1. apihelper.py
# also google: python class introspection 
# also see: http://www.ibm.com/developerworks/library/l-pyint/index.html 

def info(object, spacing=10, collapse=1): 
    """
    Print methods and doc strings. 
    Takes module, class, list, dictionary, or string.
    Usage:
        >>> li = []
        >>> info(li)
    """ 
    methodList = [method for method in dir(object) if callable(getattr(object, method))] 
    processFunc = collapse and (lambda s: " ".join(s.split())) or (lambda s: s) 
    print "\n".join(["%s %s" % 
        (method.ljust(spacing),
        processFunc(str(getattr(object, method).__doc__)))
        for method in methodList])

Big Templates Back to Contents

class

See also:

Python 3 – Object Oriented
Python 3 – The Python Tutorial – 9. Classes
“Python Course.eu”; Python 3; Object-Oriented Programming

#from http://www.ibiblio.org/g2swap/byteofpython/read/inheritance.html
class SchoolMember(object):
'''Represents any school member.'''
# class variables go here
school = 'mySchool'
def __init__(self, name, age):
self.name = name
self.age = age
print '(Initialized SchoolMember: %s)' % self.name

def tell(self):
'''Tell my details.'''
print 'School:"%s" Name:"%s" Age:"%s"' % (SchoolMember.school, self.name, self.age),

class Teacher(SchoolMember):
'''Represents a teacher.'''
def __init__(self, name, age, salary):
SchoolMember.__init__(self, name, age)
self.salary = salary
print '(Initialized Teacher: %s)' % self.name

def tell(self):
SchoolMember.tell(self)
print 'Salary: "%d"' % self.salary

class Student(SchoolMember):
'''Represents a student.'''
def __init__(self, name, age, marks):
SchoolMember.__init__(self, name, age)
self.marks = marks
print '(Initialized Student: %s)' % self.name

def tell(self):
SchoolMember.tell(self)
print 'Marks: "%d"' % self.marks

t = Teacher('Mrs. Shrividya', 40, 30000)
s = Student('Swaroop', 22, 75)

print # prints a blank line

members = [t, s]
for member in members:
member.tell() # works for both Teachers and Students

# here's the output:
(Initialized SchoolMember: Mrs. Shrividya)
(Initialized Teacher: Mrs. Shrividya)
(Initialized SchoolMember: Swaroop)
(Initialized Student: Swaroop)

School:"mySchool" Name:"Mrs. Shrividya" Age:"40" Salary: "30000"
School:"mySchool" Name:"Swaroop" Age:"22" Marks: "75"

Main

#!/usr/bin/python
# -*- coding: utf-8 -*-
"""example.py DoesSomething to INFILEPATH Producing OUTFILEPATH
Usage:   example.py INFILEPATH OUTFILEPATH
Example: example.py myInFile   myOutfile
"""
import sys, os, fnmatch, shutil, json

def myFunc(infilepath, outfilepath):
indirpath, infilename  = os.path.split(infilepath)
infilename_root, infilename_ext = os.path.splitext(infilename)
print()
print (infilepath)
print (indirpath)
print (infilename)
print (infilename_root)
print (infilename_ext)

outfilename_new = infilename_root + '_filled' + infilename_ext
outfilepath_new = os.path.join(indirpath, outfilename_new)
print(outfilepath_new)

NUM_ARGS = 2
def main():
args = sys.argv[1:]
if len(args) != NUM_ARGS or "-h" in args or "--help" in args:
print (__doc__)
sys.exit(2)
myFunc(args[0], args[1])

if __name__ == '__main__':
main()

GUI

Here’s what this will look like. The code follows.

gui-screen-shot

# -*- coding: ISO-8859-1 -*-
import sys, time

if not('.' in sys.path): sys.path.append('.')
import midi24txt 

from Tkinter import *
from tkFileDialog import *

# thinking in tkinter http://www.ferg.org/thinking_in_tkinter/all_programs.html

class TheGui:
    def __init__(self, parent):
        #------- frmSetup ----------#
        self.frmSetup = Frame(parent, bd=5)
        self.frmSetup.pack()

        self.inChoices = ('Text', 'Midi')
        self.varRadio = IntVar()

        self.r1 = Radiobutton(self.frmSetup, text="Convert Text INPUT into Midi OUTPUT",
            variable=self.varRadio, value=0, command=self.selRadio)
        self.r1.pack(anchor=W)

        self.r2 = Radiobutton(self.frmSetup, text="Convert Midi INPUT into Text OUTPUT",
            variable=self.varRadio, value=1, command=self.selRadio)
        self.r2.pack(anchor=W)
        #------- frmSetup ----------#

        sep = Frame(parent, width=1, bd=5, bg='black')
        sep.pack(fill=X, expand=1)

        #------- frmIn ----------#
        # http://effbot.org/tkinterbook/tkinter-widget-styling.htm
        self.frmIn = Frame(parent, bd=5)
        self.frmIn.pack()

        self.lblIn = Label(self.frmIn, text='Text Input File Path', width=20)
        self.lblIn.pack(side=LEFT)  

        self.inFilePath = StringVar() # http://effbot.org/tkinterbook/entry.htm
        self.entIn = Entry(self.frmIn, width=20, textvariable=self.inFilePath)
        self.entIn.pack(side=LEFT)

        self.btnIn = Button(self.frmIn, text='Browse', command=self.btnInBrowseClick)
        self.btnIn.pack(side=LEFT)
        #------- frmIn ----------#

        #------- frmOut ----------#
        self.frmOut = Frame(parent, bd=5)
        self.frmOut.pack()

        self.lblOut = Label(self.frmOut, text='Midi Output File Path', width=20)
        self.lblOut.pack(side=LEFT) 

        self.outFilePath = StringVar()
        self.entOut = Entry(self.frmOut, width=20, textvariable=self.outFilePath)
        self.entOut.pack(side=LEFT) 

        self.btnOut = Button(self.frmOut, text='Browse', command=self.btnOutBrowseClick)
        self.btnOut.pack(side=LEFT)
        #------- frmOut ----------#

        sep = Frame(parent, width=1, bd=5, bg='black')
        sep.pack(fill=X, expand=1)

        #------- frmButtons ----------#
        self.frmOut = Frame(parent, bd=5)
        self.frmOut.pack()

        self.btnConvert = Button(self.frmOut, text='Convert', command=self.btnConvertClick)
        self.btnConvert.pack()  

    #------- handle commands ----------#
    def selRadio(self):
        self.lblIn.config(text = self.inChoices[self.varRadio.get()] + ' Input File Path')
        self.lblOut.config(text = self.inChoices[(self.varRadio.get()+1)%2] + ' Output File Path')
        print str(self.varRadio.get())

    def btnInBrowseClick(self):
        rFilepath = askopenfilename(defaultextension='*',
            initialdir='.', initialfile='', parent=self.frmIn, title='select a file')
        self.inFilePath.set(rFilepath)
        print self.entIn.get()

    def btnOutBrowseClick(self):
        rFilepath = asksaveasfilename(defaultextension='*',
            initialdir='.', initialfile='', parent=self.frmIn, title='select a file')
        self.outFilePath.set(rFilepath)
        print self.entOut.get()

    def btnConvertClick(self):
        #defClr = self.btnConvert.cget("bg")
        self.btnConvert.config(relief=SUNKEN)
        self.btnConvert.update()
        #print 'Convert from %s to %s' % (self.inChoices[self.varRadio.get()], self.inChoices[(self.varRadio.get()+1)%2])
        print 'self.varRadio.get()', self.varRadio.get()

        if self.varRadio.get() == 0:
            inputTextFilePath = str(self.inFilePath.get()); outputMidiFilePath = str(self.outFilePath.get())
            print 'midi 4 txt', inputTextFilePath, outputMidiFilePath
            midi24txt.mid4txt(inputTextFilePath, outputMidiFilePath)
        else:
            inputMidiFilePath = str(self.inFilePath.get()); outputTextFilePath = str(self.outFilePath.get())
            print 'midi 2 txt', inputMidiFilePath, outputTextFilePath
            midi24txt.mid2txt(inputMidiFilePath, outputTextFilePath)
        time.sleep(0.5)
        self.btnConvert.config(relief=RAISED)
        self.btnConvert.update()

root = Tk()
root.title("Convert between Midi and Text Files")
#http://infohost.nmt.edu/tcc/help/pubs/tkinter/std-attrs.html#geometry
#http://infohost.nmt.edu/tcc/help/pubs/tkinter/toplevel.html
root.geometry("350x200+10+10")
gui = TheGui(root)
root.mainloop()

cgi

#!/usr/bin/env python
# from: http://webpython.codepoint.net/cgi_file_upload
import cgi, os
import cgitb; cgitb.enable()

try: # Windows needs stdio set for binary mode.
    import msvcrt
    msvcrt.setmode (0, os.O_BINARY) # stdin  = 0
    msvcrt.setmode (1, os.O_BINARY) # stdout = 1
except ImportError:
    pass

form = cgi.FieldStorage()

def upload():
    """ upload the file into files/ """
    fileitem = form['file']

    # Test if the file was uploaded
    if fileitem.filename:

       # strip leading path from file name to avoid
       #    directory traversal attacks
       fn = os.path.basename(fileitem.filename)
       open('files/' + fn, 'wb').write(fileitem.file.read())
       message = 'The file "' + fn +
               '" was uploaded successfully'

    else:
       message = 'No file was uploaded'

    print """\
    Content-Type: text/html\n

%s

    """ % (message,)

def main():
    #cgi.test()
    message = 'from main'
    for k in form.keys():
        if k != 'file':
            message = ''.join([message,
                k+': '+form.getvalue(k)+''])
     print """\
    Content-Type: text/html\n

%s

    """ % (message,)

main()

Modules Back to Contents

File Admin Module

'''fa - the File Admin Module
fa encapsulates some code from the "Admin, os, sys" section of my blog.
See https://joecodeswell.wordpress.com/python-notes/#Admin
'''
import os, shutil, time

def copy_all_files(source_dir_path, dest_dir_path, verbose=False):
    ''' copies all files in the source_dir into the dest_dir
    >>> os.mkdir('./test/out/d4')
    >>> fa.copy_all_files('./test/in', './test/out/d4')
    '''
    assert(os.path.isdir(source_dir_path) and os.path.isdir(dest_dir_path))
    for filename in os.listdir(source_dir_path):
        if not os.path.isdir(os.path.join(source_dir_path,filename)):
            shutil.copy(os.path.join(source_dir_path,filename), dest_dir_path)
    if verbose: print 'All files in %s have been copied into %s .'%(source_dir_path, dest_dir_path)

def delete_all_files(dir_path, verbose=False):
    ''' deletes all files in the dir - NOT RECURSIVE - NOT DIRECTORIES 
    >>> fa.delete_all_files('./test/out/d4')
    '''
    assert(os.path.isdir(dir_path))
    if verbose:
        yn = raw_input('delete all files in %s ? [y or n] '%(dir_path))
        while ((yn.lower() != 'y') and (yn.lower() != 'n')):
            yn = raw_input('delete all files in %s ? [y or n] '%(dir_path))             
        if yn.lower == 'n': return
    for filename in os.listdir(dir_path):
        if not os.path.isdir(os.path.join(dir_path,filename)):
            print 'deleting %s'%(os.path.join(dir_path,filename))
            os.remove(os.path.join(dir_path,filename))
    if verbose: print 'All files in %s have been deleted.'%(source_dir_path, dest_dir_path)

def copy_over_dir_tree(source_dir_path, dest_dir_path, verbose=False):
    ''' RECURSIVELY copy the source_dir OVER the dest_dir replacing it.
    >>> fa.copy_over_dir_tree('./test/in', './test/out/d4')
    '''
    assert(os.path.isdir(source_dir_path) and os.path.isdir(dest_dir_path))
    if os.path.exists(dest_dir_path):
        shutil.rmtree(dest_dir_path)
    shutil.copytree(source_dir_path, dest_dir_path)
    if verbose: print 'The dir tree %s has copied over %s .'%(source_dir_path, dest_dir_path)

### this needs work
##def wait_while_open(file_path, verbose=False):
##    ''' waits while the file is open
##    >>> fa.wait_while_open('./test/in/d1/f1.txt', True)
##    '''
##    assert(os.path.isfile(file_path))
##    is_open = True   #assume it is open
##    while is_open:
##        if verbose: print 'waiting for %s'%(file_path)
##        time.sleep(1)  # sleep for 1 second
##        try:
##            t = open(file_path, "w")
##            t.close()
##            is_open = False
##        except IOError:
##            continue

if __name__ == "__main__":
    print '**running standard doctest'
    import doctest,fa
    os.mkdir('./test')
    os.mkdir('./test/in')
    os.mkdir('./test/out')
    f = open('./test/in/f1.txt', 'w'); f.write('This is test file 1\n'); f.close()
    f = open('./test/in/f2.txt', 'w'); f.write('This is test file 2\n'); f.close()
    f = open('./test/in/f3.txt', 'w'); f.write('This is test file 3\n'); f.close()
    os.mkdir('./test/in/d1')
    os.mkdir('./test/in/d2')
    os.mkdir('./test/in/d3')
    f = open('./test/in/d1/f1.txt', 'w'); f.write('This is test file 1\n'); f.close()
    f = open('./test/in/d2/f1.txt', 'w'); f.write('This is test file 1\n'); f.close()
    f = open('./test/in/d3/f1.txt', 'w'); f.write('This is test file 1\n'); f.close()    

    doctest.testmod(fa)

Parsing Back to Contents

URL Parse

Source: SaltyCrane Blog

from urlparse import urlparse

url = 'http://www.gurlge.com:80/path/file.html;params?a=1#fragment'
o = urlparse(url)
print o.scheme
print o.netloc
print o.hostname
print o.port
print o.path
print o.params
print o.query
print o.fragment
print o.username
print o.password 

Yields:
http
www.gurlge.com:80
www.gurlge.com
80
/path/file.html
params
a=1
fragment
None
None

#all, #from, #make, #on, #one, #out