Xman Blog Inside

Monday, September 11, 2006

Python Core Language Quick Reference

If you can learn a language by example, this is a place for you to learn the core of python programming. However, some statements are mis-indented. But indentation should not be a problem to you if you are familiar with structured programming.

# Getting started.
>>> print "hello world"
hello world

xman@sai python $ cat hello.py
#!/usr/bin/python
# hello world comment
print "hello world"

>>> execfile("hello.py")
hello world

>>> import sys
>>> sys.exit()
xman@sai python $

>>> while x <= y:
... print x,y
... x=x+1
>>> x = 0 ; y = 1 ; z = 2
>>> print "%4d %0.2f" % (x,y)
100 5.00

# Conditional.
>>> if x < z =" x"> y:
... z = y
... elif x == y:
... pass
... else:
... raise RuntimeError, "Unknown"
...

# Keywords: and assert break class continue def del elif else except exec finally for from global
if import in is lambda not or pass print raise return try while
# Operators: + - * ** / % << >> < > <= >= == != <> & | ^ ~ not and or

# File.
>>> f = open("hello.py")
>>> line = f.readline()
>>> while line:
... print line
... line = f.readline()
...
>>> f.close()

# File methods
f.read([n])
f.readline()
f.readlines()
f.write(s) # Write string s.
f.writelines(l) # Write all strings in list l.
f.close()
f.tell()
f.seek(offset [,where])
f.isatty()
f.flush()
f.truncate([size])
f.fileno()
f.readinto(buffer, nbytes)

# String & Char
>>> a = "hello world"
>>> print a[4]
o
>>> a = 'hello world'
>>> b = "hello world"
>>> c = """hello world"""
>>> d = """hello
... world
... """

>>> print d
hello
world

# Slice
>>> x = a[0:5]
>>> y = a[1:]
>>> z = a[:4]

# String
>>> a = "hello" ; b = "world"
>>> c = a + b ; print c
helloworld

# List
>>> data = ["a", "b", "c"]
>>> data.append("d")
>>> data = data + ['e','f']

# Reference count
>>> a = [1,3,5]
>>> b = a
>>> a, b
([1, 3, 5], [1, 3, 5])
>>> b[1] = 100
>>> a, b
([1, 100, 5], [1, 100, 5])

# Nested list.
>>> data = ['a' , ['b','c' ]]
>>> print data[1][1]
c

# Map.
>>> import string
>>> import sys
>>> f = open("data.dat")
>>> svalues = f.readlines()
>>> f.close()
>>> fvalues = map(string.atof, svalues)
>>> print svalues
['0.1\n', '0.4\n', '0.2\n', '0.3\n', '0.24\n']
>>> print fvalues
[0.10000000000000001, 0.40000000000000002, 0.20000000000000001, 0.29999999999999999, 0.2399999999
9999999]
>>> print min(fvalues)
0.1
>>> print max(fvalues)
0.4

# Tuple (Create once, no modifications allowed)
>>> a = (1,3)
>>> b = (2,4)
>>> c = (4,)
>>> print a,b,c
(1, 3) (2, 4) (4,)
>>> print a[0], a[1]

# Range.
>>> print range(3)
[0, 1, 2]
>>> print range(1,10)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> print range(1,10,2)
[1, 3, 5, 7, 9]
>>> for i in range(1,10,2):
... print i
...
1
3
5
7
9
a = xrange(0,10000000) # Recompute to save memory.

# Dictionary (associative array).
>>> a = {
... "a" : "va" ,
... "b" : "vb"
... }
>>> print a["a"], a["b"]
>>> a.keys()
>>> a.has_key("a")
>>> del a["a"]

# Function. Local scope by default.
>>> def swap(a,b,c=0):
... "SWAP: Documentation string"
... global msg
... if c != 0 : print msg
... return (b,a)
...
>>> a,b=swap(a,b)
>>> a,b=swap(a,b,1)

# Variable-length argument, keyword argument.
# args: Additional variables in a tuple.
# kw: Additional keyword arguments in a table.
>>> def func(x, *args, **kw):

# Class.
# def: Define a method.
# First argument of a method: the object.
>>> class stack:
... "Stack data structure"
... name = "Stack"
... def __init__(self):
... self.stack = []
... def push(self,object):
... "Append object to the end"
... self.stack.append(object)
... def pop(self):
... "Remove an object from the end and return it"
... return self.stack.pop()
... def length(self):
... "Number of objects in stack"
... return len(self.stack)
>>> s = stack()
>>> s.push("data1")
>>> s.push("data2")
>>> x = s.pop()

# Inheritance.
>>> class C(A,B):
# Encapsulation.
>>> class C:
... __x = 500

# Class relationship
>>> isinstance(c,C)
>>> issubclass(C,A)


# Exception.
>>> try:
... f = open("data.txt")
... except IOError, e:
... print "My error:" , e
...
My error: [Errno 2] No such file or directory: 'data.txt'

# Module.
# Import the module hello.py.
>>> import hello
>>> import string
>>> dir(string) # List contents of module string.

# Syntax.
# Multi-line.
>>> c = a + \
... b

# Consistent indentation.
# Codes in the same block must uses the same indentation.

# Documentation strings.

>>> def func(n):
... "Documentation string"
... return n

# Type & ID
>>> type(a)

>>> id(a)
5280552
>>> a is b
>>> a is not b
>>> type(a) == type(b)

# Copy.
>>> import copy
>>> b = [1,2,3]
>>> a = copy.deepcopy(b)

# List:
list(s)
s.append(x)
s.extend(x)
s.count(x)
s.index(x)
s.insert(i,x)
s.pop([i])
s.remove(x)
s.reverse()
s.sort([cmpfunc])

# Mapping types:
len(m)
m[k]
del m[k]
m.clear()
m.copy()
m.has_key(k)
m.items()
m.keys()
m.update(b)
m.values()
m.get(k,[,f]) # Return m[k] if found; otherwise return f.

# Unbound method object, bound method object.
# C is a class, c is an instance of class C.
>>> c.work()
hello world
>>> C.work(c)
hello world

# Functional programming
>>> a = lambda x,y : x+y
>>> print a(2,5)

# map(), reduce(), filter().
# Apply func to elements in s.
>>> t = map(func,s)
# Reduce a using sum function.
>>> b = reduce(sum, a)
# Filter objects which satisfy the conditions.
>>> b = filter(lambda x: x < style="font-weight: bold;"># Execution.
# eval(str, [,globals [,locals]])
>>> c = eval('a+b')
>>> exec "print 4+3"
# execfile(filename [,globals [,locals]])
>>> execfile("hello.py")
# compile(str,filename,kind)
>>> c = compile(str, '', 'single')
>>> c = compile(str, '', 'exec')
>>> c = compile(str, '', 'eval')

# Input, Output
>>> s = raw_input("type something")
>>> c = sys.stdin.read(1)
>>> sys.stdout = open("output.txt", "w")

# Serialize
>>> f = open("save.dat", "w")
>>> pickle.dump(a,f)
>>> pickle.dump(b,f)
>>> f.close()
>>> f = open("save.dat", "r")
>>> obj = pickle.load(f)
>>> obj
2

# Built-in Functions
abs(x)
apply(func [,args [,keywords]])
buffer(object [,offset [,size]])
callable(obj)
chr(i)
cmp(x,y)
coerce(x,y)
compile(string, filename, kind)
complex(real [,img])
delattr(obj, attr)
dir([obj])
divmod(a,b)
eval(expr [, globals [,locals]])
execfile(filename [,globals [,locals]])
filter(function, list)
float(x)
getattr(obj,name)
globals()
hasattr(obj,name)
hash(obj)
hex(x)
id(obj)
input([prompt])
intern(string)
isinstance(obj,class)
issubclass(subclass,class)
len(s)
list(s)
locals()
long(x)
map(function, list, ...)
max(s [,args, ...])
min(s [,args, ...])
oct(x)
open(fname [, mode [, bufsize]])
ord(c)
power(x, y, [,z])
range([start,] stop [,step])
raw_input([prompt])
reduce(func, seq [,initializer])
reload(module)
repr(obj)
round(x [,n])
setattr(obj, name, value)
slice([start,] stop [,step])
str(obj)
tuple(s)
type(obj)
vars([obj])
xrange([start,] stop [, step])

# Built-in Exceptions
Exception
StandardError
ArithmeticError
LookupError
EnvironmentError
AssertionError
AttributeError
EOFError
FloatingPointError
IOError
ImportError
IndexError
KeyError
KeyboardInterrupt
MemoryError
NameError
NotImplementedError
OSError
OverflowError
RuntimeError
SyntaxError
SystemError
SystemExit
TypeError
ValueError
ZeroDivisionError

# Internal.
>>> a = [1,4,6]
>>> a.__doc__
"list() -> new list\nlist(sequence) -> new list initialized from sequence's items"
>>> class C:
... a = 0
... def work(self):
... print("hello world")
...
>>> c = C()
>>> c.__dict__
{}
>>> C.__dict__
{'a': 0, '__module__': '__main__', 'work': , '__doc__': None}
# There are many other built-in attributes for classes, instances, modules ...

Reference: Python Essential Reference by David M. Beazley

Labels: ,

Friday, September 08, 2006

Make Quick Reference

Inside Makefile:
TARGET: DEPENDENCY LINE OR RULES LINE
COMMAND
COMMAND
...
A command line (with TAB in front) is running in a subshell by itself.
# A comment.
name = text in nameMacro definition.
$ASingle character macro reference.
$(name) or ${name}Macro references.
make -pPrint internally defined macros.
make target "DIR=/dir /dir2"Use DIR defined in command even if you defined DIR in your Makefile!
DIR=/bin make targetUse DIR in Makefile if any, otherwise use DIR=/bin.
make -eShell variables have higher priority than file macro definitions.
FILE=a.c b.c
@echo ${FILE:.c=.o}
a.o b.o
Replace suffix .c by .o. The @ suppress the printing of commands executed.
$@Current target. Use in command line only.
$?List of pre-requisites newer than current target. Use in command line only.
$$@Current target. Use in dependency line only.
$*File name without suffix part. Use in commands in suffix rule only.
$%Name of the .o when target is a library module.
$<The pre-requisite file. Use in commands in suffix rule only.
.SUFFIXES: .o .cSuffixes that make will consider.
.c.o:
${CC} -c $<
Suffix rule. To produce .o from a .c.
t: libm.a(sin.o)Target t depends on sin.o in libm.a. Hence, if sin.o is newer than libm.a, implicit rule replace newer sin.o into libm.a, then create target t.
libm.a :: sin.c
libm.a :: cos.c
Rebuild libm.a using different commands depends on the pre-requisites.
- rm -f *.oIgnore errors in the command rm, hence, errors will not stop the make process.
t: a.c \
b.c
Use '\' to continue with the next line.
.SUFFIXES:
.SUFFIXES: .a .b
.SUFFIXES: .c
1. Clear implicit suffixes. 2. Define .a .b suffixes. 3. Add .c into the suffix list.
.c.a:
true
Override default suffix rule with null command
t:
echo $$HOME
Use $$ in commands for environment variable.
.IGNOREForce make to ignore errors and keep going.
${<D}, ${<F}Pre-requisite file directory, pre-requisite file name.
VPATHView path.
include inc.mkInclude the file inc.mk.
.PRECIOUS: a.x b.xDo not let make deletes files a.x and b.x.
.SILENTDo not echo commands while executing.
.DEFAULT:
echo "hello"
Define a default action.

Reference: C Programming Utility: Managing Projects with make by Andrew Oram and Steve Talbott

Labels: ,

Wednesday, September 06, 2006

Gawk Quick Reference

By default, a record is a line. A line is made up of fields with default delimiter. Main programs are mainly based on C program syntax.
BEGIN { FS="\t+" }Initialization. Set field separator to be one or more TABs. Take the string as regular expression if more than 1 characters.
END { ... }Finalization. Execute ... after all files have been processed.
gawk -F"\t" '{ ... }'Execute with field separator TAB.
/xyz/ { print }For each line contain xyz, print the line.
$1 == 100 { print $2, $3 }For each line with first field equals 100, print the 2nd and 3rd fields separated by space.
$3 ~ /PAT/ { print $2 $3 }If the 3rd field matches PAT, print the concatenated 2nd and 3rd fields.
$3 !~ /PAT/ { x = 0 }If the 3rd field doesnt match PAT, let x = 0.
print $1 OFS $2Print 1st and 2nd fields separated by output field separator.
FS, OFS, RS, ORSField & output field separator, record & output record separator.
NF, NRNumber of fields and records.
FILENAMECurrent input file.
$NFLast field
'${val:-hello}'In matching region, represents value of $val from the BASH environment, but use "hello" if val is not defined.
"'${val}'"In code region, represents value of $val from the BASH environment.
array[2]="hello"
array["i"]="world"
for(i in array) print array[i]Also works for multi-dimensional array.
if("i" in array) print "found"Print if "i" is a subscript of array.
n = split($1, array, ":")
for(i = 1; i <= n ; i++) print array[i]
Split 1st field into array with ":" as delimiter.
array[2,5]="val25"Equivalent to array["2" SUBSEP "5"]="val25". SUBSEP is a subscript-component separator such as "\034" by default.
if((i,j) in array)
ARGC, ARGV, ENVIRONNumber of arguments, argument array, and environment array.
index(s,t)Position of t in s.
length(s)
sub(r,s[,t])Substitute first match of r in t by s. t is $0 by default.
match(s,p)Return starting position of the substring in s that regular expression p matches.
sprintf("fmt",expr)
substr(s,p,n)The substring of s at position p up to n long.
toupper(s), tolower(s)
function name(list) {
statement
}
getlineGet next line.
getline <"-"Get a line from stdin.
print > "out.txt"Print to the file out.txt.

Reference: UNIX Power Tools: sed & awk by Dale Dougherty

Labels: ,

Monday, September 04, 2006

Sed Quick Reference

Generic command format:
[address]command
command = d, a, i, c, r, w, q, ... d: Delete. a: Append. i: Insert. c: Change. r: Read. w: Write. q: Quit.

Group command format:
address{
command1
command2
command3
}
Substitution format:
[address]s/pattern/replacement/flags
flags = n, g, p, or w file. n: A number n represents nth occurrence. g: Global. p: Print. w file: Write to file.

Transform format:
[address]y/abc/xyz/
Translate a->x, b->y, and c->z.

[address] can be lines found by regular expression matches, range of lines.
s/abc/xyz/pReplace abc by xyz, and print (required 'p' if we use "sed -n" to suppress printing all lines).
s/abc/pqr/
s/pqr/xyz/
Replace abc by pqr, then replace pqr by xyz. Effectively, abc and pqr becomes xyz.
/abc/s/pqr/xyz/gFind lines with abc, then replace pqr in the line by xyz. 'g' -> Replaces all pqr in the line.
1dDeletes the first line.
$dDeletes the last line.
/^$/dDeletes blank lines.
3,10dDeletes line 3 to line 10 inclusive.
3,10!dDeletes all lines except line 3 to line 10 inclusive.
10,$dDeletes line 10 to last line inclusive.
\(abc\)\+Match one or more abc. Regular expression needs '\'.
abc+Match literal abc+.
# A commentComment.
s!abc!xyz!Replace abc by xyz, but using ! as delimiter.
a\abcAppend abc to the matching line.
i\abcInsert abc before the matching line.
c\abcChange the matching line to abc.
=Print line with line number.
nOutput the contents of the pattern space and read next line.
r fileRead file.
w fileWrite file.
&Represent the regular expression matching string (used in replacement string).
\3Match 3th substring in the regular expression using \( \).
s/\(a\)\(b\)/\2\1/Replace ab by ba.
s/abc/xyz/2Replace the 2nd abc by xyz.
There are other commands like multi-line commands (N, D, P), copying/appending pattern space to hold space vice versa (h,H,g,G,x), label(:labelname), branch to label (b), conditional branch (t).

Reference: UNIX Power Tools: sed & awk by Dale Dougherty

Labels: ,

Regular Expression Quick Reference



.Match single character except newline.
x*Match zero or more x.
[xyz]Match a character from x, y or z.
[^xyz]Match any single character except x, y, or z.
[a-zA-Z0-9]Match a character from a to z, A to Z, or 0 to 9 inclusive.
^xMatch x at beginning of the line.
x$Match x at end of the line.
x\{2,4\}Match 2 to 4 x inclusive.
x+Match one or more x.
x?Match zero or one x.
x|yMatch either x or y.
\Escapes the special character e.g. \t => TAB, \n => newline.
()Groups regular expressions e.g. (x|y)+.


Reference: UNIX Power Tools: sed & awk by Dale Dougherty

Labels: ,