From Omnia
Revision as of 12:13, 3 February 2023 by Kenneth (talk | contribs) (→‎3.x)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Subpage Table of Contents



Python (programming language) - Wikipedia -

Python is an interpreted, general-purpose high-level programming language whose design philosophy emphasizes code readability. Python aims to combine "remarkable power with very clear syntax", and its standard library is large and comprehensive. Its use of indentation for block delimiters is unique among popular programming languages.
Python supports multiple programming paradigms, primarily but not limited to object-oriented, imperative and, to a lesser extent, functional programming styles. It features a fully dynamic type system and automatic memory management, similar to that of Scheme, Ruby, Perl, and Tcl. Like other dynamic languages, Python is often used as a scripting language, but is also used in a wide range of non-scripting contexts.
The reference implementation of Python (CPython) is free and open source software and has a community-based development model, as do all or nearly all of its alternative implementations. CPython is managed by the non-profit Python Software Foundation.
Python interpreters are available for many operating systems, and Python programs can be packaged into stand-alone executable code for many systems using various tools.


"Python was conceived in the late 1980s and its implementation was started in December 1989 by Guido van Rossum at CWI in the Netherlands as a successor to the ABC programming language (itself inspired by SETL) capable of exception handling and interfacing with the Amoeba operating system. Van Rossum is Python's principal author, and his continuing central role in deciding the direction of Python is reflected in the title given to him by the Python community, Benevolent Dictator for Life (BDFL)." [1]


"The language is named after the BBC show “Monty Python’s Flying Circus” and has nothing to do with reptiles. Making references to Monty Python skits in documentation is not only allowed, it is encouraged!" [2]

We as people are "Pythonites" [3]

The Zen of Python

"The core philosophy of the language is summarized by the document 'PEP 20 (The Zen of Python)'" [4]


    Long time Pythoneer Tim Peters succinctly channels the BDFL's
    guiding principles for Python's design into 20 aphorisms, only 19
    of which have been written down.

The Zen of Python

    Beautiful is better than ugly.
    Explicit is better than implicit.
    Simple is better than complex.
    Complex is better than complicated.
    Flat is better than nested.
    Sparse is better than dense.
    Readability counts.
    Special cases aren't special enough to break the rules.
    Although practicality beats purity.
    Errors should never pass silently.
    Unless explicitly silenced.
    In the face of ambiguity, refuse the temptation to guess.
    There should be one-- and preferably only one --obvious way to do it.
    Although that way may not be obvious at first unless you're Dutch.
    Now is better than never.
    Although never is often better than *right* now.
    If the implementation is hard to explain, it's a bad idea.
    If the implementation is easy to explain, it may be a good idea.
    Namespaces are one honking great idea -- let's do more of those!

Easter Egg

    >>> import this

Other Sources:



Linux Installation

RHEL Distribution installation:

yum -y install python python-setuptools

Ubuntu Distribution installation: [5]

# enable 'universe' in the sources.list
# "Mercurial is included in the Universe repository - activate this repo first"
sudo apt-get install mercurial

CentOS 5 Option Recommended Dependencies:

yum -y install gcc make sudo readline-devel openssl-devel ncurses-devel zlib-devel gdbm-devel sqlite-devel bzip2-devel tk-devel tcl-devel
# this leaves: 2.6.6: _bsddb, bsddb185, dl, imageop, sunaudiodev
# this leaves: 2.7.1: _bsddb, bsddb185, dl, imageop, sunaudiodev
# this leaves: 3.2: none

Ubuntu 12.04 LTS 64bit dependencies:

apt-get install gcc make sudo lib64bz2-dev tcl-dev lib64ncurses5-dev libssl-dev libreadline-dev tk-dev libsqlite3-dev libgdbm-dev
# maybe: lib64readline-dev libsqlite3-dev tk-dev
# this leaves: 2.7: _bsddb, bsddb185, dl, imageop, sunaudiodev


CentOS dependencies:

yum -y install gcc make sudo readline-devel openssl-devel ncurses-devel zlib-devel gdbm-devel sqlite-devel bzip2-devel tk-devel tcl-devel

Ubuntu 12.04 LTS 64bit dependencies:

apt-get install gcc make sudo lib64bz2-dev tcl-dev lib64ncurses5-dev libssl-dev libreadline-dev tk-dev libsqlite3-dev libgdbm-dev


# PYVER=2.6.6
# PYVER=2.7.5
# PYVER=2.7.6
# PYVER=2.7.9
# PYVER=2.7.10
mkdir -p ~/.src ; cd ~/.src
wget --no-check-certificate$PYVER/Python-$PYVER.tgz
tar -zvxf Python-$PYVER.tgz
cd Python-$PYVER
./configure --prefix=/opt/python-$PYVER && make clean && make
sudo make install

sudo /opt/python-$PYVER/bin/python -m ensurepip --upgrade
sudo /opt/python-$PYVER/bin/pip install --upgrade pip
sudo /opt/python-$PYVER/bin/pip install requests

export PATH=/opt/python-$PYVER/bin:$PATH
echo -e "\n\nexport PATH=/opt/python-$PYVER/bin:\$PATH\n" >> ~/.bash_profile
# echo -e "\n\nexport PATH=/opt/python27/bin:\$PATH\n" >> ~/.bashrc

# NOTE: remove any old versions from your .bash_profile

Bootstrapping the pip installer -



mkdir -p ~/.src ; cd ~/.src
tar -zvxf Python-${VER}.tgz
cd Python-${VER}
./configure --prefix=/opt/python${VER}
# sudo make install
sudo checkinstall

export PATH=/opt/python${VER}/bin:$PATH
echo -e "\n\nexport PATH=/opt/python${VER}/bin:\$PATH\n" >> ~/.bash_profile

# have to manually update version
rm /opt/python${VER}/bin/python
ln -sfn python3.8 /opt/python${VER}/bin/python
#cp /opt/python${VER}/bin/python3.8 /opt/python${VER}/bin/python

sudo apt install ...
yum install libffi-devel openssl-devel

Note, if you want the libraries to be shared (like python-dev):

./configure --enable-shared





mkdir -p ~/src ; cd ~/src
tar -zvxf Python-3.2.tgz
cd Python-3.2
./configure --prefix=/opt/python32
sudo make install

export PATH=/opt/python32/bin:$PATH
echo -e "\n\nexport PATH=/opt/python32/bin:\$PATH\n" >> ~/.bash_profile


mkdir -p ~/src ; cd ~/src
tar -zvxf Python-3.3.0.tgz
cd Python-3.3.0
./configure --prefix=/opt/python3.3.0
sudo make install

export PATH=/opt/python3.3.0/bin:$PATH
echo -e "\n\nexport PATH=/opt/python3.3.0/bin:\$PATH\n" >> ~/.bash_profile


mkdir -p ~/.src ; cd ~/.src
tar -zvxf Python-3.3.4.tgz
cd Python-3.3.4
./configure --prefix=/opt/python3.3.4
make clean
sudo make install

export PATH=/opt/python3.3.4/bin:$PATH
echo -e "\n\nexport PATH=/opt/python3.3.4/bin:\$PATH\n" >> ~/.bash_profile


mkdir -p ~/.src ; cd ~/.src
tar -zvxf Python-3.4.0.tgz
cd Python-3.4.0
./configure --prefix=/opt/python3.4.0
sudo make install

export PATH=/opt/python3.4.0/bin:$PATH
echo -e "\n\nexport PATH=/opt/python3.4.0/bin:\$PATH\n" >> ~/.bash_profile


Purpose: to install pip

Note: required for several 'python install' installations.

Python Package Index : setuptools 0.6c11 -

By Package Manager:

yum install python-setuptools

By Source:

mkdir -p ~/.src ; cd ~/.src
wget --no-check-certificate$VER.tar.gz
tar -zvxf setuptools-$VER.tar.gz
cd setuptools-$VER
# sudo python install
sudo /opt/python-2.7.9/bin/python install

Fix header:

# Only needed on older versions
sed -i 's#!/usr/bin/python#!/usr/bin/env python#g' /opt/python279/bin/easy_install

Install pip:

sudo /opt/python-2.7.9/bin/easy_install pip

Sample pip usage:

sudo /opt/python-2.7.9/bin/pip install paramiko

IDLE and Interactive Mode

Interactive Mode - "When commands are read from a tty, the interpreter is said to be in interactive mode. In this mode it prompts for the next command with the primary prompt, usually three greater-than signs (>>>); for continuation lines it prompts with the secondary prompt, by default three dots (...)." [6]

Can use '_' to reference last output:

>>> 5 + 6
>>> print _


Python 2.7 EOL

DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.


Hello World

shebang (also called a hashbang) of Python:

#!/usr/bin/env python
print "Hello World"

NOTE: can also just use "#!/usr/bin/python"

Standard Script

Standard Script:

def main(args):
  # do logic

if __name__ == "__main__":

Needed for things like pylint

White Space

Python uses white space to indicate code blocks. Instead of { use a : and indent consistently (4 spaces).

if a == b:
    print 'equal'

Convert tabs to spaces:

sed -i 's/\t/    /g' *.py

Remove trailing whitespace:

sed -i 's/\s*$//' *.py
sed --in-place 's/space:\+$//' fileName

Clean project:

# find . -name "*py" -exec dos2unix {} \;
# find . -name "*py" -exec sed -i 's/\t/    /g' {} \;
# find . -name "*py" -exec sed -i 's/\s*$//' {} \;
find . -name "*py" -exec dos2unix {} \; -exec sed -i 's/\t/    /g' {} \; -exec sed -i 's/\s*$//' {} \;

Trim Tailing White Space

sed -e 's/space:*$//')"
find . -name "*.py" -exec sed -i -e 's/space:*$//')" {} \;

ref: [7]


print -

Print: # include new line

print("Hello World")
print "Hello World"   # python 2 only

Suppress new line: [8]

print('#', end='')    # python 3
print "Hello World",  # python 2 only (does produce a single separating white space though)

import sys
sys.stdout.write('.')  # no separating white space
# flush output
# optional, if new line won't come for some time
import sys

Multiple with separator:

print("hello", "world")  # separated by spaces
print("hello", "world", sep = '.')  # separated by period


mystr = "abc"
myint = 5
myflt = 3.141592
print( "%s %d %.3f" % (mystr, myint, myflt) )
print( "{0} {1} {2}".format(mystr, myint, myflt) )  # string's format function (python > 2.7)
print( "{a} {b} {c}".format( b = myint, a = mystr, c = myflt) )  # by name
print( "={a:4s}={b:3d}={c:10.3f}=".format( b = myint, a = mystr, c = myflt) )  # type and padding

Padded floating point decimal number: (rounds)

print "%06.2f" % 2.319         # "002.32"
print "%6.2f" % 2.311          # "  2.31"
print "{:06.2f}".format(2.319) # "002.32"
print "{:6.2f}".format(2.319)  # "  2.32"


# this is a comment
# this is a comment
x = y  # this is a comment

Using a non assigned string constant as a comment: (also used for doc strings, not generally recommended)

multi line comment

multi line comment can comment out """

This can be used like a C "#if 0" concept to comment out whole sections of code.

Using false block to comment out a block code: (does not ignore bad syntax though)

if False:
   # code in this block is ignored

doc strings

Using a non assigned string constant as a comment: (also used for doc strings, not generally recommended)

multi line comment


dir - list functions and properties of objects:

import math
dir(math)  # module
dir(dir)   # function
dir("")    # object  - "", [], {}, etc...
dir(__builtins__)  # built in functions
dir()      # list imported objects, and dunder methods

Note: dunder (double under) methods determine special functionality (eg. __add__ for +)

help - manual printout: (class doc string, function doc string, data, etc)

import math
help(math)    # full module info
help(len)     # full function info
help(["foo"]) # full object info

Pydoc: (command line, same as 'import math, help(math)'

$ pydoc math

Documentation String: (aka docstring, doc string)

print(math.pow.__doc__)  # module doc string
print(len.__doc__)       # function doc string

Object or function type:

mylist = []
isinstance(mylist, list)  # True
type(s) is list           # True

Note: isinstance() and type() not equivalent when objects and subclass get involved

IDLE interactive help:



Basic Data Types

str = "string"  # string
num = 5         # integer
flt = 5.0       # float
nothing = None  # None  (not the same as undefined)

Undefined. Variables start off in an 'undefined' state. It is always better to initially set the variable to None to avoid this state. To check if undefined: [9] [10]

 except NameError:
   x = None

Rules: PEP8 - Method Names and Instance Variables

  • any length
  • letters, numbers, underscore
  • first character can't be number
  • case sensitive
  • cannot use keywords

Multiple assignment:

x = y = z = 0              # Zero x, y and z
x, y, z = 1, "two", 3.0    # x = 1, y = "two", z = 3.0

Swap variables: (done in parallel)

a, b = b, a

Unpack variables: (requires equal items on each side)

a, b, c = [1, 2, 3]

Other Data Types:

  • tuple
  • list
  • dictionary
  • set
  • objects

Everything in Python is an object that has:

  • identity: id(obj)
  • type: type(obj)
  • value: obj
    • mutable - id() stays the same on modification (dictionary, lists, objects)
    • immutable - id() changes on modification as new object is created (string, integer, tuple)

Check if Variable Exists

    a # does a exist in the current namespace
except NameError:
    a = 10 # nope
'a' in vars() or 'a' in globals()
'a' in vars(__builtins__)
if hasattr(a, 'property'):

Ref: [11] [12]


num = 5
print "num: %d" % num
>>> print "Today's stock price: %f" % 50.4625   1
>>> print "Today's stock price: %.2f" % 50.4625 2
>>> print "Change since yesterday: %+.2f" % 1.5 3


+ - * /
//  # integer division
%   # modulus
**  # exponent

Compact Arithmetic:

i+=1  # Increment - Note, there is no 'i++'
i*=2  # Multiply
i%=2  # modulus

Math Functions:

import math
abs(x)  # built in

String interpolation:

%% % character
%d integer
%x hex
%X hex (upper)
%f float
%s string
x = 1/81
print('value: %.2f' % x)  # 0.01
print('value: %.5f' % x)  # 0.012345
print('%d %d %d' % (1, 2, 3))

Type check:

if not isinstance(myvar, (int, float)): ...


STRINGS ARE IMMUTABLE! (to modify convert to a list)

Strings: (String Constants)

str = "Hello"
str = 'Hello'
str = """
"""      # can also use ...

String literals can span multiple lines: (no wrapping when \ used)

hello = "hello\



Raw string: (back slashes are not escaped)

myrawstr = r"hello\nworld\
how are you"
myrawstr = r"C:\"  # ERROR - limitation of what won't be escaped


str = 'hot' + 'dog'
print "Hello" "World"

Can't concat numbers

str = 5 + 'test'  # error
num = 5
str = num + 'test'  # error
str = str(5) + str(num) + 'test'  # ok

Repeated Concatenation:

str = 10 * 'ha'
str = 'ha' * 10  # same
print '-'*60  # --------------------

String Indexing:

s = 'hello'
print( s[0] )    # is 'h'
print( s[-1] )   # is 'o'
print( s[len(s) - 1 ] )  # is also 'o'

Modify Strings: [13]

s[1] = 'x'  # ERROR!
ERROR: can't modify string by index

# solution:
s = list("Hello World")
s[0] = 'C'
print( "".join(s) )

String Slicing:

s = 'hello'
print( s[0:3] )  # 'hel'
i = 1
print( s[i:i+1] )  # 'e'
print( s[:3] )  # 0:3 'hel'
print( s[4:] )  # 4:end 'o'
print( s[:] )  # start:end 'hello'
print( s[-2:] )  # -2:end 'lo'

For loop:

s = 'hello'
for c in s:
  print c  # c = 'h' first time

Escape characters:

\\  \'  \"  \n  \r  \t

Alternate loop:

s = 'hello'
for i in range( len(s) ):
  print s[i]

Ord and Char (ASCII):

ord('a')  # 97
chr(97)   # 'a'
0-9 : 48-57
A-Z : 65-90
a-z : 97-122

List Functions: (dir on any string)





"a".rjust(2, '0')  # '0a'
"1".ljust(4, '0')  # '1000'

String Test Functions:

"HELLO".isupper()   # True
"hello".islower()   # True
"hello".isalpha()   # True
"12.22".isdigit()   # False (because of '.')
"12.22".isnumeric() # False (because of '.')
"hel2".isalnum()    # True
"e" in "hello"      # True
"hello".startswith('he')  # True
"hello".endswith('lo')    # True

Note: To check for floats: [14]

def is_number(s):
        return True
    except ValueError:
        return False

String Modify Functions: (sort of... Strings are immutable)

"HELLO".lower()           # 'hello'
"hello".upper()           # 'HELLO'
" hello ".strip()         # 'hello' remove leading/tailing whitespace
" hello ".lstrip()        # 'hello ' remove left whitespace
" hello ".rstrip()        # ' hello' remove right whitespace
"".split('.')      # ['www', 'com']   <- list
"".partition('.')  # ('www', '.', 'com')  <- tuple
"".join( ('a','b','c') )  # "abc"  join strings, list or tuple
"-".join( ('a','b','c') ) # "a-b-c"  join with separator
"23".zfill(4)             # "0023"

String Search Functions:

"hello".find('lo')   # 3  (-1 on fail)
"hello".index('lo')  # 3 (exception on fail)
"hello".rfind('lo')  # 3 right to left (-1 on fail)
"hello".rindex('lo') # 3 right to left (exception on fail)

Formatting: (form of concatenation)

mystr = "abc"
myint = 5
myflt = 3.141592
print( "%s %d %.3f" % (mystr, myint, myflt) )
print( "{0} {1} {2}".format(mystr, myint, myflt) )  # pep 3101 style - string's format function (python > 2.7)
print( "{a} {b} {c}".format( b = myint, a = mystr, c = myflt) )  # by name
print( "={a:4s}={b:3d}={c:10.3f}=".format( b = myint, a = mystr, c = myflt) )  # type and padding

Split multi line string:

for line in str.split('\n'):  print(line)
lines = str.splitlines()      # returns list
lines = str.splitlines(True)  # returns list (each includes new line characters)

Input a string:

name = input('What is your name: ').strip()  # python 3
name = raw_input('What is your name: ').strip()  # python 2

Leet Speak:

leet_table = .maketrans('EIOBT', '31087')
'BE COOL. SPEAK LEET!'.translate(leet_table)

String Functions:

newstr = mystr.strip()             # remove white space on both sides
newstr = mystr.rstrip()            # remove white space on right side
newstr = mystr.lstrip()            # remove white space on left side
newstr = mystr.replace('\n', ' ')  # replace string with another
newstr = mystr.replace('jello', 'hello')  # replace string with another

String Formatting

PyFormat: Using % and .format() for great good! -

Zero Padding

3 zero padding:

"{:03}".format(1)  # 003

3 zero padding, specify position:

"{1:03}".format(1, 2)  # 002

Old method:

"%03d" % 2  # 002

chr and ord

Ord and Char (ASCII):

ord('a')  # 97
chr(97)   # 'a'


Creates a list of numbers. Remember: range always ends one less than you want!


ten = 10
one = 1
range(ten)               # [0,1,...,9]   starts with zero by default
range(one, ten + 1)      # [1,...,10]    actually get 1 to 10
range(ten, one - 1, -1)  # [10,9,...,1]  count down
range(2, 10 + 1, 2)      # [2,4,...,10]  even numbers


Solution for decimal range() step value:

def drange(start, stop, step):
    r = start
    while r < stop:
        yield r
    r += step

sub_ten = drange(0.0, 1.0, 0.1)





Check Type:

type(5)     # <class 'int'>
type(5.0)   # <class 'float'>
type('5')   # <class 'str'>
type(None)  # <class 'NoneType'>
type(print) # <class 'builtin_function_or_method'>
type(())    # <class 'tuple'>
type([])    # <class 'list'>
mytype = int
if type(1) is mytype: ...

Boolean Logic

True and False:

bol = True
bol = False

Boolean Operators: (in order precedence)

a == b
a != b
not a
a and b
a or b

Note: Short-circuit evaluated - 'and' and 'or'

Return Boolean:

return myint >= 16

Get Boolean from string: [15]

# Note: no built in way to do this
myString = "false"
val = (myString == "true")
True if myString=="True" else False
s in ['true', '1', 't', 'y', 'yes', 'yeah', 'yup', 'certainly', 'uh-huh']
def str2bool(v):
  return v.lower() in ("yes", "true", "t", "1")


None is Python's version of NULL or NIL.

None is a singleton (Python only has one copy of None in the interpreter).

if myvar is None:

If Conditionals


==, !=, not, and, or, <, >, <>, <=, >=, in, not in, is, is not

If statement:

if a == b:
elif c < age < d:

Shorthand conditional: (ternary operator)

val = True if a == b else False

is - checks for identical objects - same id()

# None is a singleton, so this works great for None
if myvar is None:

Other shorthand:

def mycmp(x, y):
  if x > y: return True
  else: return False

Switch/Case statement:

  • there is no built in switch/case statement, use "if ... elif ... elif ..." sequence instead

For Loop

for is used for iterating over an iterable sequence (eg. lists, dictionaries, tuples). (for each)

  1. range(start, end + 1, skip)
for i in range(10):
  print(i)  # 0 .. 9
for i in range(5, 10):
  print(i)  # 5 .. 9
for i in range(10, 0, -1):
  print(i)  # 10 .. 1
for i in [1, 2, 3]:  # iterate over list
  print(i)  # 1 2 3

Discard variable: [16]

for _ in range(10):  # repeat and discard variable (good for pep8 check)
  print "hello world"
for key in my_dict.keys(): ...
for value in my_dict.values(): ...
for key, value in my_dict.items(): ...

Compact version:

for i in range(10): print(i)    # 0 .. 9

Access index: [17]

# using enumeration
for idx, val in enumerate(mylist):
  print idx, val
# using range
for ix in range(len(mylist)):
  print mylist[ix]
# list comprehension
[ (ix, mylist[ix]) for ix in range(len(mylist))]

While Loop

i = 0
while i < 10:
  i = i + 1
while True:
  ...  # infinite loop

Breaking Loops

break statement:

while True:
  if s == 'done':
    break  # jump out of the loop

continue statement: (works with while too)

 for i in range(10):
   if i == 2:
     continue  # skip 2


No return value: (None)

x = print()
# x is 'None'

Note: specifying no return value is the same as this:

return None

No Operation - 'pass':

def noop():


def area(radius):
  """ This is a doc string """   # can be " ... " also...
  import math
  return math.pi * radius ** 2

Function Doc string:


Function name:


Local variable:

name = "old"
def cname():
  name = "new"
print(name)  # is still 'old'

Global variable:

name = "old"
def cname():
  global name
  name = "new"
print(name)  # is now 'new'

By convention starting point is: (not required)

def main():

Parameters: (with default)

def foo(x, y, z = "default"):

Note: don't use mutable for defaults, as the object is remembered across runs! (unless that is what you want)

# each call to t() will increase list
def t(foo=[]):
# fix mutable issue
def named_param(a, foo=None):
  foo = foo or []
  if not foo:

Parameters by keyword:

def foo(x = '1', y = '2', z = '3'):
foo(y = 'a', x = 'b')   # order is not important with keywords

Trick for optional parameter:

def do(x = None):
  if x == None:

Function tests - functions are callable



Pass is a null operation (noop):

Do nothing, noop, take no action:


Great for creating minimal classes:

class MyEmptyClass:

Greate for making stub functions/methods:

def initlog(*args):
  pass  # remember to implement this!


Take take a slice of strings, lists, tuples, etc...

a = [0,1,2,3,4,5]
a[0]    # 0 - not a slice
a[-1]   # 5 - not a slice
a[0:1]  # [0]
a[1:3]  # [1, 2]
a[:2]   # [0, 1]
a[:-1]  # [0, 1, 2, 3, 4] - all but last
a[2:]   # [2, 3, 4, 5]
a[::2]  # [0, 2, 4] - stride
a[::-1] # [5, 4, 3, 2, 1, 0] - reverse
range(0,10)[::2]  # [0, 2, 4, 6, 8] - stride
a = '012345'
a[1:3]  # '12'
a[::2]  # '024'

Compound Statements

Compound statements: (generally discouraged)

fun1(); fun2(); fun3()

Compound with statements: [18]

with A() as a, B() as b: ...
#is equivalent to
with A() as a:
    with B() as b:

PEP 0343 - The “with” statement

f = open('file')
with f: ...

Try: (quick ignore all exceptions, very dangerous)

try: my_broken_function()
except: pass

Passing Command Line Arguments

Parameters are stored in the 'sys.argv' variable:

import sys
print( sys.argv[0] )  # name of script
print( sys.argv[1] )  # first argument
for arg in sys.argv: print( arg )  # list all arguments

Print script name:


Print path to script:

print sys.path[0]

Paths: [19]

import os,sys
print "CWD: ",os.getcwd()
print "Script: ",sys.argv[0]
print ".EXE: ",os.path.dirname(sys.executable)
print "Script dir: ", os.path.realpath(os.path.dirname(sys.argv[0]))
pathname, scriptname = os.path.split(sys.argv[0])
print "Relative script dir: ",pathname
print "Script dir: ", os.path.abspath(pathname)
print 'sys.argv[0] =', sys.argv[0]             
pathname = os.path.dirname(sys.argv[0])        
print 'path =', pathname
print 'full path =', os.path.abspath(pathname)

Environment Variables

import os
a = os.environ.get('MYVAR')               # get environment var
a = os.environ.get('MYVAR','default')     # get environment var
a = os.environ['MYVAR']                   # get environment var
os.environ['MYVAR'] = 'something'         # set environment var
os.environ.update({'MYVAR':'something'})  # set environment var
for env in os.environ:
  print "%s = %s" % (env, os.environ[env])
with open("environment.txt", "w") as f:
  for env in os.environ:
     f.write("%s = %s\n" % (env, os.environ[env]))


Environment Search path:


From Code:


Get site-package directory from shell: [20]

python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"

Import and Modules

Import and Modules -

Import module:

import package            # standard import
import package.code       # nested name space
from package import code  # import specifics from module
import module             # standard import
from math import sin      # import specifics from module
from module import myfunc as fun1  # alias function/object
import math as other_math # alias module
import longname as ln     # alias module
from module import *      # BAD BAD BAD!
import math

Group and organize by:

  • stdlib libraries
  • local libraries
  • 3rd party libraries

Remove import:

del math
from math import *
from math import pow
import math as math_lib   # rename

List functions in module:

import math

List all built in functions:


Note: Modules have the extension .py

Simply module Example:

def hello():
  """ this is a doc string """
  print "Hello World"
# Use the module:
import mymod
# Use everything in the the module, except for names beginning with '_':  (bad practice!)
from mymod import *
# Use specific functions in the module:
from mymod import hello, hi
# Function assignment:
h = mymod.hello
# module name
mymod.__name__   # mymod
__name__         # used inside of module will print current module or "__main__" if directly called

Note: careful when building own modules, as the import literally imports and executes the code, so any code not in functions is executed!

Check if being called directly or indirectly:

if __name__ == "__main__":
  # execute tests...

Standard Script Setup:

def main(args):
  # do logic
if __name__ == "__main__":

Easter egg:

# The Zen of Python
import this

Search path:

  • environment variable 'PYTHONPATH' (on Unix, this is usually .:/usr/local/lib/python)
  • sys.path (list of paths, initialized to PYTHONPATH)
    • sys.path.append('path')

Module folder: [21] [22] [23]

# so you can do "import kenlib.kenmod"
# create file '' in kenlib/

Check if package exists:

    import argparse
except ImportError:
    print >> sys.stderr, "Please install argparse from


if __name__ == '__main__':
if __name__ == '__main__':
    sys.exit(main(sys.argv[1:]) or 0)


  • Lists can be modified (mutable)
  • Tuples are constant (immutable)

List: (mutable or modifiable)

x = []  # empty list
x = [5] # singleton list
s = list("Hello World")
print(s)  # s = ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
s[0] = 'X'
s.append(x)     # append x
s.insert(i, x)  # insert x at i
del(s[i])       # delete item at i
s.sort()        # sort (modify in place!)
s.reverse()     # reverse
s.index(x)      # index of x in s
new = old[:]    # create copy of list
new = list(old) # create copy of list

Check if item in list:

if x in s: ...

Filter list of strings based on contents: [24]

res = [k for k in lst if 'ab' in k]

List comprehension

[n * n for n in range(1, 11)]   # squares of numbers 1 to 10
[c for c in 'pizza']            # list of string 'pizza'
[c.upper() for c in 'pizza']    # list of string 'pizza' uppercase
[n for n in nums if n > 0]      # filters out only positive numbers in nums list
[fname for fname in os.listdir(path)  # on multiple lines
   if os.path.isfile(fname)
   if fname.endswith('.py')]


for index, value in enumerate(mylist):  print index, value




x + s
x * 2

Convert list to string:

"".join(mylist)  # no separator
" ".join(mylist) # space separator
",".join(mylist) # comma separator

Test Membership

x in s

Multi dimensional list:

mylist = [ [0,"hi"], [1,"bye"] ]
print( mylist[0][1] )

Sorting: (see lambda)

data.sort()  # alpha numerica sort
data.reverse()  # alpha numerica sort
# custom sort
data = [1, 5, 3, 9]
def mylistcmp(x, y):
  if x == y: return 0
  if x < y:
    return -1
  else:  # swap 1 and -1 here to reverse
    return 1



lst = ['1a', '2b', '3c', '4d', '5e', '6f']
lst[0]     # '1a'
lst[-1]    # '6f' last item
lst[0:2]   # ['1a', '2b']
lst[0:2] = ['11', '22']   # lst = ['11', '22', '3c', '4d', '5e', '6f'] - assignment to slice
lst[0:2] = []   # remove items
lst[0:0] = ['1a', '2b']   # insert items (at beginning)
lst[-1:-1] = ['7g', '8h']   # insert items (at item BEFORE last), not really what you want, use append()
lst[-1:] = ['7g', '8h']   # replace last item
lst[:] = []   # clear entire list
del(lst[0:2])  # delete slice items

Note: it is not safe to modify a sequence while being iterated over. Create a copy first:

for x in mylst[:]:  # make a slice copy of the entire list
  if len(x) > 6: a.insert(0,x)

Make list from tuple:

list( range(10) )

Merge multiple lists: [25]

def merge(seq):
    merged = []
    for s in seq:
        for x in s:
    return merged

# sample usage:
foo = [['a', 'b'],['c'],['d', 'e', 'f']]
print merge(foo)
>>>['a', 'b', 'c', 'd', 'e', 'f']


Tuple: (immutable or constant)

x = ()    # empty tuple
x = (5,)  # singleton tuple
x = (5)   # NOTE: integer with parenthesis, not tuple

s = tuple("Hello World")
print(s)  # s = ('H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd')
# ERROR: s[0] = 'X'

Make list from tuple:

list( range(10) )

Use in conditionals:

if answer in ('y', 'ye', 'yes'): ...

Convert tuple string to tuple: [26]

x = "(1,2,3)"
t = tuple(int(v) for v in re.findall("[0-9]+", x))

Another Convert tuple string to tuple: [27]

import ast
ast.literal_eval("(1,2,3,4)") # (1,2,3,4)

Named Tuple

What are "named tuples" in Python? - Stack Overflow -

from collections import namedtuple
Point = namedtuple('Point', 'x y')
pt1 = Point(1.0, 5.0)
pt2 = Point(2.5, 1.5)
print p1     # point(x=1, y=5)
print p1.x   # 1.0
print p1[0]  # 1.0


NOTE: Dictionaries are also known as associative arrays, maps or hash tables.

Dictionary: (mutable or modifiable)

color = {}                      # empty dictionary
color = {'red' : 1, 'blue', 2}
print( color['red'] )           # 1
print( color.get('red') )       # 1
print( color.get('red', 'default') )       # red if found, or return 'default'
color['green'] = 3              # able to add new item on the fly!
'red' in color                  # True - check if index exists
del(color["red"])               # delete item
len(color)                      # 2 - count of items

List in dictionary

d.setdefault(key,[]).append(member)  # tricky!

# more tricky: set default type as list
from from collections import defaultdict
d = defaultdict(list)


for index, key in enumerate(mylist):  print index, key


d.get(key, default_val)
d.update(e)   # update with (key, value) pairs in e
d.setdefault(key, value)  # only update if not exist, and return set value

Views: (adjust on the fly as dictionary changes)

d.items()  (key, values)
for k, v in d.items(): print(k, v)

for k in d.keys(): print(k)

for v in d.values(): print(v)

Sorting: (see also lambda)

# custom sort of dictionaries in list
data = [dict(number=x) for x in '036149']  # create list of dictionaries
def mydictcmp(x, y):
  if x['number'] == y['number']: return 0
  if x['number'] < y['number']:
    return -1
  else:  # swap 1 and -1 here to reverse
    return 1

# data.sort(cmp=mydictcmp)  # modifies data

new_data = sorted(data, cmp=mydictcmp)  # better solution


my_dict = {'a':1, 'b':4, 'c':3, 'd':9}
my_dict_keys = my_dict.keys()

def my_dict_sort(x, y):
    if my_dict[x] == my_dict[y]: return 0
    if my_dict[x] < my_dict[y]: return -1
    if my_dict[x] > my_dict[y]: return 1

#sorted_my_dict = sorted(my_dict_keys, cmp=my_dict_sort, reverse=True)
sorted_tuple = sorted(my_dict_keys, cmp=my_dict_sort)
print "Sorted Dictionary:"
for key in sorted_tuple:
    print "%s: %d" % (key, my_dict[key])

# a: 1, c: 3, b: 4, d: 9,
# reverse: d: 9, b: 4, c: 3, a: 1,

Sort and keys:

keys = sorted(mydict.keys())

Sort by value: [28]

import operator
x = {1: 2, 3: 4, 4:3, 2:1, 0:0}
sorted_x = sorted(x.iteritems(), key=operator.itemgetter(1))

Sort options:

# LIST.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
# sorted(iterable, cmp=None, key=None, reverse=False)

Another: (works well!)

sorted(my_dict.items(), key=lambda x: x[1])



Sets are special lists with unique values (good for removing duplicates)

lst = [1, 1, 6, 8, 1, 5]
s = set(lst)
print(s)  # set([8, 1, 5, 6])
item in s  # true

Union (return combination of all):


Intersection (return matching):


Symmetric Difference (return only items not matching either, inverse of intersection):

list( set(mylist).symmetric_difference(set(mylist2)) )
Operation		   Equivalent	Result
len(s) 	  				cardinality of set s
x in s 	  				test x for membership in s
x not in s 	  			test x for non-membership in s
s.issubset(t) 		   s <= t       test whether every element in s is in t
s.issuperset(t) 	   s >= t 	test whether every element in t is in s
s.union(t) 		   s | t 	new set with elements from both s and t
s.intersection(t)	   s & t 	new set with elements common to s and t
s.difference(t) 	   s - t	new set with elements in s but not in t
s.symmetric_difference(t)  s ^ t 	new set with elements in either s or t but not both
s.copy() 	  			new set with a shallow copy of s



Open file, get file descriptor:

fd = open('/dev/null')

File Modes:

'r' read (default)
'w' write (truncate and create if needed)
'a' append (create if needed)

'b' binary
't' text (default)

'+' read and write

Common Functions: (fd)       # read whole file to string      # read n bytes from file to string
f.readline()   # read single line as string (while loop)
f.readlines()  # read all lines to list
f.write(str)   # write string to file (does not auto include new line)
f.writelines(list)  # write list to file (does not auto include new lines)      # seek to start of file    # seek to position
f.truncate()   # truncate file
f.close()      # close file (fd)
f.closed       # True/False flag that reports if file is closed


pydoc file

Read Text File Line by Line

f = open(fname, 'r')  # ('r' optional)
for line in f:  # or f.readlines()
f.close()  # optional

Write list to file (with new lines):


One liner read whole file:

print( open( fname, 'r' ).read() )

Write text file:

# f = open(fname, 'a')  # append text file
f = open(fname, 'w')
f.write( str(10) )  # convert numbers to strings first
f.writelines(["line one\n", "line two\n"])

Read binary file:

def is_gif(fname):
  f = open(fname, 'br')  # not sure 'b' is correct
  first4 = tuple(
  return first4 == (0x47, 0x49, 0x46, 0x38)

Read all characters of file:

c =
while c:
  # if c == ... logic
  c =

Reading file in while loop: [29]

while True:
  if not line: break
  print line  

with - implicit close - PEP 343 -- The "with" Statement: [30] (python >= 2.5)

with open("/etc/passwd") as f:
  # ...,, etc

With statement: (promptly close open handle at end of block)

with open(fname, 'r') as f:
  for ...
# eqivalent to:
f = open("/etc/passwd")

cat: (for grabbing a configuration line)

def cat(filename):
    with open(filename) as f:
        return f.readline().strip()


PEP 343 -- The "with" Statement -

'with' structure:

with EXPR as VAR:

translates to:


Note: The "as VAR" part is optional. Good for locking:

with locking(myLock):

with - implicit close - PEP 343 -- The "with" Statement: [31] (python >= 2.5)

with open("/etc/passwd") as f:

With statement: (promptly close open handle at end of block)

with open(fname, 'r') as f:
  for ...

See also #Files


Try Exception Handling

Exception Handling -

throw execption:

raise IOError("This is a test!")

Catch exceptions:

except ( [EXCEPTION], [EXCEPTION] ):
except [EXCEPTION]:
except [EXCEPTION] as err:  # use "[EXCEPTION], err" in python < 2.6
  print("Error: {}".format(err))
  raise   # reraise caught error that is not handled
else:     # if no exception caught
finally:  # always executed

Note: Usually a good idea to re-raise exception if you don't handle it (just 'raise')

General Exception: (all exceptions inherit from Exception)

  raise Exception('spam', 'eggs')
except Exception as e:

Custom Exception Class:

class MyError(Exception):
  """My Error Exception"""

class MyException(Exception):

rasie MyError("My Error")
class ValidationError(Exception):
    def __init__(self, message, Errors):

        # Call the base class constructor with the parameters it needs
        Exception.__init__(self, message)

        # Now for your custom code...
        self.Errors = Errors
    def __str__(self):
        return "MyErrors - %s" % self.Errors

Rename standard exception:

class Failure(StandardError):
    """Exception for unexpected failures."""

Note: a good place to hide the exception classes in the file.

exception line number

Python When I catch an exception, how do I get the type, file, and line number? - Stack Overflow [32]

import sys, os

    raise NotImplementedError("No error")
except Exception as e:
    exc_type, exc_obj, exc_tb = sys.exc_info()
    fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
    print(exc_type, fname, exc_tb.tb_lineno)


import traceback
    except Exception as E:
        print 'Traceback: %s', traceback.format_exc()

Catching multiple exceptions and getting properties from the exception:

import traceback
import sys

class MyError(Exception):
  """My Error Exception"""

  #raise ValueError("a value error")  # <type 'exceptions.ValueError'>
  raise MyError("a my error")         # <class '__main__.MyError'>
  raise Exception("won't be caught")  # <type 'exceptions.Exception'>
except (ValueError, MyError) as e:
  print type(e)       # <class '__main__.MyError'>
  print e             # __str__() = a my error
  msg = str(e)        # msg = 'a my error'
  print e.__doc__     # My Error Exception
  print e.__str__()   # a my error
  print e.__repr__()  # MyError('a my error',)
  print e.args        # ('a my error',)
  x = e               # __getitem__() allows args to be unpacked directly
  # x, y = e          # if two args were passed, use len(e.args) to see count

  # manually print stack trace (import traceback, sys)
  print "Exception in user code:"
  print '-'*60
  # stack_trace = traceback.format_exc()
  print '-'*60

Object Oriented Programming


  • encapsulates
  • inheritance
  • polymorphism
  • method overriding



class Animal(object):  # default "(object)" not required
  " Animal class "
  def __init__(self, name): = name       # name is private variable
    self.__iam = "animal"  # __iam is private variable
  def __str__(self):       # string representation
    return "name = '%s'" % (
  def __repr__(self):      # object representation (just typing instance in IDLE, or repr())
    return 'Animal(%s)' % str(self)  # returns __str__
  def speak(self, msg='Nothing to say'):

Note: if no __str__ defined, but __repr__ is, then: __str__ = __repr__


from animal import Animal
a = Animal('good')
print(a.__doc__)  # Animal class
print(a)          # name = 'good'
a                 # Animal(name = 'good')
print(     # good
a.speak('hi')     # hi
print(a._Animal__iam)  # animal - access to private variable (shouldn't do)


from animal import Animal
class Dog(Animal):
  " Dog class "
  def __init__(self, name): = name      # name is private variable
    self.__iam = "dog"    # __iam is private variable
  # NOTICE: no __str__, inherited from super class
  def __repr__(self):     # override to reflect "dog"
    return 'Dog(%s)' % str(self)  # returns __str__
  def speak(self, msg='Nothing to say'):   # override method
    super(Dog, self).speak('Dog says: ' + msg)     # call super class method

Subclass usage:

from dog import Dog
d = Dog('bad')
print(d.__doc__)    # Dog class
print(d)            # name = 'bad'
d                   # Dog(name = 'bad')
print(       # bad
d.speak('hi')       # Dog says: hi
print(d._Dog__iam)  # dog - access to private variable (shouldn't do)

Super: [33]

# python 3.0
# python 2.7
super(self.__class__, self).__init__()
super([CLASSNAME], self).__init__()
class Rectangle(Polygon):
    def __init__(self, id, width, height):
        #  super(Rectangle, self).__init__(id)
        super(self.__class__, self).__init__(id)
        self.shape = (width, height)

Get class name:


Full name:

obj.__module__ + "." + obj.__class__.__name__

Private variables: (uses name mangling)

 # direct access:  _classname__varname  (eg. p._Person__age = 44)


isinstance(myobj, myclass)  # check if myobj is a class or subclass of myclass 
issubclass(A, B)  # check if A is subclass of B
issubclass(Exception, BaseException)  # True

Multiple Inheritance: (methods searched in order)

class DerviedClass(Base1, Base2, Base3):

Standard startup script:

class MyClass():
    def main(cls, argv=None):
        # return cls() # creator
if __name__ == "__main__";

Raise an error if a method is not overridden (not implemented)

   def _get_dist(self):
       """Find linux disto. Implemented in subclasses."""
       raise NotImplementedError

Multiple Constructors

class Animal(object):

  def __init__(self, name): = name

  def create_fido(cls):
    return cls("fido")

Descriptor Classes

"In general, a descriptor is an object attribute with “binding behavior”, one whose attribute access has been overridden by methods in the descriptor protocol: __get__(), __set__(), and __delete__(). If any of those methods are defined for an object, it is said to be a descriptor." [34]

object.__get__(self, instance, owner)

    Called to get the attribute of the owner class (class attribute access) or of an instance of that
    class (instance attribute access). owner is always the owner class, while instance is the instance
    that the attribute was accessed through, or None when the attribute is accessed through the owner.
    This method should return the (computed) attribute value or raise an AttributeError exception.

object.__set__(self, instance, value)

    Called to set the attribute on an instance instance of the owner class to a new value, value.

Example: [35]

class Celsius(object):
    def __init__(self, value=0.0):
        self.value = float(value)
    def __get__(self, instance, owner):
        return self.value
    def __set__(self, instance, value):
        self.value = float(value)

class Temperature(object):
    celsius = Celsius()

t = Temperature()
t.celsius = 5    # calls Celsius.__set__
print t.celsius  # calls Celsius.__get__

Note: Also appears in 'pydoc' under "Data descriptors defined here" with object's docstring. Trick: the __doc__ can be overridden in the __init__ method, which makes for dynamic docstrings!


exec code


Execute code in string:

exec( 'print("hi")' )

Example of executing a startup script for IDLE:

if os.path.isfile(''): exec(open('').read())
import os
filename = os.environ.get('PYTHONSTARTUP')
if filename and os.path.isfile(filename):



Will execute a string as though it were pure python code!

eval("print 'hello'")

Common Header Format

__author__ = "software team"
__date__ ="$Feb 15, 2011 1:42:17 PM$"
__version__ = " bandelier@66d9fe1edf3e"
Next should be authorship information. This information should follow this format:

__author__ = "Rob Knight, Gavin Huttley, and Peter Maxwell"
__copyright__ = "Copyright 2007, The Cogent Project"
__credits__ = ["Rob Knight", "Peter Maxwell", "Gavin Huttley",
                    "Matthew Wakefield"]
__license__ = "GPL"
__version__ = "1.0.1"
__maintainer__ = "Rob Knight"
__email__ = ""
__status__ = "Production"

Source: Python Coding Guidelines -

These will show up in the help() like such:

Help on module test:




    __author__ = 'test author'
    __version__ = '1.0'


    test author


Common Environment Variables

1. Command line and environment — Python v2.7.2 documentation -


If this is set to a non-empty string it is equivalent to specifying the -u option. [36]
-u - Force stdin, stdout and stderr to be totally unbuffered. On systems where it matters, also put stdin, stdout and stderr in binary mode.


If this is set to a non-empty string it is equivalent to specifying the -v option. If set to an integer, it is equivalent to specifying -v multiple times.
-v - Print a message each time a module is initialized, showing the place (filename or built-in module) from which it is loaded.


Augment the default search path for module files. The format is the same as the shell’s PATH: one or more directory pathnames separated by os.pathsep (e.g. colons on Unix or semicolons on Windows). Non-existent directories are silently ignored. [37]


Good for threading.

"The Queue module implements multi-producer, multi-consumer queues. It is especially useful in threaded programming when information must be exchanged safely between multiple threads. The Queue class in this module implements all the required locking semantics." [38]

import Queue

bucket = Queue.Queue()

while True:
        item = bucket.get(block=False)
    except Queue.Empty:
        # do stuff with queue

Important methods:

 Queue.put(item[, block[, timeout]])
 Queue.get([block[, timeout]])



def worker():
    while True:
        item = q.get()

q = Queue()
for i in range(num_worker_threads):
     t = Thread(target=worker)
     t.daemon = True

for item in source():

q.join()       # block until all tasks are done


"The module implements three types of queue, which differ only in the order in which the entries are retrieved. In a FIFO queue, the first tasks added are the first retrieved. In a LIFO queue, the most recently added entry is the first retrieved (operating like a stack). With a priority queue, the entries are kept sorted (using the heapq module) and the lowest valued entry is retrieved first."

Queue Types:

 class Queue.Queue(maxsize=0)          # FIFO Queue
 class Queue.LifoQueue(maxsize=0)      # LIFO Queue
 class Queue.PriorityQueue(maxsize=0)  # Priority Queue



Remove Numbers from String

Using list comprehension: [39]

result = ''.join([i for i in s if not i.isdigit()])

Using pattern matching:

>>> re.compile(r'\D+').findall("test 1234 test 1234")
['test ', ' test ']

>>> re.compile(r"\d").sub("", "test 1234 test 1234")
'test  test '

Standard Libraries

user input

line = raw_input()  # Python 2
# line = input()    # Python 2
line = input('What is your name: ')
print(": " + line.strip() + " :")

sys - stdout stderr stdin


import sys
sys.stdout.write("Hello ")  # does not write new line


import sys


import sys
line = sys.stdin.readline()
line = input()   # use raw_input() in Python 2
line = input('What is your name: ')
print(": " + line.strip() + " :")

Read piped stdin line by line: [40]

# cat /etc/passwd | ./     # linux
# type file.txt | python  # windows
# fileinput.input() - returns fileinput object which is iterative
import fileinput
for line in fileinput.input():
    print(": " + line + " :")
# sys.stdin.readlines() - returns list of strings
import sys
for line in sys.stdin.readlines():
    print(": " + line + " :")
# sys.stdin - returns object which is iterative
import sys
for line in sys.stdin:
    print(": " + line + " :")
# ugly while loop: [41]
while True:
        s = raw_input("Say something ")
    except EOFError:
    print ": %s" % s

Check if stdin has any waiting data: [42]

import sys
import select

# if[sys.stdin,],[],[],0.0)[0]:  # alternative old method

if not sys.stdin.isatty():
    print "Have data!"
    print sys.stdin.readlines()
    print "No data"
if len(sys.argv) > 1:
    message = ' '.join(sys.argv[1:])
elif not sys.stdin.isatty():
    # read from pipe in
    message = sys.stdin.readline()
    # read from user
    sys.stdout.write("Message: ")
    message = raw_input().strip()

Regular Expressions


import re  # user regular expressions

Match vs Search:

re.match("cde", "abcdef")  # no match (match searches beginning of string, first match)
re.match("abc", "abcdef")  # match (match searches beginning of string, first match)"cd", "abcdef")  # match (matches any location, first match)

Both re.match() and return objects that are not intuitive to use. You can check the object's group() method for what matched, but I prefer re.findall().


s = 'done'
if re.match('done|quit', s) != None  # True


s = 'done'
if'do', s) != None  # True


msg = 'jello world'
msg = re.sub('jello', 'hello', msg)

For case intensive searches append 3rd parameter "re.I".

Find all:

re.findall("[a-z]+", "aaa111bbb222")       # ['aaa', 'bbb']
re.findall("111([a-z]+)", "aaa111bbb222")  # ['bbb']
re.findall("hello", "HELLO", re.I)         # ['HELLO'] - Case Insensitive


>>> re.split('[a-z]+','111aaa222bbb')
['111', '222', ]
>>> re.split('([a-z]+)','111aaa222bbb')  # parenthesis cause return of all
['111', 'aaa', '222', 'bbb', ]

Replacement Substitution:

# re.sub(pattern, repl, string[, count, flags])
re.sub(r'\\', r'/', r'\\data\files')

Compiled regular express for repeated use: (performance)

valid = re.compile(r"[0-9]+")
valid.match("aaa111bbb")               # not valid
match ="aaa111bbb222")   # valid
print(                   # what matched: '111'

Date and Time

Sleep in seconds:

import time

Use the following functions to convert between time representations:

seconds since the epochstruct_time in UTCgmtime()
seconds since the epochstruct_time in local timelocaltime()
struct_time in UTCseconds since the epochcalendar.timegm()
struct_time in local timeseconds since the epochmktime()

Date formats:

>>> time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
'2014-04-04 12:22:43'

>>> time.time()

datetime.datetime(2014, 4, 4, 12, 22, 43, 58456)
# datetime(year, month, day, hour, minute, second, microsecond)

>>> time.localtime()
time.struct_time(tm_year=2014, tm_mon=4, tm_mday=4, tm_hour=12, tm_min=22, tm_sec=43, tm_wday=4, tm_yday=94, tm_isdst=1)

Current epoch time:


Time zone offset:

standard_tz_offset = - time.timezone / 3600  # 7
daylight_tz_offset = - time.altzone / 3600  # 6

Is daylight saving time: [43]

import time
_.tm_isdst  # 0 or 1
mst_epoch_time = time.time()  # server in MST/MDT
if time.localtime().tm_isdst:  # are we in daylight saving time?
  utc_epoch_time = mst_epoch_time + time.altzone
  utc_epoch_time = mst_epoch_time + time.timezone

Format current time: [44]

time.strftime('%d%b%y')  # '12May11'
time.strftime('%Y.%m.%d %H:%M:%S')  # '2013.08.09 14:01:28'

Time conversions:

# epoch to str
epoch_time = time.time()    # 1375992088.7867229
dt = datetime.datetime.fromtimestamp( epoch_time )    # datetime.datetime(2013, 8, 8, 14, 1, 28, 786723)
date_str = dt.strftime("%Y-%m-%d %H:%M:%S")    # '2013-08-08 14:01:28'
tz_offset = - time.altzone / 3600
if tz_offset < 0:
  tz_offset_str = "-%02d00" % abs(tz_offset)
  tz_offset_str = "+%02d00" % abs(tz_offset)
print date_str + " " + tz_offset_str  # '2013-08-08 14:01:28 -0600'
# UTC epoch to str
gm_epoch_time = calendar.timegm(time.gmtime())    # 1375992114
dt = datetime.utcfromtimestamp( gm_epoch_time )    # datetime.datetime(2013, 8, 8, 20, 1, 54)
dt.strftime("%Y-%m-%d %H:%M:%S +0000")    # '2013-08-08 20:01:54 +0000'
dt.strftime("%Y-%m-%dT%H:%M:%SZ")    # '2013-08-08 20:01:54 +0000'
# Local time str to epoch
epoch_time = time.mktime( time.strptime( iso_string, '%Y-%m-%dT%H:%M:%S' ) )    # 1375992088.0

iso_string='2013-08-08T14:01:28 MDT'
epoch_time = time.mktime( time.strptime( iso_string, '%Y-%m-%dT%H:%M:%S %Z' ) )    # 1375992088.0

# not sure how to specify numerical time zone and pull it in!

# UTC (Zulu) str to epoch, and back again
iso_string = '2013-08-08T20:01:54Z'
epoch_time = calendar.timegm( time.strptime( iso_string.replace('Z', 'GMT'), '%Y-%m-%dT%H:%M:%S%Z' ) )
print epoch_time    # 1375992114
print "%sZ" % datetime.utcfromtimestamp(timestamp).isoformat()  # 2013-08-08T20:01:54Z
# Convert epoch from Local to UTC
epoch_time = 1375992114
tz_offset = - time.altzone / 3600
gm_epoch_time = epoch_time - tz_offset * 3600    # 1376013714
# Convert epoch from UTC to Local
gm_epoch_time = 1376013714
tz_offset = - time.altzone / 3600
epoch_time = gm_epoch_time + tz_offset * 3600    # 1375992114


time.localtime()  # local zime zone
time.gmtime()     # UTC time
today = time.localtime()
today_year = today.tm_year
today_month = today.tm_mon
today_day = today.tm_mday
time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())

Zulu time to epoch time: [45]

s = '2008-09-03T20:56:35.450686Z'
event_time_struct = time.strptime(s.split('.')[0]+ "UTC", "%Y-%m-%dT%H:%M:%S%Z")
epoch_time = calendar.timegm(event_time_struct)

Parse string to time.struct_time (like gmtime()): [46]

time.strptime(time.ctime())  # current time - default format is ctime format: "%a %b %d %H:%M:%S %Y"
time.strptime(time.ctime(time.time()))  # default format is ctime format: "%a %b %d %H:%M:%S %Y"
time.strptime("30 Nov 00", "%d %b %y")

Build from ticks: [47]

localtime = time.localtime( time.time() )  # build time.struct_time from ticks
print time.asctime( localtime )  # Tue Jan 13 10:17:09 2009
dt = datetime.datetime.fromtimestamp( time.time() )  # build datetime.datetime struct from ticks
dt.strftime("%Y-%m-%d  %H:%M:%S")  # '2013-07-31  23:07:48'
time.strptime( time.time(), ")
from datetime import date
now =    # now =, 12,2)
now.strftime("%m-%d-%y")   # '12-02-03'
# dates support calendar arthimetic
birthday = date(1964, 7, 31)
age = now - birthday
age.days  # 14368
ts = ("%Y%m%d%H%M"))         # 201105191118
tstxt = ("%m-%d-%Y %H:%M"))  # 05-19-2011 11:18

Seconds since epoch:

int(time.time())    # 1305842304

Zulu and back again: (Datetime hell)

import time
from calendar import timegm
from datetime import datetime
if __name__ == '__main__':
    iso_string = '2013-06-05T15:19:10Z'
    timestamp = timegm(
            iso_string.replace('Z', 'GMT'),
    print iso_string
    print timestamp
    print '-> %sZ' % datetime.utcfromtimestamp(timestamp).isoformat()

TimeTransitionsImage?action=AttachFile&do=get&target=v1.png [48]



Something I cooked up...

    def utc_time(self, years_offset=60):
        mst_epoch_time = time.time()  # server is in MST
        if time.localtime().tm_isdst: # are we daylight saving time?
            utc_epoch_time = mst_epoch_time + time.altzone
            utc_epoch_time = mst_epoch_time + time.timezone
        return utc_epoch_time

    def tz_time(self, tz_offset, daylight_adjust=True):
        """ return the future time based on timezone offset
            @tz_offset is timezone offset, eg -7 for MST
            @daylight_adjust is flag to auto adjust for daylight time"""
        if daylight_adjust:
            if time.localtime().tm_isdst: # are we daylight saving time?
                _tz_offset = tz_offset - 1
                _tz_offset = tz_offset
            _utc_epoch_time = self.utc_time()
            _adj_time = _utc_epoch_time + _tz_offset * 60 * 60
            return _adj_time

    def future_time(self, tz_offset=-7, daylight_adjust=True, years_offset=60):
        """ Calculate some future year """
        _tz_time = self.tz_time(tz_offset)
        future_utc_epoch_time = _tz_time + years_offset * 365 * 24 * 60 * 60
        # account for leap years
        cur_year =
        cur_year_leap = cur_year / 4
        future_year = cur_year + years_offset
        future_year_leap = future_year / 4
        leap_days = future_year_leap - cur_year_leap
        future_utc_epoch_time += leap_days * 24 * 60 * 60
        return future_utc_epoch_time


Math Functions:

import math
abs(x)  # built in


import random
random.choice(['a', 'b', 'c'])
random.sample(range(100), 10)
random.random()  # random float 0 < x < 1
random.randrange(3)     # integer in [0, 1, 2]
random.randrange(1, 3)  # integer in [1, 2]
random.randint(1, 3)    # integer in [1, 2, 3]

Operating System Operations

Import OS Library:

import os

Common Functions:

# directory functions
os.getcwd()       # get current directory
os.listdir(p)     # get list of directory contents
os.chdir(p)       # change directory
os.mkdir(p)       # make directory
os.makedirs(p)    # make directory and all intermediate
os.rmdir(p)       # remove empty directory, see shutil.rmtree()
os.removedirs(p)  # remove empty directories, see shutil.rmtree()
os.path.exists(p) # does file/directory exist?
os.path.isdir(p)  # does directory exist
os.path.join(folder1, folder2, file, etc)  # portable path join
# file functions
os.remove(p)      # remove file
os.unlink(p)      # same as os.remove()
os.path.exists(p) # does file/directory exist?
os.path.isfile(p) # does file exist
os.stat(fname)    # get file stats
os.stat(fname).st_size  # file size, dst) # create hard link
os.readlink(p)    # read symbolic link
os.rename(old, new) # rename/move file or directory
os.tempnam(p)     # temporary unique name for target path
os.tmpfile()      # create temporary file
os.tmpnam()       # temporary file name
os.path.getsize(filename)  # get file size
# system functions
os.fork()         # fork process
os.getpid()       # get process id
os.getppid()      # get parent's process id
os.kill(pid, sig) # kill a process
os.getlogin()     # get user's login name
# execution
os.system(cmd)    # output to stdout, return exit_status
os.popen(cmd)     # returns a pipe to command

Note: to get path to script:


Get directory and file names:

os.path.dirname(path)  # '/root/test.txt' -> '/root'
os.path.basename(path) # '/root/test.txt' -> 'test.txt'
os.path.splitext(path) # '/root/test.test.txt' -> ('/root/test.test', '.txt') - extract extention
filename.split(".")[-1] # '/root/test.test.txt' -> 'txt' - extract extention

Normalize path, eliminating double slashes, etc.

os.path.normpath(path)  # '/root/..' -> '/' or 'test/..' -> '.'
os.path.abspath(path)   # '/root/..' -> '/' or 'test/..' -> '/root/test'

List dir: (returns list)

os.listdir( os.getcwd() )

List of files:

[p for p in os.listdir(os.getcwd())
   if os.path.isfile(p)]

List of folders:

[p for p in os.listdir(os.getcwd())
   if os.path.isdir(p)]

Build path for local OS: (using appropriate slash)

pathstr = os.path.join("folder", "folder", "file_or_folder")

File and directory management with shutil

import shutil
shutil.copyfile('file1', 'file2')
shutil.move('file1', 'folder2/file1')

File Wildcards:

import glob
glob.glob('*.py')  # return list of files

Walk directory tree: [49]

import os
for dirname, dirnames, filenames in os.walk('.'):
    for subdirname in dirnames:
        print os.path.join(dirname, subdirname)
    for filename in filenames:
        print os.path.join(dirname, filename)

ugly 'deltree' (by kenneth)

import os
def deltree(path):
    files = os.listdir(path)
    for file in files:
        file_path = os.path.join(path, file)
        if os.path.isdir(file_path):

Cleaner 'deltree' (by Kep)

import shutil
# Blow away (if needed), and reclone to proper version
if os.path.isdir("xxx"):

System Platform

import platform

if platform.system() == "Linux":
 # ...
if platform.system() == "Windows":
 # ...
 # ...

Temp Files

import tempfile
filename = tempfile.mktemp()
f = open(filename, 'w')

import os
os.remove(filename)  # delete temp file


Pickling (serializing, making objects persistent):

import pickle
grades = {'bob' : [4, 5, 6],
          'sue' : [3, None, 7, 7]}
outfile = open(fname, 'wb')
pickle.dump(grades, outfile)
infile = open(fname, 'rb')
grades = pickle.load(infile)


Setup logger:

import logging
logging.basicConfig(level=logging.DEBUG,format='[%(asctime)s.%(msecs)03d] %(levelname)s %(filename)s(line:%(lineno)d): %(message)s')

Log Levels:

import logging
msg = "i broke"
logging.warning(msg)   # WARNING:root:i broke
logging.error(msg)     # ERROR:root:i broke
logging.critical(msg)  # CRITICAL:root:i broke
log.setLevel(level)  # debug, info, warn/warning, error, critical

A good convention to use when naming loggers is to use a module-level logger, in each module which uses logging, named as follows: [50]

logger = logging.getLogger(__name__)

Simple example: [51]

log_format='%(asctime)s | %(levelname)s | %(message)s'
log_date_format='%Y-%m-%d %H:%M:%S'
logging.basicConfig(format=log_format, datefmt=log_date_format)
log = logging.getLogger(__name__)

Formatter function:

def makeFormatter(format_str="", date_format="%Y-%m-%d %H:%M:%S"):
    if not format_str:
        format_str = "%(asctime)s | %(levelname)s | %(filename)s:%(lineno)d:%(funcName)s | %(message)s"
    return logging.Formatter(fmt=format_str, datefmt=date_format)

Medium example:

# Create logger
log_format='%(asctime)s | %(levelname)s | %(message)s'
log_date_format='%Y-%m-%d %H:%M:%S'
log_formatter = logging.Formatter(fmt=log_format, datefmt=log_date_format)
log = logging.getLogger(__name__)

# Log to console
ch = logging.StreamHandler()

# Log to file
fh = logging.FileHandler('my_logger.log')

Application Setup (not basic log configuration carries across imported modules):

import logging
def main():
    logging.basicConfig(filename='myapp.log', level=logging.INFO)'Started')

if __name__ == '__main__':

Log multiple lines:

for line in output.splitlines():

Set log level: (warning is default)


Set log level from string value: [52]

# assuming loglevel is bound to the string value obtained from the
# command line argument. Convert to upper case to allow the user to
# specify --log=DEBUG or --log=debug
numeric_level = getattr(logging, loglevel.upper(), None)
if not isinstance(numeric_level, int):
    raise ValueError('Invalid log level: %s' % loglevel)
logging.basicConfig(level=numeric_level, ...)

Log to file:

logging.basicConfig(filename='example.log', filemode='w', level=logging.DEBUG)  # do not append

Format output: (once set, can't be changed?)

logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
# 05/23/2011 12:50:02 PM MESSAGE
logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
# 05/23/2011 12:55:28 PM : WARNING : MESSAGE
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
# 2011-05-23 13:05:30 : WARNING : MESSAGE
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
# 2011-05-23 13:05:30 | WARNING | FILE:FUNC | MESSAGE
logging.basicConfig(format='%(asctime)s | %(levelname)s | %(filename)s:%(funcName)s | %(message)s', datefmt='%Y-%m-%d %H:%M:%S')

Log record attributes -

Format Example:

FORMAT = '%(asctime)-15s %(clientip)s %(user)-8s %(message)s'  # doesn't work!
d = { 'clientip' : '', 'user' : 'fbloggs' }
logger = logging.getLogger('tcpserver')
logger.warning('Protocol problem: %s', 'connection reset', extra=d)
import logging

# create logger with 'spam_application'
logger = logging.getLogger('spam_application')

# create file handler which logs even debug messages
fh = logging.FileHandler('spam.log')

# create console handler with a higher log level
ch = logging.StreamHandler()

# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

# add the handlers to the logger
logger.addHandler(ch)'Message to loggers and handlers')


from string import Template
t = Template('${name}'s fav dish is $food')
t.substitute(name = 'ken', food = 'beans') # throws exception if missing one, but can have more
t.safe_substitute(name = 'ken')  # will fill in what it can

Change delimiter:

class MYT(Template):
  delimiter = '%'
minput = input("File name format (%d - date, %s - size): ")
myt = MYT(minput)
myt.substitute(%d=date, %s=size)


Basic thread: [53]

import thread
def someFunc():
    print "someFunc was called"
thread.start_new_thread(someFunc, ())

More complete basic thread:

import thread
import threading
import time

def run_thread (threadname, sleeptime):
 print "Thread start: " + threadname
 print "Thread end: " + threadname
 thread.interrupt_main()   # throw keyboard interrupt to kill main program

if __name__ == "__main__":
 thread.start_new_thread(run_thread, ("Thread1", 3))
     while 1:
     print "Thread1 exited...." 

Can use a global counter instead of exception to track number of threads:

import thread
import threading
import time

threadcount = 3

def run_thread (threadname, sleeptime):
  global threadcount
  print "Thread start: " + threadname
  print "Thread end: " + threadname
  threadcount -= 1

if __name__ == "__main__":
  thread.start_new_thread(run_thread, ("Thread1", 3))
  thread.start_new_thread(run_thread, ("Thread2", 4))
  thread.start_new_thread(run_thread, ("Thread3", 2))
  while 1:
    if threadcount == 0:
  print "completed"


Thread object: [54]

import threading
t1 = threading.Thread(target=someFunc)
#  with parameters: Thread(target=myfunction, args=('MyStringHere',1)).start()

Basic threading with parameters:

import threading
t = threading.Thread(None, someFunc, None, (param1, param2))
t = threading.Thread(target=client_recv, args=(s, name))
while True:
  if t.isAlive():
    print "Thread is dead..."

Threading as a class: [55]

import threading
class FuncThread(threading.Thread):
    def __init__(self, target, *args):
        self._target = target
        self._args = args
    def run(self):
# Example usage
def someOtherFunc(data, key):
    print "someOtherFunc was called : data=%s; key=%s" % (str(data), str(key))
t1 = FuncThread(someOtherFunc, [1,2], 6)

Threading as a class #2:

import threading
import time
import sys

class MyT(threading.Thread):
  def __init__(self, name, time):
    threading.Thread.__init__(self) = name
    self.time = time
  def run(self):
    print("Thread Start: " +
    for i in range(self.time):
      print("[" + + "]", end='')
    print("Thread Stop: " +

myt1 = MyT('1', 10)

myt2 = MyT('2', 15)

myt3 = MyT('3', 5)

myt4 = MyT('4', 10)




# pass lock to thread, and use in thread as such:
#some operation

Returning data from a thread (using a queue): [56] [57]

# Pass exception back to parent
import sys
import threading
import Queue

class ExcThread(threading.Thread):
    def __init__(self, bucket):
        self.bucket = bucket
    def run(self):
            raise Exception('An error occured here.')
        except Exception:

def main():
    bucket = Queue.Queue()
    thread_obj = ExcThread(bucket)
    while True:
            exc = bucket.get(block=False)
        except Queue.Empty:
            exc_type, exc_obj, exc_trace = exc
            # deal with the exception
            print "Exception Caught:", exc_type, exc_obj
            print "Exception Trace:", exc_trace
        if thread_obj.isAlive():

if __name__ == '__main__':
Exception Caught: <type 'exceptions.Exception'> An error occured here.
Exception Trace: <traceback object at 0x2b2230a285f0>

Another Exception catching queue example: (kenneth's example)

# thread function
def run_thread(queue, item):
  queue.put( Exception("Failure on item:" + item) )

threads = []
queue = Queue.Queue()

# start threads
for item in items:
  t = Thread(None, run_thread, None, (queue, item))

# wait for threads
for t in threads:

# catch and reraise exceptions
while not queue.empty():

Returning data from a thread (using a callback function): [58]

"Another approach is to pass a callback function to the thread. This gives a simple, safe and flexible way to return a value to the parent, anytime from the new thread."
import threading
import time
import sys

class MyThread(threading.Thread):
    def __init__(self, cb):
        self.callback = cb
    def run(self):
        for i in range(10):

def count(x):
    print x

t = MyThread(count)



Interrupt main thread:

# source:

import thread
import time

def main():
        m = thread.start_new_thread(killable_input, tuple())
        while 1:
    except KeyboardInterrupt:
        print "exception" 

def killable_input():
    w = thread.start_new_thread(normal_input, tuple())
    i = thread.start_new_thread(wait_sometime, tuple())

def normal_input():
    s = raw_input("input:")

def wait_sometime():
    time.sleep(4) # or any other condition to kill the thread
    print "too slow, killing imput"

if __name__ == '__main__':


See Python/urllib

Command line arguments

import sys
for x in sys.argv:
    print x

Execute other python script

import sys
sys.argv = [, 'my', 'args', 'here']
execfile('')  # does not support arguments directly

Exit Code


import sys
sys.exit(1)  # exit code of 1 (default is 0)
sys.exit("some error")  # exit code of 1, and message displayed


import syslog
syslog.syslog('Processing started')
if error:
    syslog.syslog(syslog.LOG_ERR, 'Processing started')
import syslog
def log(msg):


Subprocess -

Methods of subprocess (or external command execution):

  • subprocess (only method to rule/replace all others)
  • os.system
  • os.spawn*
  • os.popen*
  • popen2.*
  • commands.*

My New Favorite:

import subprocess
class Run(object):
    class RC(int):
    def __new__(cls, cmd, *args, **kwargs):
        p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        rc = cls.RC(p.wait())
        rc.output = "\n".join(p.stdout.readlines())
        rc.error = "\n".join(p.stderr.readlines())
        return rc

if Run("some command"): print "do something"  # on rc of not 0 (negative or positive)

My Favorite:

def exe(cmd):
    p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    #output = " | ".join(p.stdout.readlines())
    output = "\n".join(p.stdout.readlines())
    rc = p.wait()
    return (rc, output)

def exe_checked(cmd, checked=True):
    p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    #output = " | ".join(p.stdout.readlines())
    output = "\n".join(p.stdout.readlines())
    rc = p.wait()
    if rc and checked:
        raise Exception("Command returned non zero [{0}]: {1}".format(rc, cmd)
    return (rc, output)

def exe_with_stderr(cmd):
    p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subproc
    output = " | ".join(p.stdout.readlines())
    outputerr = " | ".join(p.stderr.readlines())
    rc = p.wait()
    return (rc, output, outputerr)
def exec_program(cmd):
    s = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    stdout, stderr = s.communicate()
    result = s.wait()
    return stdout, stderr, result

Pass along output and return true error code:

import subprocess
retcode =["ls", "-l"])  # true return code, output sent to STDOUT
retcode =["","args"],shell=True)  # Execute in shell, with shell processing
retcode =['C:\\Temp\\a b c\\Notepad.exe'])
retcode =["D:/Python24/Python.exe", "", "arg1", "arg2"])

Note: shell=True is equivalent to: subprocess.Popen(['/bin/sh', '-c', args[0], args[1], ...])

Process STDOUT and STDERR and get return code:

#!/usr/bin/env python
import subprocess
import string

cmd = "cat /etc/passwd | grep ken"
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # stderr=subprocess.STDOUT to combine

out = string.join(p.stdout.readlines())
outerr = string.join(p.stderr.readlines())
retcode = str(p.wait())

print("STDOUT: " + out)
print("STDERR: " + outerr)
print("RETCODE: " + retcode)

Above with stderr redirected to stdout (combined):

import subprocess
p = subprocess.Popen('ls', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in p.stdout.readlines():
    print line,
retval = p.wait()

Replacing shell backtick: [59]

# shell code:
# OUTPUT=`mycmd myarg`
from subprocess import Popen
output = Popen(["mycmd", "myarg"], stdout=PIPE).communicate()[0]

Replacing shell pipeline [60]

# shell code:
# OUTPUT=`dmesg | grep hda`
from subprocess import Popen
p1 = Popen(["dmesg"], stdout=PIPE)
p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
p1.stdout.close()  # Allow p1 to receive a SIGPIPE if p2 exits.
output = p2.communicate()[0]

Replacing os.system(): [61]

# old:
sts = os.system("mycmd" + " myarg")
# new:
p = Popen("mycmd" + " myarg", shell=True)
sts = os.waitpid(, 0)[1]

Control an executed process:

cmd = "/usr/bin/java -Xms256M -Xmx1024M -jar minecraft_server.jar nogui"

p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE,
        stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

# cycle while process continues alive
while p.poll() == None:

  # check if input
  if[p.stdout.fileno()], [], [], 1)[0]:
    while[p.stdout.fileno()], [], [], .1)[0]:
        line = p.stdout.readline()
        # ... process

  # write something to process
  p.stdin.write("tp %s %s\n" % (user1, user2))


  # time to force a quit: (choose which you need)
  p.kill()  # SIGKILL
  p.terminate()  # SIGTERM
  os.kill(, signal.SIGTERM)  # same as first, but not as clean

Other Command Execution

Old method with os.system(): [62]

import os
encoded_wait_code = os.system("some command")  # output sent to STDOUT
# Note: returns code of 0 if command returns 0, but wait() encoded value for all other values

Quick and dirty old method (prints stderr, captures stdout)

import os
out = string.join( os.popen(cmd).readlines() )

Quick and dirty old method (prints stderr, captures stdout, captures ugly wait() encoded return code)

import os
p = os.popen(cmd)
stdout = "".join( p.readlines() )
encoded_wait_code = p.close()  # None if 0
rcode = os.WEXITSTATUS(ugly_rcode) # fix ugly wait() encoded rcode

if os.popen(cmd).close():
  print "error with cmd: %s" % cmd


# or 

Replacing os.system(): [63]

# old:
sts = os.system("mycmd" + " myarg")
# new:
p = Popen("mycmd" + " myarg", shell=True)
sts = os.waitpid(, 0)[1]

More realistic example replacing os.system(): [64]

    retcode = call("mycmd" + " myarg", shell=True)
    if retcode < 0:
        print >>sys.stderr, "Child was terminated by signal", -retcode
        print >>sys.stderr, "Child returned", retcode
except OSError, e:
    print >>sys.stderr, "Execution failed:", e


Select:, wlist, xlist[, timeout])

This is a straightforward interface to the Unix select() system call. The first three arguments are sequences of ‘waitable objects’: either integers representing file descriptors or objects with a parameterless method named fileno() returning such an integer
The optional timeout argument specifies a time-out as a floating point number in seconds. When the timeout argument is omitted the function blocks until at least one file descriptor is ready. A time-out value of zero specifies a poll and never blocks.
if[sys.stdin.fileno()], [], [], .1)[0]:
    line = p.stdout.readline()

full wrapped execution

Process timeout (by Kenneth):

import subprocess
import time
import os
import signal

def cmd(cmd, timeout=300, retry=0):
    # Execute command, time progress, and kill on timeout

    # (status, output) = commands.getstatusoutput(cmd)
    start_time = None
    status = None
    output = None
    for retry_count in range(retry+1):
        if retry_count > 0:
            print("RETRY: " + str(retry_count) + " " + cmd)
        start_time = time.time()
        p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
        timeout_fail = False
        while p.poll() == None:
            if time.time() - start_time > timeout:
                # kill process
                print("TIMEOUT: " + str(timeout) + "sec - killing (" + str( + ") - " + cmd)
                    os.kill(, signal.SIGTERM)
                except OSError as e:
                    print("Kill exception: " + str(e))
                timeout_fail = True
        status = p.poll()
        output =
        if timeout_fail == False:
    seconds = int(time.time() - start_time)
    return (status, output, seconds)


Process input and output wrapper (from minecraft):

import subprocess
import select
import re
import sys

cmd = "/usr/bin/java -Xms256M -Xmx1024M -jar minecraft_server.jar nogui"

p = subprocess.Popen(cmd, shell=True,

# cycle while process continues alive
while p.poll() == None:

    if[p.stdout.fileno()], [], [], 1)[0]:
        while[p.stdout.fileno()], [], [], .1)[0]:
            line = p.stdout.readline()
            # <kiloforce> -tp joe
            match = re.findall("<(.*)> -tp (.*)", line)
            if match:
                user1 = match[0][0]
                user2 = match[0][1]
                print ">> ACTION: Teleporting %s to %s" % (user1, user2)
                p.stdin.write("tp %s %s\n" % (user1, user2))
            if line:
                print ">> SERVER: %s" % line.strip()
                print "The server died!"

    if[sys.stdin.fileno()], [], [], .1)[0]:
        line = sys.stdin.readline()
        print ">> CONSOLE: %s" % line.strip()
        p.stdin.write("%s" % line)

Command With Timeout

python - Using module 'subprocess' with timeout - Stack Overflow -

import subprocess, threading

class Command(object):
    def __init__(self, cmd):
        self.cmd = cmd
        self.process = None

    def run(self, timeout):
        def target():
            print 'Thread started'
            self.process = subprocess.Popen(self.cmd, shell=True)
            print 'Thread finished'

        thread = threading.Thread(target=target)

        if thread.is_alive():
            print 'Terminating process'
        print self.process.returncode

command = Command("echo 'Process started'; sleep 2; echo 'Process finished'")

My version:

import subprocess
import threading

class Command(object):
    def __init__(self, cmd, capture=False, shell=True):
        self.cmd = cmd
        self.process = None
        self.thread = None = shell
        if type(cmd) == list:
   = False
        self.rc = None = None
        self.stdout = None
        self.stderr = None
        self.capture = capture  # capture stdout?

    def run(self, timeout=None, wait=True):
        def target():
            print '>> Thread started (%s)' % self.cmd
            if self.capture:
                self.process = subprocess.Popen(self.cmd,, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                self.process = subprocess.Popen(self.cmd,
            print '>> Thread PID: %d' %
            self.stdout, self.stderr = self.process.communicate()
            self.rc = self.process.returncode
            print '>> Thread finished: %d' % self.rc

        self.thread = threading.Thread(target=target)

        if wait:
        return self  # so you can do: c = Command().run()

    def wait(self, timeout=None):
        print '>> Waiting for thread...'
        if timeout:
        if self.thread.is_alive():
            print '>> Terminating process'
            self.rc = self.process.returncode
            if self.thread.is_alive():
                print '>> Failed to terminate'
            print '>> Thread completed'
        #print self.process.returncode

    def is_alive(self ):
        return self.thread.is_alive()

command = Command('do something').run()

command2 = Command(['do', 'something', 'long']).run(wait=False)

command3 = Command('want output', capture=True).run()
print command3.stdout

System call

os.system("some_command with args")
os.system("some_command < input_file | another_command > output_file")
stream = os.popen("some_command with args")

Here's a summary of the ways to call external programs and the advantages and disadvantages of each:


os.system("some_command with args") passes the command and arguments to your system's shell. This is nice because you can actually run multiple commands at once in this manner and set up pipes and input/output redirection. For example,
os.system("some_command < input_file | another_command > output_file")
However, while this is convenient, you have to manually handle the escaping of shell characters such as spaces, etc. On the other hand, this also lets you run commands which are simply shell commands and not actually external programs.


stream = os.popen("some_command with args") will do the same thing as os.system except that it gives you a file-like object that you can use to access standard input/output for that process. There are 3 other variants of popen that all handle the i/o slightly differently. If you pass everything as a string, then your command is passed to the shell; if you pass them as a list then you don't need to worry about escaping anything.


The Popen class of the subprocess module. This is intended as a replacement for os.popen but has the downside of being slightly more complicated by virtue of being so comprehensive. For example, you'd say
print Popen("echo Hello World", stdout=PIPE, shell=True)
instead of
print os.popen("echo Hello World").read()
but it is nice to have all of the options there in one unified class instead of 4 different popen functions.


The call function from the subprocess module. This is basically just like the Popen class and takes all of the same arguments, but it simply wait until the command completes and gives you the return code. For example:
return_code = call("echo Hello World", shell=True)


The os module also has all of the fork/exec/spawn functions that you'd have in a C program, but I don't recommend using them directly.
The subprocess module should probably be what you use.

Source: [65]

Advanced Code

Executing Code from Command Line

python -c [cmd]
# -c cmd : program passed in as string (terminates option list)
python -c "import sys;exec(" << "EOF"
def test1():
  print "test1"
print "test"

Integer Return Code Class

Python3 version:

class RC(int):
  def __new__(cls, rc, stdout='', stderr=''):
    """We can't use use __init__ because int is immutable."""
    return super(RC, cls).__new__(cls, rc)
  def __init__(self, rc, stdout='', stderr=''):
    self.rc = rc  # redundant
    self.stdout = stdout
    self.stderr = stderr
  def __repr__(self):
    """Optional string output"""
    return "(%s, '%s', '%s')" % (self.rc, self.stdout, self.stderr)

Python2 version:

class RC(int):
  def __new__(cls, rc, stdout='', stderr=''):
    """We can't use use __init__ because int is immutable."""
    return super(RC, cls).__new__(cls, rc)
  def __init__(self, rc, stdout='', stderr=''):
    super(self.__class__, self).__init__(rc)
    self.rc = rc  # redundant
    self.stdout = stdout
    self.stderr = stderr
  def __repr__(self):
    """Optional string output"""
    return "(%s, '%s', '%s')" % (self, self.stdout, self.stderr)


>>> rc1 = RC(5)
>>> rc2 = RC(0, 'out', 'err')
>>> rc1
(5, '', '')
>>> rc2
(5, 'out', 'err')
>>> rc2.stdout
>>> int(rc1)
>>> 0 + rc1
>>> if rc1 <= 5:  print "yes"
>>> if rc2:  print "non-zero"

Smart Execute

class RC(int):
    def __new__(cls, rc, output=''):
        """We can't use use __init__ because int is immutable."""
        return super(RC, cls).__new__(cls, rc)
    def __init__(self, rc, output=''):
        self.rc = rc  # redundant
        self.output = output
    def __repr__(self):
        """Optional string output"""
        return "(%s, '%s')" % (self.rc, self.output)

def smart_execute(cmd, display=True, checked=False):
    output = ""
    process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=1, shell=True, encoding='utf-8')
    for c in iter(lambda:, ""):
        if display:
        output += c
    rc = process.wait()
    if checked and rc:
        raise subprocess.CalledProcessError(ret, cmd)
    return RC(rc, output.strip())

Not smart version:

def execute(cmd, return_output=False):
    out = ""
    process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=1, shell=True, encoding='utf-8')
    for c in iter(lambda:, ""):
        if return_output:
            out += c
    ret = process.wait()
    if ret:
        raise subprocess.CalledProcessError(ret, cmd)
    if return_output:
        return out.strip()


usage:, wlist, xlist[, timeout]) [66]

  • rlist: wait until ready for reading
  • wlist: wait until ready for writing
  • xlist: wait for an “exceptional condition” (see the manual page for what your system considers such a condition)
  • The optional timeout argument specifies a time-out as a floating point number in seconds.
if[p.stdout.fileno()], [], [], 1)[0]:

ANSI Color


unicode - Print in terminal with colors using Python? - Stack Overflow -

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'

print bcolors.WARNING + "Warning: No active frommets remain. Continue?" + bcolors.ENDC
class colors:
  black = 0
  red = 1
  green = 2
  yellow = 3
  blue = 4
  magenta = 5
  cyan = 6
  white = 7
  fg = 30  # foreground add
  bg = 40  # background add
  bfg = 90  # bold foreground add
  bbg = 100  # bold background add

print '\033[%dm red foreground \033[0m' % ( + colors.fg)
print '\033[%dm bold red foreground \033[0m' % ( + colors.bfg)
print '\033[%dm blue background \033[0m' % ( +
print '\033[%dm bold blue background \033[0m' % ( + colors.bbg)


Before you start spouting escape sequences, you should check that stdout is a tty. You can do this with sys.stdout.isatty()

def hilite(string, status, bold):
    attr = []
    if status:
        # green
        # red
    if bold:
    return '\x1b[%sm%s\x1b[0m' % (';'.join(attr), string)

Python | change text color in shell - Stack Overflow -


For reference, a small bash script which prints a table of escape sequences: (The 256 color mode of xterm, The 90s called; they want their 256 colors back)


# prints a color table of 8bg * 8fg * 2 states (regular/bold)
echo Table for 16-color terminal escape sequences.
echo Replace ESC with \\033 in bash.
echo "Background | Foreground colors"
echo "---------------------------------------------------------------------"
for((bg=40;bg<=47;bg++)); do
	for((bold=0;bold<=1;bold++)) do
		echo -en "\033[0m"" ESC[${bg}m   | "
		for((fg=30;fg<=37;fg++)); do
			if [ $bold == "0" ]; then
				echo -en "\033[${bg}m\033[${fg}m [${fg}m  "
				echo -en "\033[${bg}m\033[1;${fg}m [1;${fg}m"
		echo -e "\033[0m"
	echo "--------------------------------------------------------------------- "


Python version: (modified by me)

#!/usr/bin/env python

import sys

# prints a color table of 8bg * 8fg * 2 states (regular/bold)
print "Table for 16-color terminal escape sequences."
print "Replace ESC with \\033 in bash."
print "Background | Foreground colors"
print "---------------------------------------------------------------------"
for bg in range(40, 47+1):
  for bold in range(0, 1+1):
                sys.stdout.write("\033[0m"" ESC[{bg}m   | ".format(bg=bg))
                for fg in range(30, 37+1):
                        if bold == 0:
                                sys.stdout.write("\033[{bg}m\033[{fg}m [{fg}m  ".format(bg=bg, fg=fg))
                                sys.stdout.write("\033[{bg}m\033[1;{fg}m [1;{fg}m".format(bg=bg, fg=fg))
  print "--------------------------------------------------------------------- "


Execute External Application

Execute program: (and send output to STDOUT)

encoded_wait_code = os.system('mkdir mydir')  # output sent to STDOUT

Get output and ugly return code: (STDERR goes to STDOUT)

p = os.popen(cmd)
print p.readlines()
encoded_wait_code = p.close()

Execute other python script

import sys
sys.argv = [, 'my', 'args', 'here']
execfile('')  # does not support arguments directly


Program Layout

    test/  # some include in package_name

Purpose: Makes the folder into a package library

NOTE: This file is most commonly blank

Good place to put your package functions.

# ken/
def load():
import ken

Can import other modules to then use as package modules:

# ken/
from ken.client import Server
import ken

Good place to put your Exceptions.

class MyException(Exception):
  """ My Error Exception """

Use the __all__ to specify what gets imported on 'from package import *'

 __all__ = ['func1', 'func2']


VERSION = (0, 2, 1)


__version__ = '0.2.1'

Also See #pydoc


__version__ = '1.10'
__author__ = 'Joshua Roesslein'
__license__ = 'MIT'

See #Distributing Python Modules with Distutils

Distributing Python Modules with Distutils

Distributing Python Modules —

Uses for package installation.

python install

Simple example

To install

from distutils.core import setup


klib example


from distutils.core import setup

setup(name = "klib",
    version = "101",
    description = "klib library",
    url = "",
    author = "Kenneth",
    author_email = "",
    license = "Kenneth License",
    long_description = """Really long text here.""",
    platforms = "Any Platform",

    packages = ['klib','klib.messenger'],

Installed to:


Metadata: (/opt/python26/lib/python2.6/site-packages/klib-101-py2.6.egg-info)

Metadata-Version: 1.0
Name: klib
Version: 101
Summary: klib library
Author: Kenneth
License: Kenneth License
Description: Really long text here.
Platform: Any platform

Any fields not included will be marked:

Platform: UNKNOWN


Uninstall Package

Collect files during install:

python install --record install-record.txt
sudo rm $(cat install-record.txt)


Distributing Python Modules with Setuptools

#!/usr/bin/env python
import setuptools

      description="Twitter library for python",
      author="Joshua Roesslein",
      packages = setuptools.find_packages(),
      keywords= "twitter library",
      zip_safe = True)


Decorators wrap functions

Function Counter: [67]

def fcount(func):
    def wrapper(*args, **kw):
        wrapper.count = wrapper.count + 1
        return func(*args, **kw)
    wrapper.count = 0
    return wrapper

def f(n):
    return n*n

print "f.count = ", f.count
print "f(10) = ", f(10)
print "f.count = ", f.count
print "f(3) = ", f(3)
print "f.count = ", f.count
print "f(12) = ", f(12)
print "f.count = ", f.count
print "f(15) = ", f(15)
print "f.count = ", f.count

# Output:
# f.count =  0
# f(10) =  100
# f.count =  1
# f(3) =  9
# f.count =  2
# f(12) =  144
# f.count =  3
# f(15) =  225
# f.count =  4

Great reference: Understanding Python decorators - Stack Overflow -

Example 1:

def func(arg1, arg2, ...): pass
# equivalent to:
def func(arg1, arg2, ...): pass
func = dec1(func)

Example 2:

def func(arg1, arg2, ...):
# equivalent to:
def func(arg1, arg2, ...):
func = dec2(dec1(func))

Example 3:

@decomaker(argA, argB, ...)
def func(arg1, arg2, ...):
# equivalent to:
func = decomaker(argA, argB, ...)(func)
# example:
def bar(low,high):

Example 4: (overrides and ignores passed in function - bad, unless desired)

def spamrun(fn):
    def sayspam(*args):
        print "spam, spam, spam"
    return sayspam

def useful(a, b):
    print a**2 + b**2

Example 5: (Decorator that modifies behavior of undecorated func)

def addspam(fn):
    def new(*args):
        print "spam, spam, spam"
        return fn(*args)
    return new

def useful(a, b):
    print a**2 + b**2

My Example 1: (print args and continue to function)

def showargs(fn):
    def doshowargs(*args, **kw):
        print "Args for:", fn.__name__, args, kw
        return fn(*args, **kw)
    return doshowargs

def useful(a, b):
    print a**2 + b**2

Another Example: [68]

def makebold(fn):
    def wrapped(*args, **kw):
        return "<b>" + fn(*args, **kw) + "</b>"
    return wrapped

def makeitalic(fn):
    def wrapped(*args, **kw):
        return "<i>" + fn(*args, **kw) + "</i>"
    return wrapped

def hello(msg):
    return msg

print hello("hello world")
# output: <b><i>hello world</i></b>



Utah Python July 2013 Meeting - Decorator Examples: [69]

Example of uses:

  • Count function calls
  • Add additional output (debugging)
  • Timing code
  • Thread synchronization (locks)
  • Type checking
## Simple wrapping call
def shout(f):
    def wrapper(*args):
        print('This is before {name}'.format(name=f.__name__))
        ret = f(*args)
        print('This is after {name}'.format(name=f.__name__))
        return ret
    wrapper.__name__ = f.__name__
    return wrapper

def echo(text):

echo('this is a test')
## Counting function calls
def count(f):
    def wrapper(*args):
        wrapper.counter += 1
        print("Function called {count} times".format(count=wrapper.counter))
        return f(*args)
    wrapper.counter = 0
    # change wrapper name, or it will just be 'wrapper' (for inspection)
    wrapper.__name__ = f.__name__
    return wrapper

def donothing():

print("Function called a total of {count} times".format(count=donothing.counter))
## Timing your code
import time

def timer(f):
    def wrapper(*arg):
        t = time.time()
        ret = f(*arg)
        print("Took {elapsedtime} seconds".format(elapsedtime=time.time()-t))
        return ret
    return wrapper

def fact(n):
    prod = 1
    while n > 0:
        prod *= n
        n -= 1
    return prod

total = fact(10000)
## Synchronization
import threading

def synchronized(lock):
    def wrap(f):
        def wrapper(*args, **kwargs):
                return f(*args, **kwargs)
        return wrapper
    return wrap

mylock = threading.Lock()

def myfunc():
## Type checking
def myMethod(ID, name):
    ''' Old way '''
    if not (isinstance(ID, 'uint') and isinstance(name, 'utf8string')):
        raise Exception()

def accepts(*typeargs):
    ''' Decorator way '''
    def wrap(f):
        def wrapper(*args):
            for a, t in zip(args, typeargs):
                if not isinstance(a, t):
                    raise Exception('{a} not of type {t}'.format(a=a, t=t))
            return f(*args)
        return wrapper
    return wrap

def add1(n):
    return n + 1
## Memoize
def memoize(f):
    def wrapper(*args):
        if not wrapper.ret:
            wrapper.ret = f(*args)
        return wrapper.ret
    wrapper.ret = False
    wrapper.__name__ = f.__name__
    return wrapper

import time

def sumto10():
    count = 0
    sum = 0
    while count < 10:
        sum += count
        count += 1
    return sum

# first time will count slow
print sumto10()
# second time will return cached value instantly
print sumto10()

Memoize: "Caches a function's return value each time it is called. If called later with the same arguments, the cached value is returned (not reevaluated)" [70]

## Nested decorators
import time
def test1():
  print "calculating"
  return 100

Order is important! For tested decorators the execution order is from the top to the bottom. If you reverse @shout and @memoize, the @shout will not run because of the way @memoize was written.

## registry
registry = []
# can also decorate decorators:
def registry(f):
    return f

def test2():

Decorator is just a function that returns a function.

Can use functools to automatically update decorator wrappers with __name__, etc [71]

## Auto syntax sygar the wrapper for you
import functools
def my_decorator(f):
    def wrapper(*args, **kwds):
        print 'Calling decorated function'
        return f(*args, **kwds)
    return wrapper

Class Method Decorators

Static methods: [72]

Similar to C++ and Java static methods
class C:
  def say(msg):


Class method: [73]

A class method receives the class as implicit first argument, just like an instance method receives the instance.
Class methods are different than C++ or Java static methods. If you want those, see staticmethod() in this section.
    def setup_parser(cls, parser):

See also #Class Properties

Class Properties

Properties: (not using decorators)

class C(object):
    def __init__(self):
        self._x = None
    def getx(self):
        print("get x")
        return self._x
    def setx(self, value):
        print("set x")
        self._x = value
    def delx(self):
        print("del x")
        del self._x
    x = property(getx, setx, delx, "I'm the 'x' property.")
c = C()
c.x = 5

Getter and Setter Property Decorators: [74]

class C(object):    # make sure to inherit from object for these properties to work!
    def __init__(self):
        self._x = None
    def x(self):
        """I'm the 'x' property."""
        print("get x")
        return self._x
    @x.setter   # python >= 2.6
    def x(self, value):
        print("set x")
        self._x = value
    @x.deleter  # python >= 2.6
    def x(self):
        print("del x")
        del self._x
c = C()
c.x = 5

Note: If you don't add the @x.setter, the property will be read only, and will throw a "AttributeError: can't set attribute" if attempted to be set.

Using x.getter example: [75]

class Cls(object):
    def __init__(self):
        self._x = 100

    x = property()

    def x(self):
        """I'm the 'x' property."""
        return self._x

    def x(self, value):
        self._x = value

    def x(self):
        del self._x

Custom Decorators


def func(arg1, arg2, ...): pass

equivalent to:

def func(arg1, arg2, ...): pass
func = dec1(func)

Basic layout (names are arbitrary):

def my_decorator(function_name):
  # update wrapper.__doc__ and .func_name
  # or @functools.wraps(my_wrapper)
  def mywrapper(*args, **kw):
    # do something before
    result = function_name(*args, **kw)
    # do something after
    return result
  return my_wrapper


def limit(length):
  def decorator(function):
    def wrapper(*args, **kw):
      result = function(*args, **kw)
      result = result[:length]
      return result
    return wrapper
  return decorator
def echo(foo):  return foo

echo('123456')  # '123'


assert True
assert False  # exception
assert 1 == 1
assert 1 == 2, "some message"  # AssertionError: some message

Snake Case

Convert Camel Case to Snake Case: [76]

def snake_case(name):
    s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
    return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
>>> convert('CamelCase')

Camel Case

Camel Case: [77]

>>> .join(x for x in 'make IT camel CaSe'.title() if not x.isspace())

To titleize a sentence:

>>> "this is a test".title()
"This Is A Test"


Simple functions

mul = lambda a, b: a * b
# equivalent to:
def mul(a, b):
  return a * b

Sort usage:

data = [dict(number=x) for x in '036149']
data.sort(key=lambda x: float(x['number']))

Compare usage:

data = [dict(number=x) for x in '036149']
data.sort(cmp=lambda x,y: cmp(x['number'], y['number']))


lambda x: x % 3 == 0

Multiple parameter:

lambda x, y: x + y

Call another function:

lambda word: len(word)


lambda line: line.split()[2]


def transform(n):
   return lambda x: x + n

No parameters:

lambda: 1

Default parameters:

lambda x=1: x+1



Apply a function to items of a sequence:

map(str, range(3))   # ['0', '1', '2']

Note: list comprehensions can replace map and filter


Apply a function to pairs of the sequence:

import operator
reduce(operator.mul, [1,2,3,4])   # 24   - ((1*2)*3)*4)

Note: sum or for loop can replace reduce


Return a sequence of items for which function(item) is True

filter(lambda x: x >= 0, [0, -1, 3])  # [0, 3]

Note: list comprehensions can replace map and filter

Find matching items in two lists:

a = ['a','b','c']
b = ['c','d']
filter(lambda x: x in a, b)  # returns only 'c' as it is in both lists

List comprehension

Nothing more than Syntactic Sugar

5. Data Structures — Python v2.7.5 documentation -

"List comprehensions provide a concise way to create lists."
"A list comprehension consists of brackets containing an expression followed by a for clause, then zero or more for or if clauses. "

Remove newlines from each item in a list:

mylist = [x.strip() for x in mylist]


doubled = [x*2 for x in mylist]
squares = [x**2 for x in range(10)]


[ 2*x for x in range(1,10) if x >= 5 ]


[(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]

Expanding PI:

[str(round(pi, i)) for i in range(1, 6)]

Return list of words

my_list = [current_word.lower() for current_word in words]

Utah Python July 2013 Meeting: (

# Iterative
squares = []
for x in range(10):
# [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

## Functional
squares = map(lambda x: x**2, range(10))

## List comprehension
squares = [x**2 for x in range(10)]
even_squares = [x**2 for x in range(10) if x%2 == 0]

# get hash of strings in list:
import sha
strings = ['Python is cool!', 'I love python']
hashes = [sha.sha(x).digest() for x in strings]

# flatten matrix:
matrix = [[1, 2, 3], [4, 5, 6]]
[x for row in matrix for x in row]
# [1, 2, 3, 4, 5, 6]

# build matrix:
[(x,y) for x in range(10) for y in range(10)]
# matrix of not both odd and not both even
[(x,y) for x in range(10) for y in range(10) if x%2 != y%2]

# suck lines of files into list, ignoring commented lines
[line.strip().split("\t") for line in open("")
    if not line.startswith('#')]


Mass Parameter Passing

Using: *args and **kw

  • *args a tuple of remaining unnamed parameters
  • **kw a dictionary of remaining named parameters
def param_func(a, b='b', *args, **kw):
  print( [a, b, args, kw] )

WARNING: If not careful, this can result in this error: "TypeError: param_func() got multiple values for keyword argument 'myvar'". Might be better to just use:

def param_func(*args, **kw):
  print( [args, kw] )
param_func(1, 2, 3, 4, 5)  # [1, 2, (3, 4, 5), {}]
param_func(1, 2, 3, 4, 5, joe='test')  # [1, 2, (3, 4, 5), {'joe': 'test'}]
mylist = ("a", "c", "g", "h", 5)
param_func(mylist)  # [('a', 'c', 'g', 'h', 5), 'b', (), {}]
param_func(*mylist)  # ['a', 'c', ('g', 'h', 5), {}]  *tricky*
Note: *mydict will strip out just keys and treat like list
Note: **mydict will treat like named parameters
def mywrapper(*args, **kw):
  # do something before
  result = function_name(*args, **kw)

Passing arbitrary parameters:

def fun2(**kw):
  print kw  # {'a': '1', 'b': '2', 'c': '3'}

def fun1(c='3', **kw):
 print kw  # {'a': '1', 'b': '2'}
 fun2(c='3', **kw)

fun1(a='1', b='2')

Iteration Protocol

PEP 234 -- Iterators

Making a class iterable:

class mycount(object):
  def __init__(self, max):
    self.x = 0
    self.max = max
  def __iter__(self):  # return object that implements __next__() or next()
    return self
  def next(self):
    if self.x < self.max:
      self.x += 1
      return self.x
      self.x = 0
      raise StopIteration()
for i in mycount(10): print i     # 1 ... 10
print [x for x in mycount(10) ]   # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
c = mycount(10)
for i in c: print i    # 1 ... 10
[ x for x in c ]       # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


Generators are functions that return to their previous state (using 'yield') when iterated over, which can be used in for loops.


Improve on Iterators:

  • Infinite data (infinite iterators)
  • Memory-intensive iteration
  • Cooperative Multi-threading

Generators are functions that return to their previous state when iterated over, which can be used in for loops.

Note: Generators throw StopIteration when completed

Note: the 'yield' keyword will stash away the state of the function and return the value. (Continuations)

def gen_range(end):
  cur = 0
  while cur < end:
    yield cur
    # return here next time
    cur += 1

# for loop iteration
for i in gen_range(10):  print i

# manual iteration
my_gen = gen_range(10)
  while True:
except StopIteration:

# list comprehension
numbers = [x for x in gen_range(10)]

Note: one can place an empty 'return' in a generator if needed. A 'return' with argument is invalid.

Making a class generate:

class mycount(object):
  def __init__(self, max):
    self.max = max
  def __iter__(self):
    return      # change here
  def next(self):           # change here
    x = 0
    while x < self.max:
      x += 1
      yield x

for i in mycount(10): print i     # 1 ... 10
print [x for x in mycount(10) ]   # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

c = mycount(10)
for i in c: print i    # 1 ... 10
[ x for x in c ]       # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

itr = mycount(10).next()  # 1  # 2  - eventually throws StopIteration exception

Utah Python July 2013 Meeting: [79] [80]

* Iterators are used to traverse a list
* Generators are more powerful Iterators

# simple 'n' number in functional form
def firstn(n):
    num = 0
    nums = []
    while num < n:
        num += 1
    return nums

# simple 'n' number in generator form
def firstn(n):
    num = 0
    while num < n:
        yield num
        num += 1


# Infinite list generator
def counter():
    num = 0
    while True:
        yield num
        num += 1

# Fibbonacci generator
def fib():
    num1 = 0
    num2 = 1
    yield num1
    while True:
        num1, num2 = num2, num1 + num2
        yield num1

# Fibbonacci generator 2
def fib(n):
    num1 = 0
    num2 = 1
    count = 0
    yield num1
    while count < n:
        count += 1
        num1, num2 = num2, num1 + num2
        yield num1

Utah Python July 2013 Meeting: [81] [82]

## Cooperative Multi-Threading:
# Cooperative multithreading relies on the threads themselves to relinquish control once they are at a stopping point.

def mythread(name):
    print('Starting Thread: {name}...'.format(name=name))
    counter = 10
    while counter > 0:
        print('Thread: {name} doing some work!'.format(name=name))
        counter -= 1

thread1 = mythread('Alice')
thread2 = mythread('Bob')
thread3 = mythread('Trudy')

    while True:
except StopIteration:


dunder methods

Some dunder (double under) methods determine what will happen when + (__add__) or other similar numerical symbols used.

class str_add(object):
    def __init__(self, name): = name
    def __add__(self, other): +=
        return self
        # note: only need to return self if you want to go beyond 'a + b'

a = str_add('a_')
b = str_add('b_')
c = str_add('c_')
d = a + b + c
print(  # a_b_c_
print(  # b_
print(  # c_
print(  # a_b_c_

Other common methods to override:

  • __add__ for +
  • __sub__ for -
  • __mul__ for *
  • __div__ for /
  • __mod__ for %



#!/usr/bin/env python
This is the "example" module.

>>> double(5)

def double(n):
  This is double function

  >>> double(3)
  return n * 2

if __name__ == "__main__":
    import doctest



Here is a short script to test three functions from the random module:

import random
import unittest

class TestSequenceFunctions(unittest.TestCase):

    def setUp(self):
        self.seq = range(10)

    def test_shuffle(self):
        # make sure the shuffled sequence does not lose any elements
        self.assertEqual(self.seq, range(10))

        # should raise an exception for an immutable sequence
        self.assertRaises(TypeError, random.shuffle, (1,2,3))

    def test_choice(self):
        element = random.choice(self.seq)
        self.assertTrue(element in self.seq)

    def test_sample(self):
        with self.assertRaises(ValueError):
            random.sample(self.seq, 20)
        for element in random.sample(self.seq, 5):
            self.assertTrue(element in self.seq)

if __name__ == '__main__':

The unittest module can be used from the command line to run tests from modules, classes or even individual test methods:

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
python -m unittest -v test_module  # verbose
python -m unittest -h   # help


Internet Access

# Python 3
from urllib.request import urlopen
for line in urlopen(''):
  line = line.decode('utf-8')
  if 'EST' in line or 'EDT' in line:  # look for eastern time

Simple SMTP

Requires local STMP server.

import smtplib
server = smtplib.SMTP("localhost")
server.sendmail('', '',
Subject: This is fun

Beware of the dog.
s = smtplib.SMTP(smtp_server)

with TLS:

s = smtplib.SMTP("",587)


s = smtplib.SMTP_SSL('', 465)

Data Compress

zlib, gzip, bz2, zipfile, tarfile

import zlib
s = b'hello world'  # only in python >3
t = zlib.compress(s)


import gzip
content = "Lots of content here"
f ='file.txt.gz', 'wb')
import gzip
f_in = open('file.txt', 'rb')
f_out ='file.txt.gz', 'wb')

Performance Timer

Time a task repeated 10 times:

from timeit import Timer
Timer('t=a; a=b; b=t', 'a=1; b=2').timeit()
Timer('print("hi")').timeit(10)  # repeat 10 times and time

Simple method:

start_time = int(time.time())
... task ...
compute_time = int(time.time()) - start_time

Python Version Check


# Check Python Version:
import sys
assert sys.version_info >= (2, 6, 0), 'requires Python 2.6 or higher'
del sys

resulting error if not > 2.6:

>>> import nucleon
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "nucleon/", line 9, in ?
    assert sys.version_info >= (2, 6, 0), 'nucleon requires Python 2.6 or higher'
AssertionError: nucleon requires Python 2.6 or higher

Other solutions:

Note: this will throw syntax error on 2.4 due to ternary

import sys
if sys.version_info < (2, 4):
    raise "must use python 2.5 or greater"
    # syntax error in 2.4, ok in 2.5
    x = 1 if True else 2
    print x

maybe solution:

  eval("1 if True else 2")
except SyntaxError:
  # doesn't have ternary
>>> try:
...     from my_module import twoPointSixCode
... except Exception: 
...     print "can't import, probably because your python is too old!"
import sys
if sys.version_info<(2,6,0):
  sys.stderr.write("You need python 2.6 or later to run this script\n")
>>> import sys
>>> sys.hexversion  # hex printed as integer
>>> '%x' % sys.hexversion  # output as hex, not zero fill  2.4.3
>>> sys.hexversion < 0x02060000  # test if version less than 2.6
>>> print "0x%08x" % sys.hexversion  # print as hex and zero fill and prepend '0x'
import sys
if sys.hexversion < 0x02060500:
    print >> sys.stderr, "This script requires python 2.6.5 or higher."
>>> import sys
>>> sys.version_info
(2, 6, 4, 'final', 0)
>>> if not sys.version_info[:2] == (2, 6):
...    print "Error, I need python 2.6"
... else:
...    from my_module import twoPointSixCode


Struct - Binary Data Record Layout

Format Characters:

Format	C Type		Python type 	Standard size
x	pad byte	no value	  	 
c	char		str of len 1	1	 
b	signed char	integer		1
B	unsigned char	integer 	1
?	_Bool		bool 		1
h	short		integer 	2
H	unsigned short 	integer 	2
i	int		integer 	4
I	unsigned int 	integer 	4
l	long		integer 	4
L	unsigned long	integer		4
q	long long	integer		8
Q	unsigned long long    integer	8
f	float		float		4
d	double		float		8
s	char[]		string
p	char[]		string
P	void *		integer
H  # two byte unassigned number
I  # four byte unassigned number
<  # standard size and in little-endian byte order
import struct
data = struct.pack('<IIIHH', 1, 2, 3, 4, 5)
# data = '\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04\x00\x05\x00'

fields = struct.unpack('<IIIHH', data)
# fields = (1L, 2L, 3L, 4, 5)
# using splice: fields = struct.unpack('<IIIHH', data[start:start+16])

f1, f2, f3, f4, f5 = fields

hex(f1)  # hex a field:
#  '0x1L'
>>> struct.pack("<5sh", "helloo", 11)
>>> struct.unpack("<5sh", 'hello\x0b\x00')
('hello', 11)

pdb interactive debugger

# uncomment the following to use the interactive debugger:
# import pdb; pdb.set_trace()



class telnetlib.Telnet([host[, port[, timeout]]])

Note: The instance is initially not connected by default; the open() method must be used to establish a connection. Alternatively, the host name and optional port number can be passed to the constructor, to, in which case the connection to the server will be established before the constructor returns.

open if not initially done so:[, port[, timeout]])


Telnet.read_until(expected[, timeout])   # Read until a given string, expected, is encountered or until timeout seconds have passed.
                                         # on timeout return whatever is in queue, possibly empty string
Telnet.close()         # close connection
Telnet.read_all()      # Read all data until EOF; block until connection closed.
Telnet.write(buffer)   # Write a string to the socket, doubling any IAC characters. This can block if the connection is blocked.
Telnet.expect(list[, timeout])  # Read until one from a list of a regular expressions matches.
                       # Return a tuple of three items: the index in the list of matches; the match object; and the text read to match.

Read until end of line:

line = tn.read_until("\n")
import getpass
import sys
import telnetlib

HOST = "localhost"
user = raw_input("Enter your remote account: ")
password = getpass.getpass()

tn = telnetlib.Telnet(HOST)

tn.read_until("login: ")
tn.write(user + "\n")
if password:
    tn.read_until("Password: ")
    tn.write(password + "\n")


print tn.read_all()
#!/usr/bin/env python

import sys
import telnetlib

HOST = ""
USER = "admin"
PASS = "@lt12345"

tn = telnetlib.Telnet(HOST)

tn.read_until("login: ")
tn.write(USER + "\n")
tn.read_until("Password: ")
tn.write(PASS + "\n")
tn.read_until("# ")

tn.write("terminal length 0\n")
tn.read_until("# ")

tn.write("show running-config\n")
configlines = tn.read_until("# ")




Cheap method:

os.system('ssh user@server command')

SecureShell - PythonInfo Wiki -

There are several ways to use Secure Shell (SSH) in Python



tar xzf pexpect-2.3.tar.gz
cd pexpect-2.3
sudo python ./ install


SFTP in Python? (platform independent) - Stack Overflow -

paramiko: ssh2 protocol for python -

pip install paramiko
mkdir -p ~/.src ; cd ~/.src
cd paramiko-
python install

SFTP Methods:

  • chdir
  • chmod
  • chown
  • close
  • file
  • from_transport
  • get
  • get_channel
  • getcwd
  • listdir
  • listdir_attr
  • logger
  • lstat
  • mkdir
  • normalize
  • open
  • put
  • readlink
  • remove
  • rename
  • request_number
  • rmdir
  • sock
  • stat
  • symlink
  • truncate
  • ultra_debug
  • unlink
  • utime
import paramiko

host = ""                    #hard-coded
port = 22
transport = paramiko.Transport((host, port))

password = "THEPASSWORD"                #hard-coded
username = "THEUSERNAME"                #hard-coded
transport.connect(username = username, password = password)

sftp = paramiko.SFTPClient.from_transport(transport)

import sys
path = './THETARGETDIRECTORY/' + sys.argv[1]    #hard-coded
localpath = sys.argv[1]
sftp.put(localpath, path)

print 'Upload done.'

VMware Example:

#!/usr/bin/env python

import paramiko
import sys

host = ""
port = 443
transport = paramiko.Transport((host, port))

username = "xxxx"
password = "xxx"
transport.connect(username = username, password = password)

sftp = paramiko.SFTPClient.from_transport(transport)

print dir(sftp)

print sftp.chdir("build-uploader")
print sftp.listdir()



See Python/paramiko

Get Password

Prompt for password:

getpass.getpass(prompt='Password: ')
import getpass
user = raw_input("Enter your remote account: ")
password = getpass.getpass()

ini files

ini_filename = 'my.ini'

import ConfigParser
parser = ConfigParser.SafeConfigParser()

sections = parser.sections() # get sections
for section in sections:
  print "[" + section + "]"
  for name, value in parser.items(section):
    print name + " = " + value
if parser.has_option(section, option):
  print( parser.get(section, option) )

To have the option names not convert to lower case:

parser.optionxform = str

Ignore section: [85]

ini_str = '[root]\n' + open(ini_path, 'r').read()
ini_fp = StringIO.StringIO(ini_str)
config = ConfigParser.RawConfigParser()

Get option:

config.has_option('root', 'some_key')
config.get('root', 'some_key')
config.items('root')  # list of Key Value tuples

optparse - Option Parser

[ - Option Parser

Note: Deprecated since version 2.7: The optparse module is deprecated and will not be developed further; development will continue with the argparse module.

import optparse
parser = optparse.OptionParser()

parser.add_option('--name', default=None)

    '-l', '--loglevel',
    help='Log Level (default: info)',
    default="info", action='store', type='string')

(options, args) = parser.parse_args()

# if no parameters passed show help
if len(sys.argv) == 1:

# if a stand alone parameter passed, override option
if args: = args[0]

# show option:

Note: Order is important when listing add_options!

Pretty Description:

parser = optparse.OptionParser(description="Purpose: to do something")
# or
parser.description = "Purpose: to do something"

Parse arguments:

(options, args) = parser.parse_args()

Note: If you like, you can pass a custom argument list to parse_args(), but that’s rarely necessary: by default it uses sys.argv[1:].

Change Usage Message:

usage = "%prog [options]"  # default message
# Usage: <prog> [options]"  # where <prog> is replaced with the script name

parser = OptionParser(usage=usage)
# or
parser.usage = usage  # default message
# or

Remove option: (does not remove default value from 'options' variable!!! - stupid!)


Modify an option: (changes are saved to parser automatically, unless in an option group)

option = parser.get_option('--loglevel') = 'something else'
option.default = 'debug'
# or
parser.add_option("--loglevel", help="something else", default="info")

Modify an option in an option group:

opt_group = parser.get_option_group("--loglevel")
opt_group.add_option("--loglevel", help="something else", default="info")

Types: "string", "int", "long", "choice", "float" and "complex" [86] Defaults:

  • Default actions is 'store'.
  • Default type is 'string'.
  • Default dest is the long name of the parameter (or short name if no long name provided)
  • Default value is None
from optparse import OptionParser
parser = OptionParser()

# string
parser.add_option("-f", "--file", dest="filename",
                  help="write report to FILE", metavar="FILE")

# boolean - action is what to do when flag found
parser.add_option("-q", "--quiet",
                  action="store_true", dest="verbose", default=False,
                  help="don't print status messages to stdout")

# integer
    "-m", "--minutes",
    help="Minutes (default: 60)",
    default=60, type='int')

(options, args) = parser.parse_args()

print "Filename: " + options.filename

fake handling of required option:

if not option.filename:
    parser.error("filename is required")

change default usage:

 parser = OptionParser(usage="Usage: %prog [args] arg1 arg2")

Really ugly way to print help usage :-(

def Usage():
    pname = sys.argv[0]
    cmd='%s -h' % (pname)

if (len(sys.argv) < 2):


#  OptionGroup(parser, title, description=None)
group = OptionGroup(parser, "Dangerous Options",
                    "Caution: use these options at your own risk.  "
                    "It is believed that some of them bite.")
group.add_option("-g", action="store_true", help="Group option.")

Hide an option:

"Help text to print for this option when listing all available options after the user supplies a help option (such as --help). If no help text is supplied, the option will be listed without help text. To hide this option, use the special value optparse.SUPPRESS_HELP."
parser.add_option("--secret", help=SUPPRESS_HELP)

Issues with Option Parser:

  • No handling for positional arguments
  • No adding help for positional arguments
  • No handling for required field


argparse -

argparse — Parser for command-line options, arguments and sub-commands


Note: New in version 2.7.

Installation on <= 2.6

# Install argparse: -
mkdir -p ~/.src ; cd ~/.src
tar -zvxf argparse-1.2.1.tar.gz
cd argparse-1.2.1
sudo python install

The argparse module makes it easy to write user-friendly command-line interfaces. The program defines what arguments it requires, and argparse will figure out how to parse those out of sys.argv. The argparse module also automatically generates help and usage messages and issues errors when users give the program invalid arguments.

import argparse

parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
                   help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
                   const=sum, default=max,
                   help='sum the integers (default: find the max)')

args = parser.parse_args()
print args.accumulate(args.integers)
import argparse

argparser = demoproject.util.argparse.ArgumentParser(
    usage='Kick off demoproject benchmarks.')
    help='What cards to run on.  Depends on Jenkins NODE_LABEL environment'
        'variable for correct mappings.')
    '--suite', metavar='SUITE',
    help='Specify the appropriate benchmark suite as defined in '
        'the benchmark ini file.  Defaults to standard_perf',
args = argparser.parse_args()

print args.card

Remove an argument: [87]

def remove_options(parser, options):
    """ remove_options(parser, ['--some-option', '--some-option2']) """
    for option in options:
        for action in parser._actions:
            if vars(action)['option_strings'][0] == option:


35.16. commands — Utilities for running commands — Python v2.7.1 documentation -

Note: Deprecated since version 2.6: The commands module has been removed in Python 3.0. Use the subprocess module instead.

The commands module contains wrapper functions for os.popen() which take a system command as a string and return any output generated by the command and, optionally, the exit status.

Get status and output:

status, output = commands.getstatusoutput(cmd)

Get output ignoring status:

output = commands.getoutput(cmd)


status = commands.getstatus(file)
>>> import commands

>>> commands.getstatusoutput('ls /bin/ls')
(0, '/bin/ls')

>>> commands.getstatusoutput('cat /bin/junk')
(256, 'cat: /bin/junk: No such file or directory')

>>> commands.getstatusoutput('/bin/junk')
(256, 'sh: /bin/junk: not found')

>>> commands.getoutput('ls /bin/ls')

>>> commands.getstatus('/bin/ls')
'-rwxr-xr-x  1 root        13352 Oct 14  1994 /bin/ls'

Windows Registry

Note: The _winreg module has been renamed to winreg in Python 3.0.

Read from registry:

from _winreg import *
key = OpenKey(HKEY_LOCAL_MACHINE, r'Software\Microsoft\Outlook Express', 0, KEY_ALL_ACCESS)
QueryValueEx(key, "InstallRoot")

Write to registry:

keyVal = r'Software\Microsoft\Internet Explorer\Main'
    key = OpenKey(HKEY_CURRENT_USER, keyVal, 0, KEY_ALL_ACCESS)
    key = CreateKey(HKEY_CURRENT_USER, keyVal)
SetValueEx(key, "Start Page", 0, REG_SZ, "")

Enumerate registry keys:

from _winreg import EnumKey, HKEY_USERS
    i = 0
    while True:
        subkey = EnumKey(HKEY_USERS, i)
        print subkey
        i += 1
except WindowsError:
    # WindowsError: [Errno 259] No more data is available    

Another enumeration example:

# File:

import _winreg

explorer = _winreg.OpenKey(

# list values owned by this registry key
    i = 0
    while 1:
        name, value, type = _winreg.EnumValue(explorer, i)
        print repr(name),
        i += 1
except WindowsError:

value, type = _winreg.QueryValueEx(explorer, "Logon User Name")

print "user is", repr(value)

Enable Remote Desktop Example: (by Kenneth)

from _winreg import *
key = OpenKey(HKEY_LOCAL_MACHINE, r'SYSTEM\CurrentControlSet\Control\Terminal Server', 0, KEY_ALL_ACCESS)
SetValueEx(key, "fDenyTSConnections", 0, REG_DWORD, 0)
ValueTypeTuple = QueryValueEx(key, "fDenyTSConnections")   # (0, 4)
print("Please reboot the system to complete the changes")





import json
json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
# '["foo", {"bar": ["baz", null, 1.0, 2]}]'

compact encoding:

import json
json.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',',':'))
# '[1,2,3,{"4":5,"6":7}]'

Pretty printing:

import json
print json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4)
# {
#     "4": 5,
#     "6": 7
# }


import json
json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
# [u'foo', {u'bar': [u'baz', None, 1.0, 2]}]

Load from file:

f = open(filename)
j = json.load(f)
print j["kind"]  # "dual-controller-adapter"
#   "kind": "dual-controller-adapter",

From Command Line:

echo '{"foo": "lorem", "bar": "ipsum"}' | python -mjson.tool
# get value
echo '{"test":1,"test2":2}' | python -c 'import sys,json;data=json.loads(; print data["test"]'
# read large file
python -c "import json;f=open('file.json');data=json.load(f);print json.dumps(data,indent=2)"
python -c "import json;print json.dumps(json.load(open('file.json')),indent=2)"

Load from web:

import httplib
import json

def get_http_json(server, path):
    """ This performs an HTTP GET against a server and path and returns the
        resulting page data.

        server The server we're hitting, e.g.
        path   The path we're hitting, e.g. api/part/MA12345/
    connection = httplib.HTTPConnection(server)
        url = 'http://%s/%s' % (server, path)
        print 'Querying url %s' % url
        connection.request('GET', url)
        response = connection.getresponse()
        if response.status == httplib.OK:
            return json.loads(
            print 'Unable to get %s. %s %s' % (url, response.reason,
            return None

j = get_http_json(server, path)

Pretty printing from the command line:

echo '{"json":"obj"}' | python -mjson.tool
#   "json": "obj"



See Python/Sockets


import base64


encoded = base64.b64encode('data to be encoded')




Creating a daemon the Python way « Python recipes « ActiveState Code -

import os
import sys

    pid = os.fork()
except OSError, e:
    raise Exception, "%s [%d]" % (e.strerror, e.errno)

if (pid != 0):  # parent

# create new process session

# redirect stdin, stdout, stderr'/dev/null', os.O_RDWR)
os.dup2(0, 1)
os.dup2(0, 2)

#... tasks ...

My Solution:

import os
import sys
import signal
import syslog

HUP = False

def log(msg):

def kill_signal_handler(signal, frame):
    log("Being killed off: {0} {1}".format(signal, frame))
    log("Shutting Down!")
signal.signal(signal.SIGTERM, kill_signal_handler)
signal.signal(signal.SIGALRM, kill_signal_handler)

def hup_signal_handler(signal, frame):
    global HUP
    log("Signal reload received: {0} {1}".format(signal, frame))
signal.signal(signal.SIGHUP, hup_signal_handler)

def main_loop():
    global HUP

    while True:
        #  ... handle HUP=True ...

        # ... main code ...

        # wait 60 seconds before we start the next loop
        time.sleep(60)  # signals will interrupt this

if __name__ == "__main__":
    log("Starting Daemon...")
    child_id = os.fork()
    if child_id:
    # redirect stdin, stdout, stderr'/dev/null', os.O_RDWR)
    os.dup2(0, 1)
    os.dup2(0, 2)

    # write pid
    with open("", "w") as f:

    while True:
        except Exception as e:
            log("Critical Failure Caught: %s" % str(e))
            log("Critical Failure - Restarting!")
            #raise e
ln -s /opt/pyton27/bin/python pyton-water
killall python-water


import os
import sys
import time

child_id = os.fork()
if child_id:
  print "parent exiting"

while True:
    print "child"

For windows, to hide the console, see #Hide Console


13.1. csv — CSV File Reading and Writing — Python v2.7.2 documentation -


import csv
spamReader = csv.reader(open('eggs.csv', 'rb'), delimiter=' ', quotechar='|')
for row in spamReader:
  print ', '.join(row)


import csv
spamWriter = csv.writer(open('eggs.csv', 'wb'), delimiter=' ',
    quotechar='|', quoting=csv.QUOTE_MINIMAL)
spamWriter.writerow(['Spam'] * 5 + ['Baked Beans'])
spamWriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])


11.13. sqlite3 — DB-API 2.0 interface for SQLite databases — Python v2.7.2 documentation -

sqlite3 - New in version 2.5.

import sqlite3

conn = sqlite3.connect('/tmp/example')

c = conn.cursor()

# Create table
c.execute('''create table stocks
(date text, trans text, symbol text,
 qty real, price real)''')

# Insert a row of data
c.execute("""insert into stocks
          values ('2006-01-05','BUY','RHAT',100,35.14)""")

# Show affected row count
print c.rowcount

# Save (commit) the changes

# We can also close the cursor if we are done with it

parameter substitution:

# Never do this -- insecure!
symbol = 'IBM'
c.execute("... where symbol = '%s'" % symbol)

# Do this instead
t = (symbol,)
c.execute('select * from stocks where symbol=?', t)
c.execute('select * from stocks where symbol=?', (symbol,))

# Larger example
for t in [('2006-03-28', 'BUY', 'IBM', 1000, 45.00),
          ('2006-04-05', 'BUY', 'MSOFT', 1000, 72.00),
          ('2006-04-06', 'SELL', 'IBM', 500, 53.00),
    c.execute('insert into stocks values (?,?,?,?,?)', t)

select results:

c = conn.cursor()
c.execute('select * from stocks order by price')

for row in c:
  print row  # row is a tuple

# OR (but not both)

rows = c.fetchall()    # rows is list of tuples
print len(rows)
for row in rows:
  print row

Show which version of sqlite3: [88]

# -*- coding: utf-8 -*-

import sqlite3 as lite
import sys

con = None

    con = lite.connect('test.db')

    cur = con.cursor()
    cur.execute('SELECT SQLITE_VERSION()')

    data = cur.fetchone()

    print "SQLite version: %s" % data

except lite.Error, e:

    print "Error %s:" % e.args[0]


    if con:

Or simply: [89]

# echo -e "import sqlite3\nprint sqlite3.sqlite_version_info" | python
(3, 3, 6)

MySQL Database

Ubuntu packages:

  • python-mysqldb - Python interface to MySQL
  • python-mysql.connector - pure Python implementation of MySQL Client/Server protocol
apt-get install python-mysqldb
>>> import _mysql
apt-get install python-mysql.connector


pip install MySQL-python
pip install mysqlclient


_mysql -

apt-get install python-mysqldb
import _mysql


db.query("""SELECT spam, eggs, sausage FROM breakfast
         WHERE price < 5""")

# ...or...

row = r.fetch_row()


Getting started with couchdb-python — couchdb-python v0.8 documentation -

couch = couchdb.Server('')

# database
db = couch.create('test') # create new
db = couch['mydb'] # or get existing

# print databases:
for db in couch:
  print db

doc = {'foo': 'bar'}
doc_id, doc_rev =
# (u'9278f7691c3421227a9f8436540008f9', u'1-4c6114c65e295552ab1019e2b046b10e')
# note: this also updates doc with _rev and _id
doc = db['9278f7691c3421227a9f8436540008f9']

# better to create a document using an id you want, instead of the randomly generated one
db[] = doc  # this will write immediately to the database

# print all docs:
for id in db:
  print id

# delete doc

# delete database

Search database:

# show all records
map_fun = '''function(doc) { emit(, null); }'''

# row = <Row id=u'12341234123412341234', key=None, value=None>
# emit(key, value) - returns the values for key and value

map_fun = '''
function(doc) {
  if (doc.username == 'virt-01')
     emit(doc.username, null);

# row = <Row id=u'12341234123412341234', key=None, value=None>
for row in db.query(map_fun):

for row in db.query(map_fun, descending=True):

couchdb-python - CouchDB Python Library - Google Project Hosting -


mkdir -p ~/.src ; cd ~/.src
tar -zvxf CouchDB-0.8.tar.gz
cd CouchDB-0.8
python install


echo "import couchdb" | python


nose: nose extends unittest to make testing easier

nose : Python Package Index -


mkdir -p ~/.src ; cd ~/.src
tar -zvxf nose-1.1.2.tar.gz
cd nose-1.1.2
sudo python install


mkdir skeleton
cd skeleton
mkdir bin NAME tests docs
touch NAME/
touch tests/
touch tests/

nose test skeleton:

# tests/
from import *
import klib

def setup():
    print "SETUP!"

def teardown():
    print "TEAR DOWN!"

def test_basic():
    print "I RAN!"

# def test_something_else():



See Also:


25.1. pydoc — Documentation generator and online help system — Python v2.7.2 documentation -

pydoc - The pydoc module automatically generates documentation from Python modules.

pydoc - same as 'import [library], help([library])'

$ pydoc [library]

Note: lists some special 'module metadata variables' separately (e.g. version, author).

special pydoc recognized metadata sections:


pydoc function module

Help on package klib:

    klib - This is the name


    These are module doc strings
        that give helpful details


        This is a doc string
        for func1

    PI = 3.141
    __all__ = ['func1', 'PI']
    __author__ = 'Kenneth Burgener <>'
    __copyright__ = 'Copyright 2007, The Project'
    __credits__ = 'Credits go to me'
    __date__ = '26 February 2001'
    __email__ = ''
    __license__ = 'GPL'
    __maintainer__ = 'Ken Burgener'
    __status__ = 'Production'
    __version__ = '$Revision: 83492 $'


    26 February 2001

    Kenneth Burgener <>

    Credits go to me


This is the name

These are module doc strings
    that give helpful details

#__version__ = "1.0"
__version__ = "$Revision: 83492 $"
__author__ = "Kenneth Burgener <>"

__date__ = "26 February 2001"
__credits__ = "Credits go to me"

__all__ = ['func1', 'PI']

PI = 3.141

__copyright__ = 'hello license'
__license__ = "GPL"
__copyright__ = "Copyright 2007, The Project"
__maintainer__ = "Ken Burgener"
__email__ = ""
__status__ = "Production"

def func1():
    This is a doc string
    for func1

pydoc class module


Help on module class_pydoc:

    class_pydoc - This is the name


    These are module doc strings
        that give helpful details


    class Toad
     |  doc string for object
     |  Methods defined here:
     |  __init___(self, name)
     |      Constructor
     |  instance_function(self)
     |      Instance Function
     |  ----------------------------------------------------------------------
     |  Class methods defined here:
     |  class_function(cls) from __builtin__.classobj
     |      Class Function
     |  ----------------------------------------------------------------------
     |  Data and other attributes defined here:
     |  my_class_attribute = 'sample class attribute'

    PI = 3.141
    __author__ = 'Kenneth Burgener <>'
    __copyright__ = 'Copyright 2007, The Project'
    __credits__ = 'Credits go to me'
    __date__ = '26 February 2001'
    __email__ = ''
    __license__ = 'GPL'
    __maintainer__ = 'Ken Burgener'
    __status__ = 'Production'
    __version__ = '1.0'


    26 February 2001

    Kenneth Burgener <>

    Credits go to me


This is the name

These are module doc strings
    that give helpful details

__version__ = "1.0"
#__version__ = "$Revision: 83492 $"
__author__ = "Kenneth Burgener <>"

__date__ = "26 February 2001"
__credits__ = "Credits go to me"

#__all__ = ['func1', 'PI']

PI = 3.141

__copyright__ = 'hello license'
__license__ = "GPL"
__copyright__ = "Copyright 2007, The Project"
__maintainer__ = "Ken Burgener"
__email__ = ""
__status__ = "Production"

class Toad:
    """ doc string for object """

    my_class_attribute = 'sample class attribute'

    def __init___(self, name):
        """ Constructor """

    def instance_function(self):
        """ Instance Function """

    def class_function(cls):
        """ Class Function """


User ID:


User Name:

# fails it stdin piped to program

User Name:

import sys
import pwd
def get_username():
    return pwd.getpwuid( os.getuid() )[ 0 ]




import socket
hostname = os.system('hostname')


Get Hostname

import socket

Convert hostname to ip address

>>> socket.gethostbyaddr('demo')
('', [], [''])

/usr/local/bin/whatip: (or nametoip)

#!/usr/bin/env python

# whatip - convert hostname to ip address
# author: Kenneth Burgener <> (FEB 2012)

import socket
import sys

if len(sys.argv) != 2:
  print "Convert Hostname to IP Address"
  print "Usage: %s HOSTNAME" % sys.argv[0].split('/')[-1]

hostname = sys.argv[1]
  result = socket.gethostbyaddr(hostname)
  print result[2][0]
except (socket.gaierror, socket.herror) as e:
  print "Error: Unable to resolve '%s'" % hostname

Get Public IP Address

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

networking - Finding local IP addresses using Python's stdlib - Stack Overflow -

Python Network Server

#!/usr/bin/python           # This is file

import socket               # Import socket module

s = socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345                # Reserve a port for your service.
s.bind((host, port))        # Bind to the port

s.listen(5)                 # Now wait for client connection.
while True:
   c, addr = s.accept()     # Establish connection with client.
   print 'Got connection from', addr
   c.send('Thank you for connecting')
   c.close()                # Close the connection



Python Network Client

#!/usr/bin/python           # This is file

import socket               # Import socket module

s = socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345                # Reserve a port for your service.

s.connect((host, port))
print s.recv(1024)
s.close                     # Close the socket when done



Run Library Module as Script

Run python with -m option:

-m mod : run library module as a script (terminates option list)


cat [json_file] | python -m json.tool
# same as:
cat [json_file] | python /opt/python26/lib/python2.6/json/

Good for when library is buried deep in the library directories


>>> hex(10)  # print integer as hex string
>>> sys.hexversion  # python version as integer
>>> hex(sys.hexversion)  # python version as hex
>>> print "0x%08x" % sys.hexversion  # python version as hex
>>> int('0xa', 16)  # hex string to integer
>>> int('0a', 16)  # hex string to integer
>>> int('0xa', 0)  # hex string to integer auto detected du to '0x'


Integer to Binary String with Negative Number Support: (2's complement)

# source:
def bin(x):
    if x < -32768 or x > 65535:
        raise Exception("Out of Range Error: %d" % x)
    if x < 0:
        x = 65535 + x + 1
    bin_str = ''
    bin_str += ''.join(x & (1 << i) and '1' or '0' for i in range(15, 11, -1))
    bin_str += ' '
    bin_str += ''.join(x & (1 << i) and '1' or '0' for i in range(11, 7, -1))
    bin_str += ' '
    bin_str += ''.join(x & (1 << i) and '1' or '0' for i in range(7, 3, -1))
    bin_str += ' '
    bin_str += ''.join(x & (1 << i) and '1' or '0' for i in range(3, -1, -1))
    return bin_str

Integer to Binary String:

# source:
def bin(x):
    if x < 0 or x > 65535:
        raise Exception("Out of Range Error: %d" % x)
    bin_str = ''
    bin_str = ''.join(x & (1 << i) and '1' or '0' for i in range(15, 7, -1))
    bin_str = bin_str + ' '
    bin_str = bin_str + ''.join(x & (1 << i) and '1' or '0' for i in range(7, -1, -1))
    return bin_str

print bin(0)      # '00000000 00000000'
print bin(1)      # '00000000 00000001'
print bin(255)    # '00000000 11111111'
print bin(256)    # '00000001 00000000'
print bin(43690)  # '10101010 10101010'
print bin(65535)  # '11111111 11111111'
print bin(65536)  # '00000000 00000000' - rolled over to next set!

Integer to Binary:

bin(170)  # '0b10101010'

Binary to Integer:

int('10101010', 2)  # 170


See #Binary

BitwiseOperators - Python Wiki -

The Operators:

x << y
    Returns x with the bits shifted to the left by y places (and new bits on the right-hand-side are zeros). This is the same as multiplying x by 2**y. 
x >> y
    Returns x with the bits shifted to the right by y places. This is the same as //'ing x by 2**y. 
x & y
    AND - Does a "bitwise and". Each bit of the output is 1 if the corresponding bit of x AND of y is 1, otherwise it's 0. 
x | y
    OR - Does a "bitwise or". Each bit of the output is 0 if the corresponding bit of x AND of y is 0, otherwise it's 1. 
~ x
    Returns the complement of x - the number you get by switching each 1 for a 0 and each 0 for a 1. This is the same as -x - 1. 
x ^ y
    XOR - Does a "bitwise exclusive or". Each bit of the output is the same as the corresponding bit in x if that bit in y is 0, and it's the complement of the bit in x if that bit in y is 1. 

Script Path

if __name__ == "__main__":
    # Change directory to where this script lives so we can find relative
    # paths from here regardless of where the working directory was when we
    # ran the script.
    # APP_DIR = os.path.dirname(sys.argv[0])
    APP_DIR = os.path.dirname(os.path.abspath(sys.argv[0))
    if APP_DIR:
normpath(path)  # Normalize path, eliminating double slashes, etc.
abspath(path)   # Return an absolute path

Path to a module: [90] (couldn't fine a_module)

import a_module
import os
path = os.path.dirname(a_module.__file__)

Path to module: [91]

import inspect, os
print inspect.getfile(inspect.currentframe()) # script filename (usually with path)
print os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) # script directory

Even better:

print os.path.realpath(__file__)
print os.path.dirname(__file__)
print os.path.basename(__file__)


keywords: scriptpath script path program path pathname



None is a singleton (Python only has one copy of None in the interpreter).

Make your own singleton:

override the new method like this:

class Singleton(object):
    _instance = None
    def __new__(cls, *args, **kwargs):
        if not cls._instance:
            cls._instance = super(Singleton, cls).__new__(
                                cls, *args, **kwargs)
        return cls._instance

if __name__ == '__main__':
        print "Same"
        print "Different"

Using decorator -implementation from PEP318? Implementing the singleton pattern with a decorator:

def singleton(cls):
    instances = {}
    def getinstance():
        if cls not in instances:
            instances[cls] = cls()
        return instances[cls]
    return getinstance

class MyClass:


from xml.dom.minidom import parse, parseString

dom1 = parse('c:\\temp\\mydata.xml') # parse an XML file by name

datasource = open('c:\\temp\\mydata.xml')
dom2 = parse(datasource)   # parse an open file

dom3 = parseString('<myxml>Some data<empty/> some more data</myxml>')

Pretty Print: [92]

import xml.dom.minidom

xml = xml.dom.minidom.parse(xml_fname) # or xml.dom.minidom.parseString(xml_string)
pretty_xml_as_string = xml.toprettyxml()

Pretty Print with etree [93]

import lxml.etree as etree

x = etree.parse("filename")
print etree.tostring(x, pretty_print = True)



Simply don't include self.output.startElement and self.output.endElement for elements you don't want: (my version of [94])

# this parses out the time element of a gpx for Google Earth
import os
import sys
import optparse
import fnmatch
from sys import argv
import xml.sax, xml.sax.saxutils

class my_filter_xml(xml.sax.ContentHandler):
    def __init__(self, outfile, verbose=False):
        self.output = xml.sax.saxutils.XMLGenerator(outfile, 'utf-8')
        self.indent = 0
        self.ignore = False

    def startElement(self, name, attrs):
        print " " * self.indent + "start: " + name
        if attrs.keys():
            keys = " " * (self.indent + 2) + "attrs:"
            for key in attrs.keys():
                keys = keys + " " + key + "=" + attrs[key]
            print keys
        self.indent = self.indent + 2
        #self.output.startElement(name, {})
        if name == "time":
            self.ignore = True
        #if not self.ignore:
        self.output.startElement(name, attrs)

    def characters(self, ch):
        if ch.strip():
            print " " * self.indent + "ch: " + ch.strip()
            if not self.ignore:
            if not self.ignore:

    def endElement(self, name):
        self.indent = self.indent - 2
        print " " * self.indent + "/end: " + name
        #if not self.ignore:
        if name == "time":
            self.ignore = False

if __name__ == '__main__':
    source = open("tracks.gpx")
    dest = open("tracks_out.gpx", 'w+')
    xml.sax.parse(source, my_filter_xml(dest, True))


import os
import sys
import optparse
import fnmatch
from sys import argv
import xml.sax, xml.sax.saxutils

class fix_descriptor(xml.sax.ContentHandler):
    def __init__(self, outfile, verbose=False):
        self._outfile = outfile
        self._verbose = verbose
        self.output = xml.sax.saxutils.XMLGenerator(outfile, 'utf-8')
        self._filter = [ 'payload',
                         'checksum' ]
        self._keep = [ 'vib',
                       'maintenance-mode' ]
        self._inside = None

    def startElement(self, name, attrs):
        # Certain elements are kept unchanged
        if name in self._keep:
            if self._verbose:
                print '%s element start found' % (name)
            self.output.startElement(name, {})
            if name == 'maintenance-mode':
                if self._verbose:
                    print 'inside maintenance-mode'
                self._inside = name
        # Other elements are filtered (removed)
        elif name in self._filter:
            if self._verbose:
                print '%s element start found' % (name)
            self._inside = name
        self.output.startElement(name, attrs)

    def characters(self, ch):
        if self._inside == 'maintenance-mode':
            if self._verbose:
                print 'new content for maintenance-mode'
        elif self._inside not in self._filter:

    def endElement(self, name):
        # If it's not a checksum element, only print it
        if name in self._filter + self._keep:
            self._inside = None
            if self._verbose:
                print '%s element end found' % (name)
            if name in self._filter:

if __name__ == '__main__':
    parser = optparse.OptionParser(usage='%prog ...')
    parser.add_option('-v', '--verbose',
                       action='store_true', default=False,
                       help='Print more information.')
                       type='str', default='descriptor.xml',
                       help='Name of input XML [default: %default]')
                       type='str', default='fixed_descriptor.xml',
                       help='Name of output XML [default: %default]')
    opts, args = parser.parse_args(argv[1:])
    source = open(opts.input_xml)
    dest = open(opts.output_xml, 'w+')
    xml.sax.parse(source, fix_descriptor(dest, opts.verbose))


Simple read and output parsing:

import os
import sys
import optparse
import fnmatch
from sys import argv
import xml.sax, xml.sax.saxutils

class fix_descriptor(xml.sax.ContentHandler):
    def __init__(self, outfile, verbose=False):
        self._outfile = outfile
        self._verbose = verbose
        self.output = xml.sax.saxutils.XMLGenerator(outfile, 'utf-8')
        self._inside = None

    def startElement(self, name, attrs):
        # attrs.items() is a list of tuples (name, value)
        print "start:", name, "/", attrs.items()
        self.output.startElement(name, attrs)

    def characters(self, ch):
        print "char:", self._inside, "/", ch

    def endElement(self, name):
        print "end:", name

if __name__ == '__main__':
    parser = optparse.OptionParser(usage='%prog ...')
    parser.add_option('-v', '--verbose',
                       action='store_true', default=False,
                       help='Print more information.')
                       type='str', default='descriptor.xml',
                       help='Name of input XML [default: %default]')
                       type='str', default='fixed_descriptor.xml',
                       help='Name of output XML [default: %default]')
    opts, args = parser.parse_args(argv[1:])
    source = open(opts.input_xml)
    dest = open(opts.output_xml, 'w+')
    xml.sax.parse(source, fix_descriptor(dest, opts.verbose))


Unicode string:

u'Hello World !'

Raw Unicode string:

>>> ur'Hello\u0020World !'
u'Hello World !'


u = u'hello\xe4'
str(u)  # UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 5: ordinal not in range(128)
s = u.encode('utf8')  # 's' is now type str



See Python/Zip


signal.SIGTERM  : kill  [pid]
signal.SIGALRM  : kill -ALRM  [pid]
signal.SIGHUP   : kill -HUP  [pid]
import signal, os

def handler(signum, frame):
    print 'Signal handler called with signal', signum
    raise IOError("Couldn't open device!")

# Set the signal handler and a 5-second alarm
signal.signal(signal.SIGALRM, handler)

# This open() may hang indefinitely
fd ='/dev/ttyS0', os.O_RDWR)

signal.alarm(0)          # Disable the alarm

Note: use signal.SIGTERM to handle "kill" command



#!/usr/bin/env python
import signal
import sys
def signal_handler(signal, frame):
        print 'You pressed Ctrl+C!'
signal.signal(signal.SIGINT, signal_handler)
print 'Press Ctrl+C'

# linux only:

# windows alternative:
#import time


Non-blocking raw_input for Python

Non-blocking raw_input for Python:

# source:

import signal

class AlarmException(Exception):

def alarmHandler(signum, frame):
    raise AlarmException

def nonBlockingRawInput(prompt='', timeout=20):
    signal.signal(signal.SIGALRM, alarmHandler)
        text = raw_input(prompt)
        return text
    except AlarmException:
        print '\nPrompt timeout. Continuing...'
    signal.signal(signal.SIGALRM, signal.SIG_IGN)
    return ''


import smtplib
import email.mime.text

msg_text = "Hello World"
msg = email.mime.text.MIMEText(msg_text)

# me == the sender's email address
# you == the recipient's email address
msg['Subject'] = 'The contents of %s' % textfile
msg['From'] = me
msg['To'] = you

# Send the message via our own SMTP server, but don't include the
# envelope header.
s = smtplib.SMTP('localhost')
s.sendmail(me, [you], msg.as_string())

my nagios fake /bin/mail:

#!/usr/bin/env python

import smtplib
import email.mime.text
import optparse
import os
import sys
import pwd

os.system('logger mail called')

parser = optparse.OptionParser()
    '-s', '--subject',
    '-t', '--to',
    '-f', '--from',
    '-S', '--server',
(options, args) = parser.parse_args()

def get_username():
    return pwd.getpwuid( os.getuid() )[ 0 ]

if options.send_to:
    send_to = options.send_to
    if len(args) == 1:
        send_to = args[0]

if options.send_from:
    send_from = options.send_from
    username = get_username()
    hostname = os.uname()[1]
    send_from = username + "@" + hostname

msg_text =
msg = email.mime.text.MIMEText(msg_text)

msg['Subject'] = options.subject
msg['From'] = send_from
msg['To'] = send_to

s = smtplib.SMTP(options.server)
s.sendmail(send_from, [send_to], msg.as_string())

Authenticated (with gmail):

import smtplib
import email.mime.text

# from_email = ""
# to_email = ""
from_email = "Kenneth Burgener <>"
to_emails = ["Kenneth Burgener <>", "Kenneth Cell <>"]

msg_text = "hello world"
msg = email.mime.text.MIMEText(msg_text)
msg['Subject'] = 'Test text'
msg['From'] = from_email
# msg['To'] = to_email
msg['To'] = ", ".join(to_emails)
# s = smtplib.SMTP('localhost')
print "sending..."
s = smtplib.SMTP_SSL('', '465')
s.login('xxx', 'xxx')
# s.sendmail(from_email, [to_email], msg.as_string())
s.sendmail(from_email, to_emails, msg.as_string())
print "sent!"



8.18. pprint — Data pretty printer — Python v2.7.5 documentation -

import pprint
import pprint
pp = pprint.PrettyPrinter(indent=4)

Note: Useless on objects!

Dump object:

def dumpobject(obj):
    print "<<<---"
    print type(obj), obj.__class__.__name__
    for attr in dir(obj):
        if attr[0] == "_": continue  # ignore under/dunder properties
        if str(type(getattr(obj, attr))) == "<type 'instancemethod'>":
            print attr, ": METHOD"
            print attr, ":", type(getattr(obj, attr)), ":", getattr(obj, attr)
    print "END --->>>"


Curses Programming with Python — Python v3.3.2 documentation -

"The curses library supplies a terminal-independent screen-painting and keyboard-handling facility for text-based terminals; such terminals include VT100s, the Linux console, and the simulated terminal provided by various programs. Display terminals support various control codes to perform common operations such as moving the cursor, scrolling the screen, and erasing areas. Different terminals use widely differing codes, and often have their own minor quirks.

In a world of graphical displays, one might ask “why bother”? It’s true that character-cell display terminals are an obsolete technology, but there are niches in which being able to do fancy things with them are still valuable. One niche is on small-footprint or embedded Unixes that don’t run an X server. Another is tools such as OS installers and kernel configurators that may have to run before any graphical support is available."


Thread from Utah Python User's Group mailing list:


Hello, I am trying to learn more about curses in python for a desktop application. Does anybody have any recommendations on tutorials or documentation?



We've use urwid at work. Seems to be a good approach if you aren't already tied to something. Learning curve can be a bit steep (well, compared to what you'd think it should be). Good luck!

Gabriel Gunderson


This video series looks pretty good:

Official HOWTOs from the site:

As Gabe mentioned, there are other libraries that provide similar or greater functionality, so if you aren't doing this specifically to learn curses, it's worth at least looking at a few of them to compare.

There's a brief comparison of three possibilities here:

Daniel Fackrell


Here's another option that looks promising:

Daniel Fackrell


pythondialog -

"pythondialog is a Python wrapper for the dialog utility originally written by Savio Lam, and later rewritten by Thomas E. Dickey. Its purpose is to provide an easy to use, pythonic and comprehensive Python interface to dialog. This allows one to make simple text-mode user interfaces on Unix-like systems (including Linux). "

Note: The latest versions (2.12 and later) only support Python 3; users who really want to stick to Python 2 should use version 2.11 for now.


# latest:
# wget
tar -zvxf python2-pythondialog-2.11.tar.gz
cd pythondialog-2.11
import dialog
d = dialog.Dialog()
if d.yesno("Continue?") == d.DIALOG_OK:
 print "Continuing..."


My solution

kiloforce / dialog / source / — Bitbucket -

#!/usr/bin/env python

""" Wrapper for Linux 'dialog' command """

__author__ = "Kenneth Burgener <>"
__copyright__ = "(c) 2013"
__date__ = "$Sep 27, 2013$"
__version__ = "1.0"
__credits__ = r"""
X Dialog Wrapper #

import subprocess
import time
import sys

class Dialog():
    """ Python wrapper for Linux 'dialog' command

    dialog - display dialog boxes from shell scripts

    >>> import dialog
    >>> dlg = dialog.Dialog()
    >>> rc = dlg.msgbox('hello world')

    Return codes:
        YES = 0
        OK = 0
        EXIT = 0
        NO = 1
        CANCEL = 1
        ESC = 255

    YES = 0
    OK = 0
    EXIT = 0
    NO = 1
    CANCEL = 1
    ESC = 255

    def __init__(self):
        rc = self._exe('which dialog > /dev/null')[0]
        if rc != 0:
            print "Error: failed to find Linux dialog command"

    def _exe(self, cmd, strip=True):
        p = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE)
        output = "\n".join(p.stderr.readlines())
        if strip:
            output = output.strip()
        rc = p.wait()
        return (rc, output)

    def rc_to_str(self, rc):
        if rc == 0:
            return "YES"
        elif rc == 1:
            return "NO"
        elif rc == 255:
            return "ESX"
            raise Exception("Unknown return code: %s" % rc)

    def clear(self):
        Clear screen

        >>> dialog.clear()"""
        rc, output = self._exe('clear')
        assert rc in (0,), "clear rc"
        return rc

    def msgbox(self, msg):
        Simple message box

        @param msg (str) = display message
        @return: rc (int)

        >>> rc = dialog.msgbox('hello world')"""
        rc, output = self._exe('dialog --msgbox "{msg}" 0 0'.format(msg=msg))
        assert rc in (self.YES, self.ESC), "msgbox rc"
        return rc

    def yesno(self, msg):
        Yes/No dialog

        @param msg (str) = display message
        @return: rc (int)

        >>> rc = dialog.yesno('continue')"""
        rc, output = self._exe('dialog --yesno "{msg}" 0 0'.format(msg=msg))
        assert rc in (self.YES, self.NO, self.ESC), "yesno rc"
        return rc

    def infobox(self, msg):
        Dialog that does not wait for user's response
        (do your own pause)

        @param msg (str) = display message
        @return: rc (int) always zero

        >>> for i in range(10):
        >>>   dialog.infobox('counting: ' + str(i))
        >>>   time.sleep(3)"""
        rc, output = self._exe('dialog --infobox "{msg}" 0 0'.format(msg=msg))
        assert rc in (self.YES,), "infobox rc"
        return rc

    def inputbox(self, msg):
        Get user input

        @param msg (str) = display message
        @return: rc (int), output (str/None)

        >>> rc, output = dialog.inputbox('name:')"""
        rc, output = self._exe('dialog --inputbox "{msg}" 0 0'.format(msg=msg))
        assert rc in (self.YES, self.NO, self.ESC), "inputbox rc"
        if not output:
            output = None
        return rc, output

    def textbox(self, filename):
        Display a file

        @param filename (str) = file to display
        @return: rc (int)

        >>> dialog.textbox('/etc/passwd')"""
        rc, output = self._exe('dialog --textbox {filename} 0 0'.format(filename=filename))
        assert rc in (self.YES, self.ESC), "textbox rc"
        return rc

    def menu(self, msg, item_list):
        Single selection checkbox menu

        @param msg (str) = display message
        @param item_list (str list) = list of items to display
        @return: rc (int), selection (int/None)

        >>> rc, selection ='Select multiple options:',
        ...     ('option 1', 'option 2'))"""
        item_str = ""
        for i in range(len(item_list)):
            item_str += " " + str(i+1) + " \"" + item_list[i] + "\""
        rc, output = self._exe('dialog --menu "{msg}" 0 0 0 {item_str}'.format(msg=msg, item_str=item_str))
        if output:
            selection = int(output)
            selection = None
        assert rc in (self.YES, self.NO, self.ESC), "menu rc"
        return rc, selection

    def checklist(self, msg, item_list, checked_list=None):
        Multiple selection checkbox menu

        @param msg (str) = display message
        @param item_list (str list) = list of items
        @param checked_list (int list) = default items to check
        @return: rc (int), selections (int list/None)

        >>> rc, selections = dialog.checklist('Select multiple options:',
        ...     ('option 1', 'option 2')
        item_str = ""
        assert type(item_list) in (type(None), type(()), type([])), "checklist: item_list must be a list"
        assert type(checked_list) in (type(None), type(()), type([])), "checklist: checked_list must be a list"
        for i in range(len(item_list)):
            item_str += " " + str(i+1) + " \"" + item_list[i] + "\""
            if checked_list and i+1 in checked_list:
                item_str += " on"
                item_str += " off"
        rc, output = self._exe('dialog --checklist "{msg}" 0 0 0 {item_str}'.format(msg=msg, item_str=item_str))
        print item_str
        selections = []
        for choice in output.split('"'):
            if choice.strip():
        if selections:
            selections = tuple(selections)
            selections = None
        assert rc in (self.YES, self.NO, self.ESC), "checklist rc"
        return rc, selections

    def radiolist(self, msg, item_list, checked_item=None):
        Single selection radio menu, with default

        @param msg (str) = display message
        @param item_list (str list) = list of items
        @param checked_item (int) = default item to select
        @return: rc (int), selection (int)

        >>> rc, selection = dialog.radiolist('Select one option:',
        >>>     ('option 1', 'option 2'), 2)"""
        item_str = ""
        for i in range(len(item_list)):
            item_str += " " + str(i+1) + " \"" + item_list[i] + "\""
            if checked_item and i+1 == int(checked_item):
                item_str += " on"
                item_str += " off"
        rc, selection = self._exe('dialog --radiolist "{msg}" 0 0 0 {item_str}'.format(msg=msg, item_str=item_str))
        if not selection:
            selection = None
        assert rc in (self.YES, self.NO, self.ESC), "radiolist rc"
        return rc, selection

    def gauge(self, msg, percent):
        Progress bar (does not wait for user's input)

        @param msg (str) = display message
        @param percent (int) = progress
        @return: rc (int) always zero

        >>> for i in range(0, 100+1, 2):
        >>>   dialog.gauge('Progress:', i)
        >>>   time.sleep(.1)"""
        rc, output = self._exe('echo {percent} | dialog --gauge "{msg}" 0 0'.format(percent=percent, msg=msg))
        assert rc in (self.YES,), "guage rc"
        return rc

    def progress(self, msg, percent):
        Alias for self.guage()
        return self.gauge(msg=msg, percent=percent)

    def form(self, msg, item_list):
        Multiple item entry form

        @param msg (str) = display message
        @param item_list (str list) = list of items
        @return: rc (int), answers (str list)

        >>> rc, answers = dialog.form('Enter details',
        ...     ('first name', 'last name'))"""
        item_str = ""
        name_len = 1
        for i in range(len(item_list)):
            if len(item_list[i])+3 > name_len:
                name_len = len(item_list[i])+3
        for i in range(len(item_list)):
            item_str += " \"" + item_list[i] + ":\" " + str(i+1) + " 1 \"\" " + str(i+1) +\
                " " + str(name_len) + " 40 0"
        rc, answers = self._exe('dialog --form "{msg}" 0 0 0 {item_str}'.format(msg=msg, item_str=item_str),
        answers = answers.split('\n\n')
        if not answers:
            answers = None
        assert rc in (self.YES, self.NO, self.ESC), "form rc"
        return rc, answers

    def test(self):
        Test features

        >>> import dialog
        >>> dlg = dialog.Dialog()
        >>> dlg.test()

        def wait_user(msg):
            self.msgbox("%s" % msg)

        # rc_to_str
        wait_user("Testing return code...")
        print self.rc_to_str(self.YES)  # YES
        print self.rc_to_str(self.NO)  # NO
        print self.rc_to_str(self.ESC)  # ESX
            print self.rc_to_str(111)  # exception
            raise Exception("Did not throw exception")
        except Exception as rc:

        # msgbox
        wait_user("Testing msgbox...")
        rc = self.msgbox("msgbox")
        if rc not in (self.YES, self.ESC):
            raise Exception("Unknown return code for msgbox: %s" % rc)

        # yesno
        wait_user("Testing yesno...")
        rc = self.yesno("yesno")
        if rc not in (self.YES, self.NO, self.ESC):
            raise Exception("Unknown return code for yesno: %s" % rc)

        # infobox
        wait_user("Testing infobox...")
        rc = self.infobox("infobox sleep 1 sec")
        if rc not in (self.YES,):
            raise Exception("Unknown return code for infobox %s" % rc)

        # inputbox
        wait_user("Testing inputbox...")
        rc, output = self.inputbox("inputbox")
        if rc not in (self.YES, self.NO, self.ESC):
            raise Exception("Unknown return code for inputbox: %s" % rc)
        wait_user("Result: %s" % output)

        # textbox
        wait_user("Testing textbox...")
        rc = self.textbox("/etc/passwd")
        if rc not in (self.YES, self.ESC):
            raise Exception("Unknown return code for textbox: %s" % rc)

        # menu
        wait_user("Testing menu...")
        rc, selection ="menu", ("option 1", "option 2"))
        if rc not in (self.YES, self.NO, self.ESC):
            raise Exception("Unknown return code for menu: %s" % rc)
        wait_user("Result: %s" % selection)

        # checklist
        wait_user("Testing checklist...")
        rc, selection = self.checklist("menu", ["option 1", "option 2"])
        if rc not in (self.YES, self.NO, self.ESC):
            raise Exception("Unknown return code for checklist: %s" % rc)
        wait_user("Result: %s" % str(selection))
        wait_user("Testing checklist with default selection...")
        rc, selection = self.checklist("menu", ["option 1", "option 2"], [1, 2])
        if rc not in (self.YES, self.NO, self.ESC):
            raise Exception("Unknown return code for checklist: %s" % rc)
        wait_user("Result: %s" % str(selection))

        # radiolist
        wait_user("Testing radiolist...")
        rc, selection = self.radiolist("menu", ["option 1", "option 2"])
        if rc not in (self.YES, self.NO, self.ESC):
            raise Exception("Unknown return code for radiolist: %s" % rc)
        wait_user("Result: %s" % str(selection))
        wait_user("Testing radiolist with default selection...")
        rc, selection = self.radiolist("menu", ["option 1", "option 2"], 2)
        if rc not in (self.YES, self.NO, self.ESC):
            raise Exception("Unknown return code for radiolist: %s" % rc)
        wait_user("Result: %s" % str(selection))

        # guage
        wait_user("Testing guage...")
        for i in range(4+1):
            rc = self.gauge("test guage", i*25)
            # rc = self.progress("test progress", i*25)
            if rc not in (self.YES,):
                raise Exception("Unknown return code for guage: %s" % rc)

        # form
        wait_user("Testing form...")
        rc, answers = self.form("form", ["option 1", "option 2"])
        if rc not in (self.YES, self.NO, self.ESC):
            raise Exception("Unknown return code for form: %s" % rc)
        wait_user("Result: %s" % str(answers))

        # done with test, cleanup
        wait_user("Done with tests...")

if __name__ == "__main__":
    # dialog = Dialog()
    # dialog.test()

Busy Loop and Thread.yield()

"I want to tell my Python threads to yield, and so avoid hogging the CPU unnecessarily. In Java, you could do that using the Thread.yield() function"





Programming with PyUSB 1.0 -


git clone
import usb.core
dev = usb.core.find()
# find our device
dev = usb.core.find(idVendor=0xfffe, idProduct=0x0001)

"Dealing with multiple identical devices - Sometimes you may have two identical devices connected to the computer. How can you differentiate them? Device objects come with two additional attributes which are not part of the USB Spec, but are very useful: bus and address attributes. First of all, it is worth to say that these attributes come from the backend and a backend is free to not support them, in which case they are set to None. That said, these attributes represent the bus number and bus address of the device and, as you might already have imagined, can be used to differentiate two devices with the same idVendor and idProduct attributes."


Python GUI Bitcoin: [95]

import json
import urllib2
from Tkinter import *


mtusd_price = output[-10]['avg']
mteur_price = output[31]['avg']
mtgbp_price = output[44]['avg']

MTUSD= 'USD' + ' ' + str(mtusd_price)
MTEUR= 'EUR' + ' ' + str(mteur_price)
MTGBP = 'GBP' + ' ' + str(mtgbp_price)

root= Tk()
root.title("Bitcoin Price")

USD = Label(root, text=MTUSD)
EUR = Label(root, text=MTEUR)
GBP = Label(root, text=MTGBP)


"ctypes is a foreign function library for Python. It provides C compatible data types, and allows calling functions in DLLs or shared libraries. It can be used to wrap these libraries in pure Python."

-- 15.17. ctypes — A foreign function library for Python — Python v2.7.7 documentation -

Use "c scope" to build a database of symbols to see what options are available.



#include <stdio.h>

void myprint(void);

void myprint()
    printf("hello world\n");


gcc -shared -Wl,-soname,testlib -o -fPIC testlib.c


import ctypes

testlib = ctypes.CDLL('/full/path/to/')

Source: python - ctypes - Beginner - Stack Overflow -


nzjrs's Gists -


#include <stdio.h>

int sum(double *x,int n)
  int i;
  double counter;
  counter = 0;

  return counter;
gcc -c sum.c
gcc -shared -o sum.o
from ctypes import *
for i in range(10):

Source: Ctypes — Numerical Computing with Sage v6.2 -



if sys.version_info >= (1, 0, 0):
  warnings.warn("Danger Will Robinson: trying Phi on untested Python 3.0!")


Python 2: [96]

class Enum(set):
    """ Basic enumerated type
        >>> Animals = phi.Enum(["DOG", "CAT", "HORSE"])
        >>> Animals.DOG
        >>> 'HORSE' in Animals
    def __getattr__(self, name):
        if name in self:
            return name
        raise AttributeError("Missing Enum property: %s" % name)
    def __setattr__(self, name, value):  # this makes it read-only
        raise AttributeError("Unable to modify Enum property: %s (%s)" % (name, value))

Animals = Enum(["DOG", "CAT", "HORSE"])

print(Animals.DOG)  # 'DOG'


class Stationery:
    Pen, Pencil, Eraser = range(0, 3)

One liner:

class Enum(tuple): __getattr__ = tuple.index
>>> State = Enum(['Unclaimed', 'Claimed'])
>>> State.Claimed
>>> State[1]
>>> State
('Unclaimed', 'Claimed')
>>> range(len(State))
[0, 1]
>>> [(k, State[k]) for k in range(len(State))]
[(0, 'Unclaimed'), (1, 'Claimed')]
>>> [(k, getattr(State, k)) for k in State]
[('Unclaimed', 0), ('Claimed', 1)]

Python 3 supports enum natively with the enum.Enum library:


"This module encapsulates the access for the serial port. It provides backends for Python running on Windows, OSX, Linux, BSD (possibly any POSIX compliant system) and IronPython. The module named “serial” automatically selects the appropriate backend."

Intro -


import serial
import time

ser = serial.Serial(

print("connected to: " + ser.portstr)

#this will store the line
seq = []
count = 1

while True:
    for c in
        seq.append(chr(c)) #convert from ANSII
        joined_seq = ''.join(str(v) for v in seq) #Make a string from array

        if chr(c) == '\n':
            print("Line " + str(count) + ': ' + joined_seq)
            seq = []
            count += 1




I modified the code to monitor a COM port: (Windows)

Monitor Serial:

import serial, time, sys

logger = open("serial.txt", "w")
timer = time.time()

def log(msg, head=False):
    msg = msg.strip()
    if head:
      msg = ">>> " + msg
    logger.write(msg + "\n")
    sys.stdout.write(msg + "\n")
    newtime = time.time()
    if newtime > timer + 1:

while True:

    ser = None
        ser = serial.Serial(
    except serial.serialutil.SerialException as e:
        log(str(e), True)

    log("Connected to: " + ser.portstr, True)

        line = []
        while True:
            for c in ser.readline():
                if c == "\n":
                    line = []
    except serial.serialutil.SerialException as e:
        log(str(e), True)


>>> class NewBool(int):
...    def __new__(cls, value):
...        return int.__new__(cls, bool(value))

>>> y = NewBool(2)
>>> y == 1

Also check out the __cmp__


import md5"Nobody inspects the spammish repetition").hexdigest()
# 'bb649c83dd1ea5c9d9dec9a18df0ffe9'
$ echo -n "Nobody inspects the spammish repetition" | md5sum
bb649c83dd1ea5c9d9dec9a18df0ffe9  -

== External Libraries ==

=== execnet ===

Python Package Index - execnet -

Examples -
* basic local and remote communication -

Execute source code in subprocess, communicate through a channel:
>>> import execnet
>>> gw = execnet.makegateway()
>>> channel = gw.remote_exec("channel.send(channel.receive()+1)")
>>> channel.send(1)
>>> channel.receive()

Compare current working directories

>>> import execnet, os
>>> gw = execnet.makegateway()
>>> ch = gw.remote_exec("import os; channel.send(os.getcwd())")
>>> res = ch.receive()
>>> assert res == os.getcwd()

Get information from remote ssh account

>>> import execnet, os
>>> gw = execnet.makegateway("")
>>> channel = gw.remote_exec("""
...     import sys, os
...     channel.send((sys.platform, sys.version_info, os.getpid()))
... """)
>>> platform, version_info, remote_pid = channel.receive()
>>> platform
>>> version_info
(2, 4, 2, 'final', 0)

remote-exec a function (avoiding inlined source part I)

import execnet

def multiplier(channel, factor):
    while not channel.isclosed():
        param = channel.receive()
        channel.send(param * factor)

gw = execnet.makegateway()
channel = gw.remote_exec(multiplier, factor=10)

for i in range(5):
    result = channel.receive()
    assert result == i * 10


remote-exec a module (avoiding inlined source part II)

"You can pass a module object to remote_exec in which case its source code will be sent. No dependencies will be transferred so the module must be self-contained or only use modules that are installed on the “other” side. Module code can detect if it is running in a remote_exec situation by checking for the special __name__ attribute."
# content of a module

if __name__ == '__channelexec__':
    channel.send('initialization complete')
>>> import execnet, remote1
>>> gw = execnet.makegateway()
>>> ch = gw.remote_exec(remote1)
>>> print (ch.receive())
initialization complete

Use a callback instead of receive() and wait for completion

>>> import execnet
>>> gw = execnet.makegateway()
>>> channel = gw.remote_exec("for i in range(10): channel.send(i)")
>>> l = []
>>> channel.setcallback(l.append, endmarker=None)
>>> channel.waitclose() # waits for closing, i.e. remote exec finish
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, None]

a simple command loop pattern

# contents of:
import os

def simple(arg):
    return arg + 1

def listdir(path):
    return os.listdir(path)
if __name__ == '__channelexec__':
    for item in channel:
>>> import execnet, remotecmd
>>> gw = execnet.makegateway()
>>> ch = gw.remote_exec(remotecmd)
>>> ch.send('simple(10)') # execute func-call remotely
>>> ch.receive()



apt-get install python-setuptools python-dev
easy_install pip
pip install evdev

Simple read usage:

from evdev import InputDevice
from evdev import ecodes

# look for a /dev/input/by-id/usb...kbd or similar
DEVICE = "/dev/input/by-id/usb-ORTEK_USB_Keyboard_Hub-event-kbd"

dev = InputDevice(DEVICE)

while True:
    r, w, x = select([dev], [], [], 5)
    for event in
        if event.type == 1 and event.value == 1:
            print event.code

List LED states:


Inject usage: (does not appear to really work)

from evdev import InputDevice
from evdev import ecodes
from evdev import UInput

DEVICE = "/dev/input/by-id/usb-ORTEK_USB_Keyboard_Hub-event-kbd"
dev = InputDevice(DEVICE)

ui = UInput()

ui.write(ecodes.EV_KEY, ecodes.KEY_4, 1)
ui.write(ecodes.EV_KEY, ecodes.KEY_4, 0)

ui.write(ecodes.EV_KEY, ecodes.KEY_NUMLOCK, 1)
ui.write(ecodes.EV_KEY, ecodes.KEY_NUMLOCK, 0)





python-ldap: LDAP client API for Python -

Index of Packages : Python Package Index -

yum install python-devel openldap-devel
pip install python-ldap




xmmp is the Jabber protocol

Best options:

MIT libraries.

  • sleekxmpp (was: sleekxmpp) is pretty popular and is used for examples in Peter Saint-Andre's XMPP book from O'Reilly. It reportedly works well, and finally got an email list in April 2010, and has a chat room at

GPL libraries.

  • xmpppy is used by gajim.

LPGL libraries.

  • pyxmpp is pretty good and uses libxml2 internally for xml parsing.
  • pyxmpp2 is the next version of pyxmpp, runs on python 2.7 and 3.2, and removes the libxml2 requirement. Like many, it requires dnspython.
  • jabberpy is the original and still works for a lot of tasks, but is thoroughly unmaintained.

Other libraries.

  • Wokkel, mentioned in another post. That's a new one for me, based on twisted.

chat - Best python XMPP / Jabber client library? - Stack Overflow -


xmpppy: the jabber python project -

"xmpppy is a Python library that is targeted to provide easy scripting with Jabber. Similar projects are Twisted Words and

This library was not designed from scratch. It inherits some code from jabberpy and have very similar API in many places. Though it is separate project since it have almost completely different architecture and primarily aims to work with jabberd2 - the new Open Source Jabber Server.

xmpppy is distributed under the terms of GNU General Public License and can be freely redistributed without any charge."

wget -O xmpppy-0.4.1.tar.gz
tar -zvxf xmpppy-0.4.1.tar.gz
pip install pydns

--- basic connection ---

#!/usr/bin/env python
import xmpp
jid = xmpp.JID(user)
connection = xmpp.Client(server,debug=[])
result = connection.auth(jid.getNode(), password,"LFY-client")
while connection.Process(1):


--- basic receive ---

Print messages when received:

#!/usr/bin/env python
import xmpp
def message_handler(connect_object, message_node):
        message = "Welcome to my first Gtalk Bot :)"
        connect_object.send( xmpp.Message( message_node.getFrom() ,message))
jid = xmpp.JID(user)
connection = xmpp.Client(server)
result = connection.auth(jid.getNode(), password, "LFY-client")
connection.RegisterHandler('message', message_handler)
while connection.Process(1):


--- remote-control shell bot ---

Replace the simple bot’s message_handler() function with this new one:

def message_handler(connect_object,message_node):
        command = str(message_node.getBody())
        process = subprocess.Popen(command,shell=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        message =
        if message=="":
        connect_object.send( xmpp.Message( message_node.getFrom() ,message))


--- roster ---

import xmpp
import sys

userID   = '' 
password = 'mypassword'
ressource = 'Script'

jid  = xmpp.protocol.JID(userID)
jabber  = xmpp.Client(jid.getDomain(), debug=[])

connection = jabber.connect(('',5222))
auth = jabber.auth(jid.getNode(), password, ressource)

myroster = jabber.getRoster()

To get status, you have to wait for the other clients to send you their status:

jabber.RegisterHandler('presence', myPresenceHandler)

def myPresenceHandler(con, event):
  fromjid = event.getFrom().getStripped()
  status = myroster.getStatus(fromjid)

References: python - XMPP chat: accessing contacts' status messages with xmppPy's Roster - Stack Overflow -

--- full roster script ---

kparal/jabber-roster · GitHub -

A simple Python tool for listing your Jabber roster contacts. You can use it to easily backup list of your buddies.

This program is maintained, but not further developed. Bugs will be fixed, but no new features will be added. If you want to work on this program, don't hesitate to contact me, I will gladly assign you to the development team.

git clone

--- vcard ---

This information is not in the roster. You will need to query the clients individually and get their vCard by sending this IQ :

<iq from=''
  <vCard xmlns='vcard-temp'/>

My solution:

def vcard(disp, jid):
    msg = xmpp.protocol.Iq()
    qc = msg.addChild('vCard')
    qc.setAttr('xmlns', 'vcard-temp')
    rep = disp.SendAndWaitForResponse(msg)
    # to see what other fields are available in the XML output:
    # print rep
    for i in rep.getChildren():
        for j in i.getChildren():
            if j.getName() == "TITLE":
                title = j.getData().encode('utf-8')
            for k in j.getChildren():
                if k.getName() == "GIVEN":
                    fname = k.getData().encode('utf-8')
                if k.getName() == "FAMILY":
                    lname = k.getData().encode('utf-8')
                if k.getName() == "ORGUNIT":
                    department = k.getData().encode('utf-8')
                if k.getName() == "REGION":
                    region = k.getData().encode('utf-8')
    return fname, lname, title, department, region



Google APIs Client Library for Python, which includes:

  • google-api-python-client: The core Python library for accessing Google APIs.
  • oauth2client: A Python client library for OAuth 2.0.
  • Sample applications using google-api-python-client and oauth2client


  • Calendar API - Python/Command Line

- Download:



git clone
git clone


Documentation Tools


pydoc -

Documentation generator and online help system

Doxygen and Doxypy

See Python/Doxygen


Sphinx -

"Sphinx is a tool that makes it easy to create intelligent and beautiful documentation, written by Georg Brandl and licensed under the BSD license.

It was originally created for the new Python documentation, and it has excellent facilities for the documentation of Python projects, but C/C++ is already supported as well, and it is planned to add special support for other languages as well. Of course, this site is also created from reStructuredText sources using Sphinx!"


epydoc -

"Epydoc is a tool for generating API documentation for Python modules, based on their docstrings. For an example of epydoc's output, see the API documentation for epydoc itself (html, pdf). A lightweight markup language called epytext can be used to format docstrings, and to add information about specific fields, such as parameters and instance variables. Epydoc also understands docstrings written in reStructuredText, Javadoc, and plaintext. For a more extensive example of epydoc's output, see the API documentation for Python 2.5. "

Python 3

Python 3 Wall of Superpowers

Python 3 Wall of Superpowers -

"As listed on PyPI - packages in red don't support python 3, packages in green do. Hopefully one day everything will be greener. "

New in Python 3


New in 3.3

>>> def f():
...   def g():
...     class H: pass
...     return H
...   return g

>>> H=f()()

>>> # 3.3:
>>> H
<class '__main__.f.<locals>.g.<locals>.H'>
>>> H.__qualname__

>>> # 2.6:
>>> H
<class __main__.H at 0x2b885044b890>
>>> H.__qualname__
AttributeError: class H has no attribute '__qualname__'

Python 2 or Python 3

Python2orPython3 - PythonInfo Wiki -

Short version: Python 2.x is the status quo, Python 3.x is the shiny new thing.
At the time of writing (July 4, 2010), the final 2.7 release is out, with a statement of extended support for this end-of-life release. The 2.x branch will see no new major releases after that. 3.x is under active and continued development, with 3.1 already available and 3.2 due for release around the turn of the year.

Python 2.7.1 Release -

What’s New In Python 3.0 — Python v3.0.1 documentation -

  • print() requires parenthesis
  • raw_input() is now input()

Python for Windows

Python for Windows -

Windows IDEs


Hide Console

fork() does not work on windows. So what else can we do?

How to hide console window in python? -

  • "Simply save it with a .pyw extension. This will prevent the console window from opening. Explanation at the bottom of section [97]"

32bit vs 64bit



Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

64bit: (note: default path is C:\Python27)

Python 2.7.6 (default, Nov 10 2013, 19:24:24) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

Task Manager:

python.exe *32


>>> import platform
>>> platform.python_compiler()
'MSC v.1500 32 bit (Intel)'
>>> import platform
>>> platform.python_compiler()
'MSC v.1500 64 bit (AMD64)'

Code: [98]

import struct
def is_python_64bit():
    return (struct.calcsize("P") == 8)

Stand-Alone Executable

Python ZIP Application

Python can be run from a single bundled zip file - similar to a Java WAR! Python has been able to (Allow interpreter to execute a zip file

"Python quietly added a new feature in 2.6 that makes it possible to bundle up a directory full of Python code into a single executable file." [99]

When a python zip application is executed, it will execute '' [100]

mkdir app
touch app/
cd app
zip -r ../ *
cd ..

Load a resource - The easiest way to find and load a program bundle like this is to use the pkg_resources module. -

# file: app/

import pkg_resources

def main():
  print('The rain in Spain falls mainly in the plain.')
  print(pkg_resources.resource_string('resources', 'inFrance.txt'))

if __name__ == '__main__':

Now use a bit of UNIX magic to turn into an executable:

echo '#!/usr/bin/env python' | cat - > app2
chmod +x app2

"The zip file format is designed to allow a small executable program to be inserted at the front (that's how self-extracting zip files are created), so this is kosher and doesn't corrupt the zip file." [101]

Now you can simply run app2 like any executable.

$ ./app2


Shell Python Loader

Came before the Python ZIP Application support.

Python zip exe - -

"This takes your Python application and packages it into a single executable shell script. You application can be made of up multiple python scripts. The executable shell script is also compressed, so the resulting script is smaller than the original. This should work on any system that has Python and a Bourne shell (works on Cygwin too). Note that this does not package the Python interpreter into the executable. This packages only you python code into a zip file, but this still allows you to distribute your application as a single shell script that will run on any system with Python installed. In theory, you could zip the .pyc files, but the byte code is guaranteed to run only on exactly the same version Python interpreter (including minor version). It is safer to just package the .py files."

First you need to zip all your python files:


# This is a self-extracting executable.
# Execute this like any normal executable.
# You may need to "chmod a+x" this file.
# This is a binary ZIP file with a Python loader header.
# Bourne shell loader:
PYTHON=$(which python 2>/dev/null)
if [ ! -x "$PYTHON" ] ; then
    echo "Python not found!"
    exit 1
exec $PYTHON -c "
# Python loader:
import sys, os
if int(sys.version[0])<2:
    print 'Python version 2.3 final or greater is required.'
    print 'Your version is', sys.version
major = sys.version_info[0]
minor = sys.version_info[1]
releaselevel = sys.version_info[3]
if (major==2 and minor<3) or (major==2 and minor==3 and releaselevel!='final'):
    print 'Python version 2.3 final or greater is required.'
    print 'Your version is', sys.version
sys.path.insert(0, sys.argv[1])
del sys.argv[0:1]
import main
" $0 $@
# Zip file:

Concatinate with the zip file.

cat > main

Set main as executable.

chmod +x main

"This is an improvement on Joerg Raedler's Python recipe, 215301. The original closes stdin. The herefile in the shell script redirects stdin before python gets a chance to start. This disables raw_input() and anything else that reads sys.stdin. Unfortunately, Raedler's boot script closes stdin, which is a fairly big limitation. The herefile in the shell script (END_OF_PYTHON_CODE) redirects stdin before python starts; stdin is closed at the end of the herefile. This disables raw_input() and anything else that reads sys.stdin. "



py2exe -

"py2exe is a Python Distutils extension which converts Python scripts into executable Windows programs, able to run without requiring a Python installation. "

Tutorial -

Note: Install the version that matches your version of python

Note: run installer "as administrator"

from distutils.core import setup
import py2exe


No console window version: [102] [103]

from distutils.core import setup
import py2exe



python py2exe

pygame is more complex:




Windows Libraries

Python for Windows Extensions - PyWin32

Python for Windows Extensions -

  • There is a build for each python version and architecture (32bit vs 64bit)

Follow event viewer

import win32evtlog  # install Python for Windows Extensions -

# open event viewer
server = "localhost"
source = "System"
handle = win32evtlog.OpenEventLog(server, source)

# seek to end of log
events = win32evtlog.ReadEventLog(handle, flags, 0)

# return to normal reading direction
win32evtlog.ReadEventLog(handle, flags, 0)  # ignore initial logs

# loop through events
while True:
    events = win32evtlog.ReadEventLog(handle, flags, 0)
    if events:
        for event in events:
            data = event.StringInserts
            if data:
                # Example: 07/03/14 12:34:35 - MYEVENTSOURCE - 0 - 1 - 4  "HELLO"
                print event.TimeGenerated, '-', event.SourceName, '-', event.EventCategory, '-', event.EventID, '-', event.EventType, '-', '"%s"' % " ".join(data)
    time.sleep(.5)  # yield cpu


PyCrypto for Windows

Download Install:

Installation: [104]


Paramiko for Windows

  1. Install PyCrypto (above)
  2. pip install paramiko

PyGame for Windows

Download -


Python: Myths about Indentation -

There are quite some prejudices and myths about Python's indentation rules among people who don't really know Python. I'll try to address a few of these concerns on this page.
  • "Whitespace is significant in Python source code."
  • "Python forces me to use a certain indentation style."
  • "You cannot safely mix tabs and spaces in Python."
  • "I just don't like it."
  • "How does the compiler parse the indentation?"

PEP - Python Enhancement Proposals

Python Enhancement Proposals (PEPs)

Convert Tabs to Spaces

Convert tabs to 4 spaces:

sed -i 's/\t/    /g'


  • pep8 - python style guide checker
  • pychecker - more verbose, imports code, slower
  • pylint - most verbose, configurable, "rates" code
  • pyflakes - least verbose (dead/redundant code)


yum install pyflakes pychecker
# pylint - not sure which pacakge includes


See Python/pep8


See Python/pychecker


See Python/pylint

PEP8 Style Guidlines

PEP 8 -- Style Guide for Python Code -

  • Use spaces - Use 4 spaces per indentation level.
  • Never mix tabs and spaces.
  • Limit all lines to a maximum of 79 characters.
  • Line wrapping - The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses.
  • Encoding - For Python 3.0 and beyond, UTF-8 is preferred over Latin-1, see PEP 3120.
  • Imports should usually be on separate lines
  • Avoid extraneous whitespace in most situations
  • Always use 'self' for the first argument to instance methods.

PEP8 Style Checker

See #pep8

Benefits of Python

What is the return on investment for this group by learning Python? The list of benefits below sums up what the language has to offer:

  • Python is designed from the ground up to encourage maintainable, readable and reusable code. It has great support for particular paradigms such as object-oriented programming.
  • Python code is usually around a quarter of the length of code generated by C++ or Java - less typing, less debugging and less maintenance.
  • Python does not need to be compiled or linked, which allows for a more iterative programming style and a quicker development cycle.
  • Python is extremely portable. Code can be easily exchanged between Windows, Mac OS and Linux with little to no modification.
  • Python comes with a staggering array of standard libraries and mature third-party projects. Web development, scientific computing, operator system access and network scripting are all supported.
  • Python can talk with many other languages easily. It has support for invoking C and C++ code and can itself be called from libraries written in these languages.

Source: - Quant Reading List Python Programming

Benefits of Python:

  • Python is available for most operating systems, including Windows, UNIX, Linux, and Mac OS.
  • Python's clean object-oriented design and extensive support libraries offer two to ten fold the programmer productivity seen with languages like C, C++, C#, Java, VB, and Perl.

Source: Python Development - Benefits of Python - Digital Mesh -

Python Package Management

Install Module

Installing Python Modules — Python v2.7.1 documentation -

The new standard: Distutils

python install

Download and extract target package, then:

sudo python install

Note: Running install builds and installs all modules in one run


python --help

Incremental method:

python build
python install
python build --help   # help

Force rebuild and install:

python build --force    # if have to repeat
python install --force  # if have to repeat

Default installation paths:

Platform	Standard installation location		Default value
Unix (pure)	prefix/lib/pythonX.Y/site-packages	/usr/local/lib/pythonX.Y/site-packages
Unix (non-pure)	exec-prefix/lib/pythonX.Y/site-packages	/usr/local/lib/pythonX.Y/site-packages
Windows		prefix					C:\Python

Alternate install path:

python install --home=<dir>

Alternate base folders:

Type of file			Installation Directory	Override option
pure module distribution	home/lib/python		--install-purelib
non-pure module distribution	home/lib/python		--install-platlib
scripts				home/bin		--install-scripts
data				home/share		--install-data

Modifying Python’s Search Path:

import sys

Python Package Index

user-submitted Python packages:
sudo python install

Package Site:[PACKAGE]

If they are on

pip install foo

For Python programmers, is there anything equivalent to Perl's CPAN? - Stack Overflow

Easy Installation

EasyInstall - The PEAK Developers' Center -

Easy Install is a python module (easy_install) bundled with setuptools that lets you automatically download, build, install, and manage Python packages.

CheeseShopTutorial - PythonInfo Wiki -

EasyInstall (easy_install) gives you a quick and painless way to install packages remotely by connecting to the Package Index or even other websites via HTTP. It is somewhat analogous to the CPAN and PEAR tools for Perl and PHP, respectively.


See Python/pip

Create PIP Package

See Python/pip#Create PIP Package


distribute - Easily download, build, install, upgrade, and uninstall Python packages

distribute 0.6.24 : Python Package Index -


mkdir -p ~/.src ; cd ~/.src
tar -zvxf distribute-0.6.24.tar.gz
cd distribute-0.6.24
sudo python install


echo "import distribute" | grep python

Named Parameters

Python allows function arguments to have default values; if the function is called without the argument, the argument gets its default value. Futhermore, arguments can be specified in any order by using named arguments. Stored procedures in SQL Server Transact/SQL can do this, so if you're a SQL Server scripting guru, you can skim this part.

Here is an example of info, a function with two optional arguments:

def info(object, spacing=10, collapse=1):

spacing and collapse are optional, because they have default values defined. object is required, because it has no default value. If info is called with only one argument, spacing defaults to 10 and collapse defaults to 1. If info is called with two arguments, collapse still defaults to 1.

Say you want to specify a value for collapse but want to accept the default value for spacing. In most languages, you would be out of luck, because you would need to call the function with three arguments. But in Python, arguments can be specified by name, in any order.

If you add a parameter with two asterixes (**) on the end of a function definition, that parameter is taken as being the name of a dictionary into which all otherwise unidentified parameters are stored.

def getnet(gross, taxrate=17.5, **others):
  net = gross / (1.0 + taxrate*0.01)
  for more in others.keys():
    print "GETNET:",more," - ",others[more]
  return neto

Python vs other languages

Python is Cool (and Perl is not), Especially for C/C++ Programmers

Experimenting with Memory Remanence

Experimenting with Memory Remanence

    #!/usr/bin/env python

    # a pirate's favorite chemical element
    a = ""
    while 1: a += "ARGON" 
sudo strings /dev/mem | less

UTOSC 2008 - 90 percent of the Python you need to know (in 90 minutes)

See UTOSC 2008 - 90 percent of the Python you need to know (in 90 minutes)

Program Layout Best Practices Presentation

Program layout best practices - Seth House <> - Utah Django User Group - 2011-06-23

Module skeleton:

#!/usr/bin/env python
# coding: utf-8

"""Module docstring"""

__author__ = ’Mr. Me <>’
__version__ = ’1.2.3’

if __name__==’__main__’:

Executing modules as scripts

# PEP 338:
python -m SimpleHTTPServer

CLI package

cli — command line tools — pyCLI v1.1.1 documentation -

Python IDE

IntegratedDevelopmentEnvironments - PythonInfo Wiki -

  • IDEs with introspection-based code completion and integrated debugger
  • IDEs with introspection-based code completion /or/ integrated debugger
  • IDEs with integrated gui builder
  • Editors
  • Reviews

Is there a good, free Python IDE for Windows? - Stack Overflow -

  • Eclipse with PyDev
  • Aptana Studio 3
  • Active State also offers Komodo Edit
  • WingWare -
  • Eric IDE -
  • MMM-Experts - Products -
    • PyScripter is a Python IDE built in Python for Delphi(P4D) components and created with the ambition to become competitive in functionality with commercial Windows-based IDEs available for other languages. Being built in a compiled language is rather snappier than some of the other Python IDEs and provides an extensive blend of features that make it a productive Python development environment.
  • ActiveState Python comes with PythonWin which is reasonably good
  • PyCharm from JetBrains is not free in general, but has free early access program.
  • SPE - Stani's Python Editor, is pretty awesome. See
  • You may want to take a look at the pdb module -
  • Geany ( is a fairly light text editor that does a lot of IDE-like stuff.
  • SciTE:
  • Editra:
  • Netbeans has support for python development in their Python Early Access version. -
  • I use Notepad++ with the appropriate settings.
  • The Eric Python IDE -
  • BOa Constructor really nice! drag and drop stuff!
  • DrPtyhon is also very nice, and has many plug-ins.
  • Vim
  • I will make a plug for my preference of Python development environments: SPE. SPE is an integrated development environment that provides code completion, integrated Python shell, calltips, and various helper tools like notes and todo lists. It also includes wxGlade so you can make wxPython applications easily. Plus, it includes the python debugger WinPdb.
  • this software is my sample visual python ide.its demo version.
  • I use PyScripter, and find it pretty awesome.
  • There is one Python IDE written in Java: jHepWork: it can detect Python's indentation rules rather nicely (+ detects structure of a code and syntax)
  • gvim built with your python version support and with this plugins:
    • NERDTree (don't read he's blog ;)
    • matchit (it's builtin in new versions just put a symlink or junction)
    • pyflakes
    • pysmell
  • Try Python Tools for Visual Studio. Supports CPython and IronPython. -

PythonEditors - PythonInfo Wiki -

  • List of editors

Python Ide -

There are a lot of Ides for PythonLanguage springing up. Many of them written in Python

Is there a better Python IDE? « Lennart Regebro: Python, Plone, Web -

I’m currently a user of WingIDE, but sometimes I feel the need for something better, mainly something that is a bit more stable. WingIDE often hangs when switching projects, sometimes the file listing decides that you must double-click on the expand arrow instead of just click on it, etc. It still doesn’t have macros despite this being a much requested feature (how hard can it be to add, really?) and I never really get the hang of the SVN integration, so I just ignore it. And they keep recommending me to use WingIDE’s separate instance of GTK instead of the System GTK, which is daft. Their designs are ugly, and it should work with the system GTK so it looks the same. Anything else is just silly.

Open Komodo

Open Komodo -

Open Komodo is the source code repository for Komodo Edit - a free multi-language editor for dynamic languages, based on the award-winning Komodo IDE. ActiveState created the Open Komodo code repository in August 2007 and it has been used since then for building all Komodo Edit releases.


Build an Eclipse development environment for Perl, Python, and PHP -

This tutorial shows how Eclipse's DLTK makes it possible to build development tools for scripting languages. In particular, it explains how to implement syntax coloring, user preferences, and interpreter integration in a plug-in-based project.

See #PyDev


EasyEclipse -

EasyEclipse packages together Eclipse, the open-source software

development platform, and selected open source plugins.

We select, assemble, test, patch, build installers and document

a full IDE, offered as reliable distributions and plugins.

EasyEclipse is:
  • Free and open-source,
  • Easy to download and install, and
  • Simple to maintain, without version and dependency issues.

EasyEclipse for Python -

EclipsePythonIntegration - PythonInfo Wiki -

Using Eclipse as a Python editor


PyDev -

PyDev is a Python IDE for Eclipse, which may be used in Python, Jython and IronPython development.


Pydev - (old link)

How to Configure Eclipse for Python -

Python Development with PyDev and Eclipse - Tutorial -

This article describes how to write and debug Python programs with Eclipse

PyDev Getting Started -

Running a program -


Supported Task Tags:

  • TODO:
  • TASK:
  • FIXME:



  1. Platform Runtime Binary (download around 45-50 MB) - or
    • Example: Eclipse Classic 4.2.2
  2. Eclipse Software Add - PyDev:
  3. Eclipse Software Add - MercurialEclipse: [105] [106]
  4. Switch perspectives to PyDev

The Eric Python IDE

The Eric Python IDE -

Eric is a full featured Python and Ruby editor and IDE, written in python. It is based on the cross platform Qt gui toolkit, integrating the highly flexible Scintilla editor control. It is designed to be usable as everdays' quick and dirty editor as well as being usable as a professional project management tool integrating many advanced features Python offers the professional coder. eric4 includes a plugin system, which allows easy extension of the IDE functionality with plugins downloadable from the net.

Eric Python IDE - Wikipedia -

Wingware Python IDE

The Intelligent Development Environment for Python Programmers - Wingware Python IDE -

Wingware's Python IDE is an Integrated Development Environment designed specifically for the Python programming language. Since 1999, Wingware has focused on Python.


Python .vimrc:

" Kenneth's Preferences

set nobackup            " don't create backup files
"set nohlsearch          " don't highlight search results
set hlsearch            " do highlight search results
set incsearch           " incremental search
set ignorecase          " ignore case during search
set smartcase           " do case sensitive search when upper case is typed
set shiftwidth=4        " shift blocks in 4 space increments
set tabstop=4           " the (typewriter) tab stops are every 4 spaces
set smarttab            " tab at beginning of line inserts 4 spaces (shiftwidth)
set showmatch           " briefly highlight matching bracket
set matchtime=1         " highlight match for 0.1 seconds
set background=dark     " black background
set shiftwidth=4        " shift blocks by 4 spaces
set shortmess=at        " use short messages so we don't have to hit enter
set expandtab           " expand tabs into spaces
set scrolloff=5         " always show 5 lines before & after cursor
set shell=bash          " use bash for the command shell
set shellcmdflag=-lc    " make bash startup as a login shell for ~/.bashrc
"set nowrap              " do not wrap long lines
set visualbell          " flash the screen rather than beep
set noerrorbells        " don't beep on errors
set wildmenu            " enable filename tab completion
set ruler               " display the ruler: shows cursor position
set cpoptions+=$        " display '$' at end of cw sequence
set title               " display filename and vim info in window title bar
set modelines=5         " search the first and last 5 lines for "vim:" modeline
set backspace=2         " backspace deletes

set wildignore=*.o,*.class,*.pyc,*.pyo,*.a,*.so,*.dll,*.exe,core,*.jar,*.zip

"let loaded_matchparen = 1 " don't highlight parenthesis match

filetype indent on

"if v:version > 700
"    set cursorline          " underline the current line

if &t_Co > 2 || has( "gui_running" )
    syntax on

if has( 'mouse' )
    set mouse=a " enable mouse

if has( "autocmd" )
    " open the file in the last place we were editing
    autocmd BufReadPost *
        \ if line("'\"") > 1 && line("'\"") <= line("$") |
        \     exe "normal! g'\"" |
        \ endif

"folding settings
set foldmethod=indent   "fold based on indent
set foldnestmax=10      "deepest fold is 10 levels
set nofoldenable        "dont fold by default
set foldlevel=10        "this is just what i use

" Auto clear trailing white space in Python files
func! DeleteTrailingWS()
    exec "normal mz"
    exe "normal `z"
autocmd BufWrite *.py :call DeleteTrailingWS()

Older versions:

set nocp                " turn off compatibility mode
set nomodeline          " do not allow files to dictate vim behavior

set incsearch           " incremental search
set ignorecase          " ignore case during search

set visualbell          " flash the screen rather than beep
set noerrorbells        " don't beep on errors
set background=dark     " black background
syntax on               " turn on syntax highlighting
set hlsearch            " highligh last search pattern

set nowrap              " do not wrap long lines
set scrolloff=5         " always show 5 lines before & after cursor
set ruler               " show the cursor position all the time
set backspace=2         " allow backspace deletion
set bs=indent,eol,start " allow backspacing over everything in insert mode

set shiftwidth=4        " shift blocks in 4 space increments
set tabstop=4           " the (typewriter) tab stops are every 4 spaces
set expandtab           " expand tab into spaces
set smarttab            " tab at beginning of line inserts 4 spaces (shiftwidth)
filetype indent on      " turn on auto indent for specific file types

Other options:

set number              " show line numbers
set numberwidth=1       " remove space before largest line numbers

set smartindent         " ?? - maybe included in 'filetype indent on'

set ai                  " always auto indent

if has( "autocmd" )
    " open the file in the last place we were editing
    autocmd BufReadPost *
        \ if line("'\"") > 1 && line("'\"") <= line("$") |
        \     exe "normal! g'\"" |
        \ endif

Kenneth's fio .vimrc:

"source $VIM/mswin.vim

set nobackup            " don't create backup files
"set nohlsearch          " don't highlight search results
set incsearch           " incremental search
set ignorecase          " ignore case during search
set smartcase           " do case sensitive search when upper case is typed
set shiftwidth=4        " shift blocks in 4 space increments
set tabstop=4           " the (typewriter) tab stops are every 4 spaces
set smarttab            " tab at beginning of line inserts 4 spaces (shiftwidth)
set showmatch           " briefly highlight matching bracket
set matchtime=1         " highlight match for 0.1 seconds
set background=dark     " black background
set shiftwidth=4        " shift blocks by 4 spaces
set shortmess=at        " use short messages so we don't have to hit enter
set expandtab           " expand tabs into spaces
set scrolloff=5         " always show 5 lines before & after cursor
set shell=bash          " use bash for the command shell
set shellcmdflag=-lc    " make bash startup as a login shell for ~/.bashrc
"set nowrap              " do not wrap long lines
set visualbell          " flash the screen rather than beep
set noerrorbells        " don't beep on errors
set wildmenu            " enable filename tab completion
set ruler               " display the ruler: shows cursor position
set cpoptions+=$        " display '$' at end of cw sequence
set title               " display filename and vim info in window title bar
set modelines=5         " search the first and last 5 lines for "vim:" modeline

set wildignore=*.o,*.class,*.pyc,*.pyo,*.a,*.so,*.dll,*.exe,core,*.jar,*.zip

"let loaded_matchparen = 1 " don't highlight parenthesis match

filetype indent on

if v:version > 700
    set cursorline          " underline the current line

if &t_Co > 2 || has( "gui_running" )
    syntax on

if has( 'mouse' )
    set mouse=a " enable mouse

if has( "autocmd" )
    " open the file in the last place we were editing
    autocmd BufReadPost *
        \ if line("'\"") > 1 && line("'\"") <= line("$") |
        \     exe "normal! g'\"" |
        \ endif

"folding settings
set foldmethod=indent   "fold based on indent
set foldnestmax=10      "deepest fold is 10 levels
set nofoldenable        "dont fold by default
set foldlevel=10        "this is just what i use

Interpreter vs Script


>>> import os
>>> os.uname()
('Linux', '', '2.6.18-194.8.1.el5', '#1 SMP Thu Jul 1 19:07:06 EDT 2010', 'i686')


#!/usr/bin/env python
import os
print os.uname()

Web Development

WebProgramming - PythonInfo Wiki -

This topic guide attempts to cover every aspect of programming Web applications (both clients and servers) using Python.

HOWTO Use Python in the web — Python v2.7.2 documentation -

This document shows how Python fits into the web. It presents some ways to integrate Python with a web server, and general practices useful for developing web sites.

Plug Simple Python Web App

-------- Original Message --------
Subject: 	Re: Writing a simple python web app
Date: 	Mon, 25 Oct 2010 19:03:31 -0400
From: 	Roberto Mello <>
To: 	Provo Linux Users Group <>

On Sat, Sep 11, 2010 at 5:40 PM, Dave Smith <> wrote:
> I'm creating a very simple web application, and I want to use python. My first guess was that mod_python
would provide the easiest entry point. Boy, was I wrong. All the mod_python tutorials spend 80% of their
time extolling the virtues of mod_python over CGI, but they are quite lean on specific examples. It's quite
disappointing. A natural progression then leads me to mod_wsgi, and since that has equally poor
documentation, to Django. However, Django is overkill for my app (I don't even have a database).

For something as simple as you have described, and to run on an
embedded platform, I would probably stay away from frameworks designed
to run on full hardware, and go with something more minimalistic.

Take a look at CherryPy [1] or [2].


Roberto Mello


See also

See also CherryPy

See also Django

Apache CGI


Great tutorial:

My favorite option, make all .cgi files executable:

 <Location />
   AddHandler cgi-script .cgi 
   Options +ExecCGI 

To enable on Ubuntu:

sudo a2enmod cgi
service apache2 restart

Directory index:


Or if you really want to go crazy, this will try to run *every* file: [107]

    # this has problems with / directory being run as a script, instead of index too!
    #<Location />
    #    SetHandler cgi-script
    #    Options +ExecCGI

    <Files ~ ".">
        SetHandler cgi-script
        Options +ExecCGI


    <Files ~ "^.[a-zA-Z]*$">
        SetHandler cgi-script
        Options +ExecCGI

Or just have index file:

   <Files "index">

Note: Place within a <VirtualHost> section if needed.

Note: Make sure to 'chmod +x' the files.

Start all HTML output with:

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

If you want just text output:

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

GET (from Environment):

print "<pre>"
for env in os.environ:
    print "%s = %s" % (env, os.environ[env])
print "</pre>"
# sample for:

QUERY_STRING = var1=ken&var2=barbie
PATH_INFO = /test


POST (from stdin):

post = sys.stdin.readline()
if post:
    print post
CONTENT_TYPE = application/x-www-form-urlencoded<br>


cgi helper class:

import cgi

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

form = cgi.FieldStorage()

# field keys
print "fields keys: ", form.keys()  # list of field keys

# fild values
if "name" in form:  # if form.has_key('name'):
    print "field 'name' is: ", form["name"].value
    print "field 'name' is: ", form.getvalue('name')

# headers
print "dictionary of headers: ", form.headers
print form.type
print form.headers['content-type']

To prevent XSS escape the characters:

 print cgi.escape(form.getvalue('name'))
import cgi

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

form = cgi.FieldStorage()
if (form.has_key("action") and form.has_key("name") \
        and form.has_key("age")):
    if (form["action"].value == "display"):
        print "%s = %s" % (form["name"].value form["age"].value)

Note: cgi helper class also does sessions and cookies.


See also:


REST Example:

import os, re
def get_rest():
    if not "PATH_INFO" in os.environ:
        return ()
    # filter for garbage
    #print os.environ["PATH_INFO"]
    path_info = re.findall('[/A-Za-z0-9_-]*', os.environ["PATH_INFO"])
    if not path_info:
        return ()
    path_info = ''.join(path_info)
    # collapse empty paths
    paths = path_info.split('/')
    final_paths = []
    for path in paths:
        if path:
    return tuple(final_paths)
rest = get_rest();


AVOID! AVOID! AVOID! - if you want this simple, just use CGI!

See Apache mod_python

mod_python -


yum install mod_python




"People coming from PHP often find it hard to grasp how to use Python in the web. Their first thought is mostly mod_python, because they think that this is the equivalent to mod_php. Actually, there are many differences. What mod_python does is embed the interpreter into the Apache process, thus speeding up requests by not having to start a Python interpreter for each request. On the other hand, it is not “Python intermixed with HTML” in the way that PHP is often intermixed with HTML. The Python equivalent of that is a template engine. mod_python itself is much more powerful and provides more access to Apache internals. It can emulate CGI, work in a “Python Server Pages” mode (similar to JSP) which is “HTML intermingled with Python”, and it has a “Publisher” which designates one file to accept all requests and decide what to do with them.
mod_python does have some problems. Unlike the PHP interpreter, the Python interpreter uses caching when executing files, so changes to a file will require the web server to be restarted. Another problem is the basic concept – Apache starts child processes to handle the requests, and unfortunately every child process needs to load the whole Python interpreter even if it does not use it. This makes the whole web server slower. Another problem is that, because mod_python is linked against a specific version of libpython, it is not possible to switch from an older version to a newer (e.g. 2.4 to 2.5) without recompiling mod_python. mod_python is also bound to the Apache web server, so programs written for mod_python cannot easily run on other web servers.
These are the reasons why mod_python should be avoided when writing new programs. In some circumstances it still might be a good idea to use mod_python for deployment, but WSGI makes it possible to run WSGI programs under mod_python as well."



"The Web Server Gateway Interface, or WSGI for short, is defined in PEP 333 and is currently the best way to do Python web programming. While it is great for programmers writing frameworks, a normal web developer does not need to get in direct contact with it. When choosing a framework for web development it is a good idea to choose one which supports WSGI."

See mod_wsgi


See Django


See CherryPy makes web apps

Think about the ideal way to write a web app. Write the code to make it happen. : Python Package Index -

Home Page:

"Django lets you write web apps in Django. TurboGears lets you write web apps in TurboGears. lets you write web apps in Python."

— Adam Atlas

NOTE: Can modify source code without having to restart app! :-)


PIP Install:

pip install


Hello World: [108]

import web
urls = (
    '/(.*)', 'hello'
app = web.application(urls, globals())

class hello:        
    def GET(self, name):
        if not name: 
            name = 'World'
        return 'Hello, ' + name + '!'

if __name__ == "__main__":

Note: can be renamed.



Note: You can add an IP address/port after the "" bit to control where launches the server. You can also tell it to run a fastcgi or scgi server.

python 1234



# templates/hello.html
$def with (name)

$if name:
    I just wanted to say <em>hello</em> to $name.
    <em>Hello</em>, world!
app = ...
render = web.template.render('templates/')


name = 'Bob'    
return render.hello(name)  # render.[TEMPLATE_NAME](vars)

get variable: (/?name=Joe)

i = web.input(name=None)
return render.hello(

variable in url:

urls = ('/(.*)', 'hello')
class index:
    def GET(self, name):
       return render.hello(name)
# visit: /Joe

multiple variables in url:

urls = ('/(.*)/(.*)', 'hello')
 class hello:
   def GET(self, fname, lname):
       return render.hello(fname, lname)
# visit: /Joe/Smith


lpthw.web: a fork of for Learn Python The Hard Way

Locks at a specific version and removes some magic. Thanks to Aaron Swartz for making it originally.

lpthw.web: a fork of for Learn Python The Hard Way -

Home Page:

This conflicts with, so only install one.


web - Web modules for CGI and WSGI web programming

web : Python Package Index -

Warning: This is an alpha release for those who are interested in

   checking progress. No documentation and not for production use.

Auth, session, mail, form and templating tools for writing CGI scripts or WSGI web applications.

This version incompatible with previous versions becuase large portions of the code have been refactored into separate components.


Hide DeprecationWarning

Import DeprecationWarning


/opt/xmpppy-0.4.1/xmpp/ DeprecationWarning: the sha module is deprecated; use the hashlib module instead
  import sha,base64,random,dispatcher,re
/opt/xmpppy-0.4.1/xmpp/ DeprecationWarning: the md5 module is deprecated; use hashlib instead
  import md5

Hide deprecation warnings with code: [109]

import warnings

with warnings.catch_warnings():
    import md5, sha

Hide with header:

#!/usr/bin/env python -W ignore::DeprecationWarning

Hide from command line:

python -W ignore::DeprecationWarning


Funciton DeprecationWarning


/opt/xmpppy-0.4.1/xmpp/ DeprecationWarning: socket.ssl() is deprecated.  Use ssl.wrap_socket() instead.
  tcpsock._sslObj    = socket.ssl(tcpsock._sock, None, None)

Hide with header:

#!/usr/bin/env python -W ignore::DeprecationWarning

Hide from command line:

python -W ignore::DeprecationWarning