Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Nov 15, 2008

The Zen of python

PEP is short for Python Enhancement Proposals, and PEP0 is the index of these files.
http://www.python.org/dev/peps/pep-0000/

The Zen of python -- May be simple, but practical. It is in PEP20.

    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!

And I think Simple is beautiful.

I like python, for its liquid, simple and prowerful.

Oct 15, 2008

Learning python 读书笔记 PART 6

PART 6: Classes and OOP
Chapter 19. OOP: The Big Picture
classes: instance factories, data + function
Instances: represent the concrete items in a program's domain
attributes searching at runtime, lower level to higher level
Coding Class Trees
Class C1(C2, C3): multiple inheritance
Attributes --> class  :  assignments made within class statements
Attributes --> instances  :  assignments to self inside class statements
>>> class C1:
    def setname(self, who):
        self.name = who       
>>> I1 = C1()
>>> I1.setname('xyz')
>>> print I1.name
xyz
initialization: def __init()

Chapter 20. Class Coding Basic
Class: namespace, generating multiple objects, namespace inheritance, operator overloading
Module: namespace
Classes VS. Instances
Class objects probide default behavior:
attributes functions --> class <-- a class name
Instance Objects are concrete items:
call a class object like a function makes a new instance object
each instance object inherits class attributes and gets its own namespace
a new attribute can be generated on the instance
Classes are customized by Inheritance
classes and modules:
from module1 import Class1
class Class2(Class1): ...
Classes can intercept Python Operators
def __init__(self, value): ...
def __add__(self, other):
    return ...
def __mul__(self. other):
    self.data = ...

Chapter 21. Class Coding Details
The Class Statement
class <name>(superclass,...):
    data = value
    def method(self, ...):
        ....
Methods
instance.method(args...) == class.method(instance, args ...)
Calling superclass constructors:
class Sub(Super):
    def __init__(self, x):
        Super.__init__(self, x)
        ......
Inheritance
Abstract superclasses:
class Super:
    def method(self): ...
    def delegate(self):
        self.action()
    def action(self):
        assert 0, 'action must be defined!'
Operator Overloading
__init__: Constructor
__del__: destructor
__add__, __radd__, __iadd__: +, right-side + , +=
__or__:  |
__repr__,__str__ : printing
__call__ : X()
__getattr__ : X.a
__setattr__ : X.a = value
__getitem__ : X[key], for loops, in tests
__setitem__ : X[key] = vlaue
__len__
__cmp__, __lt__, __eq__
__iter__
user-defined iterators
def __init__(self, start, stop):
def __iter__(self)
    return self
def next(self)
    ....
__getattr__ and __setattr__ catch attribute references
__getattr__: is not called if Python can find the attribute, used to deal with undefined attributes
__setattr__: if defined, self.attr = value --> self.__setattr__('attr', value)
    self.__dict__['name'] = x
__repr__ VS. __str__
>>>x  :  run __repr__
>>>print x  :  run __str__
add:
>>>x + 1 #__add__
>>>1 + y #__radd__
__call__:  x(attr)
__del__: run automatically when an instance's space is being reclaimed
Namespaces: The Whole Story
instance -- __class__ --> class
class -- __base__ --> higher superclasses

Chapter 22. Designing with Classed
Python and OOP
inheritance polymorphism encapsulation
Overloading by Call Signatures
*class C:
     def meth(self, x): ...    #a
     def meth(self, x, y):...  #b
 a will overwrited
*class C:
     def meth(self, *args):
          if len(args) == 1: ...
*class C:
     def meth(self, x):
          x.operation()
Classed as Records
OOP and Inheritance: "is-a" relationship
OOP and Composition: "has-a" relationship
OOP and Delegation
Multiple Inheritance
search process depth-first all the way to the top
Classes are objects: Generic Object Factories
apply function:
def factory(aClass, *args)
    return apply(aClass, args)
Methods are objects: Bound or Unbound
Unbound: class, no self
object1 = Spam()
t = Spam.doit
t(object1, 'lalal')
bound: instance, self + function pairs
object1 = Spam()
x = object1.doit
x('lalal')
Documentation String Revisited
import docstr
a.__doc__, a.b.__doc__
Class Versus Modules
they are both namespacefiles
Modules:
Are data/logic packages
Are created by writing Python or C extensions
Are used by imported
Classes:
Implement new objects
Are created by class statements
Are used by being called
Always live within a module

Oct 9, 2008

Learning python 读书笔记 PART 5

PART 5: MODULES
Chapter 15. Modules: The Big Picture
a.code reuse b.System namespace partitioning c.Implementing shared services or data
Python Program Architecture
import and from statements execute and load another file at runtime
How Imports Work
Find it --> Compile it(Maybe) --> Run it
Find it
The module search path:

a. home directory of the top-level file
b. PYTHONPATH directories (if set)
c. Standard library directories
d. The contents of any .pth files (if present)
current working directory
sys.path: the actual search path, is configured by Python at program startup
Compile it
.py & .pyc
Run it
import operation executes the byte code of the module
def: run at import time to create functions

Chapter 16. Module Coding Basics
Module Creation
module --import--> variables (name)
they should follow the normal variable name rules
<directory>m.<extension>
import a --> a.b
from a import b --> b
from a import * --> b
imports happen only once
Later imoort operations simply fetch an already loaded module object.
def, import, from are executable statements
import and from are assignments!
from small import x,y
x = 42 #change my x only
import small
small.x = 42 #change x in other module
equivalence from module import name
is euqal to
import module
name = module.name
del module
Module Namespace
attributes --namespace--> module ojbect
Module statements run on the first imoort: create an empty module object and execute the statements inside the module
Top-level assignments create module attributes
Module namespace: attribute__dict__, or dir(M) --> a directory object
Attribute Name Qualification
Import Versus Scope: function <--> module
Namespace Nesting
Reloading Modules
reload currently only works on modules written in Python; C extension modules cannot.
Reload Basics:
Reloads impact all clients that use import to fetch modules
Reloads impact future from clients only

Chapter 17. Module Package
Package import Basics
import dir1.dir2.mod --> dir0/dir1/dir2/mod
__init__.py
package initialization: run automatically
module namespace initialization
from* statement behavior
from Versus import with packages
import dir1.dir2.mod as mod #short name
from dir1.dir2 import mod

Chapter 18. Advanced Module Topics
_X: prevent being copied out in from* statement, but still not import statement
__all__: from* will only copy out those names listed in the __all__ list
can be used in __init__.py
__all__ = ["b","c"]
from __future__ import featurename
__name__ and __main__
run at top level: __name__ <-- "__main__"
if __name__ == '__main__'
tester()
from module import longname as name
Module Design Concept
you're always in a module in python
Minimize module coupling: global variables
Maximize module cohesion: unified purpose
Module should raraly change other module's variables

Oct 5, 2008

Learning python 读书笔记 PART 4

PART FOUR. FUNCTIONS
Chapter 12. Function Basics
calls:  myfunc(argv)
def, return, yield: def adder(a, b=1, *c): return a+b+c[0]
global:  def function(): global x; x = 'new'
lambda:  funcs = [lambda x: x**2, lambda x: x*3]
1. Coding Functions
def is a executable statement which create an object and assigns it to a name
arguments are passed by assignment
global declares module-level variables that are to be assigned
def <name> (arg1, arg2...):
    ...
    return <value>
def can executes at runtime
if test:
    def function():
else:
    def function():
2. Definitions and Calls
Polymorphism in Python: because python is a dynamically typed language, every operation is polymorphic in python
in python, code is not supposed to care about specific data types.

Chapter 13. Scopes and Arguments
1. Scope Rules
Name <--> Namespace
The enclosing module is a global scope
The global scope spans a single file only
Each call to a function is a new local scope
Assigned names are local, unless declared global
All other names are enclosing locals, globals, or built-ins
LEGB: local --> enclosing functions --> global --> built-in
the built-in scope
>>> import __builtin__
>>> dir(__builtin__)
zip <--> import __builtin__,  __builtin__.zip
2. The global Statements
3. Scope and Nested Functions
LEGB: statically nested scope
lambda: an expression that generates a new function to be called later, much like def statement
4. Passing Arguments
immutable arguments <--> C's "by value" mode
mutable arguments <--> C's "by pointer" mode
Avoiding mutable arguments change:
pass a copy or tuple
Simulating output parameters:
>>> def multiple(x,y):
    x = 2
    y = [3,4]  #change local names only
    return x,y #return new values in a tuple(not two values)
>>> x = 1
>>> y = [1,2]
>>> x,y = multiple(x,y)  #assign result to caller's names
>>> x,y
(2, [3, 4])
5. Special Argument Matching Modes
func(value)    : Normal argument: matched by position
func(name=value)    : Keyword argument: matched by name
self documention, more meaningful
def func(name)   
def func(name=value)    : default argument value, if not passed in the call
make some arguments optional
def func(*name)    : Matchs remaining positional args(in a tuple)
>>>def f(*args): print args
def func(**name)    : Matchs remaining keyword args(in a dict)
>>>def f(**args): print args
>>>f(a=1,b=2)
{'a':1, 'b':2}
Argument Matching
rules:
key arguments after non-keyargument
*name after normal argument
**name last
Matching steps:
non-key argus --> key word argus --> extra non-key argus --> extra key argus -->default values

Chapter 14. Advanced Function Topics
1. Anonymous Functions: lambda
lambda expression creates a function to be called later, but returns it instead of assigning it to a name
lambda argu1, argu2, argu3...arguN: expression using argus
lambda bodies are single expression, not a block of statements
example1:
L = [(lambda x: x**1),(lambda x: x**2), (lambda x: x**3)]
for f in L
    print f(2)
print L[0](2)
example2:
key = 'b'
{'a': (lambda f1),
  'b': (lambda f2),
}[b]()
is equivalent to
def f1():...
def f2():...
...
key = ...
{'a':f1, 'b':f2}[key]()
How(not) to obfuscate your python code
((a and b) or c) is roughly equivalent to:
if a: b else: c
(C's a?b:c)
2. Applying Functions to Arguments
The apply built-in
apply(func, argus) == func(argus)
argument list is passed by tuple
if <test>:
    action,args = func1, arguments_a
else
    action,args = func2, arguments_b
...
apply(action,args)
3. Functions Over Sequences
>>> counter = [1,2,3,4]
>>> def func(x): return x + 10
>>> map(func,counter)
[11, 12, 13, 14]
>>>map((lambda x: x + 3), counter)
[3,4,5,6]
map(func,[1,2,3],[4,5,6]) #[func(1,4),func(2,5),func(3,6)]
4. Functional Programing Tools
filter
>>>filter((lambda x: x>0), range(-5,5))
[1,2,3,4]
reduce
>>>reduce((lambda x, y: x+y), [1,2,3,4]) 
10
>>>import operator
>>>reduce(opetator.add, [1,2,3,4])
5. List Comperhensions
[x ** 2 for x in range(10)]
map((lambda x: x ** 2), range(10))
[x for x in range(5) if x%2 == 0]
filter((lambda x: x%2 == 0), range(5))
map((lambda x: x**2), filter((lambda x: x%2 == 0),range(10)))
the genaral structure of list comprehensions
[expression for target1 in sequence 1 [if condition]
                for target2 in sequence 2 [if condition]....
                for targetN in sequence N [if condition]]
list comperhension and map are faster than for loop.
6. Generators and Iterators
Generators yield a value rather than returning one. The yield statment suspends the function and sends a value back to the caller and allow the function to resume from where it left off.
>>> def gensqure(N):
    for i in range(N):
        yield i ** 2
>>>x=gensqure(10)
>>>x.next(), x.next()...
Iterators and built-in type: iter(L)
__iter__ method

Learning python 读书笔记 PART 3

PART THREE: Statements and Syntax
Chapter 8. Assignment, Expressions, and Print
program  are composed of  modules contains statements contain expressions create and process objects
1. Assignment Statements
a = 'a'
a, b = 'a', 'b'   #tuple assignment
[a,b] = ['a','b']    #List assignment
a = b = 'c'    #Mutiple-assignment
L = L + [3], L = L + [5,6]    #Concatenate(create a new object)
L.append(3), L.extend([4,5])    #Faster, but in-place(add at the and of memory)
L += [9,10]    #Mapped to L.extend([9,10]): the faster mothed
2. Expression Statements
3. Print Statements
print a,b,    #don't add newline at the end of text
print >>myfile a,b    #send data to myfile.write, not to sys.stdout.write
redirecting the Output Stream
import sys
sys.stdout = open('myfile', 'a')
print and stdout
class FileFaker:
    def write(self, string)  #do something with the string
import sys
    sys.stdout = FileFaker()

Chapter 9. if Tests
1. if stataments
if: elif: else:
if 1:
    print 'true'
else:
    print 'false'
switch & case:
*if choice = 'a':
    print 'aaa'
elif choice = 'b':
    print 'bbb'
else:
    print 'default'
choice = 'b'
*print {'a':'aaa','b':'bbb'}[choice]
*branch = {'a':'aaa','b':'bbb'}
  print branch.get('a', 'bad choice')
2. truth tests
2<3, 3<2   #Less-than: return 1 or 0
short circuit evaluation
2 or 3, 3 or 2   #(2,3) Return left operand if true. Else return right operand.
[] or 3   #3
[] or {}   #{}
2 and 3, 3 and 2   #(3,2) return left operand if false. Else return right operand.

Chapter 10. while and for Loops
1. while loops
while <test>:
     <statement1>
     if <test2>: break    #Exit loop now, skip now
     if <test2>: continue    #goto top of loop now   
else:
     <statement2>    #if we didn't hit break
breaks, continue, pass and the loop else
Emulating C while Loops

C assignments return the value assignment; Python assignments are just statements, not expressions
C: while ((x = next()) != NULL){process x...}
Python equivalent:
*while 1:
    x = next()
    if not x: break
    ...process...
x = 1
*while x:
    x = next()
    if x:
    ...process
2. for loops
for <target> in <object>:
    <statements>
else:
    <statements>  #if we didn't hit break
3. loop variations
range

Counter Loops
range(3)  #[0,1,2]
range(2,5) #[2,3,4]
range(0,10,2)  #[0,2,4,6,8]
*for item in X: print item
*i = 0
 while i < len(X)
     print x[i],; i += 1
*for i in range(len(x)): print X[i]
Nonexhaustive Traversals
S = 'abcdefghijk'
for i in range(0, len(s), 2) print S[i],  #manual for indexing
for i in S[::2]: print x
Changing Lists
for i in range(len(L)):
    L[i] += 1
zip and map
zip can enable us to visit multiple sequences in parallel
L1 = [1,2,3], L2 = [4,5,6]
zip(L1,L2)   #[(1,4),(2,5),[3,6]]
for (x,y) in zip(L1,L2):
    print x, y,
when argument lengths differ, zip choose the shortest length
map(None, L1, L2)
map choose the longest length and filled empty items with "None"
Dictionary Construction with zip
>>> keys = ['a','b','c']
>>> values = [1,2,3]
>>> zip(keys,values)
[('a', 1), ('b', 2), ('c', 3)]
>>> D = {}
>>> for(k,v)in zip(keys,values):
    D[k]= v   
>>> D
{'a': 1, 'c': 3, 'b': 2}
>>> D2 = dict(zip(keys,values))
>>> D2
{'a': 1, 'c': 3, 'b': 2}
File Scanners
file = open('test.txt', 'r')
while 1:
    line = file.readline()
    if not line: break
    print line
1. for line in open('test.txt').readlines(): print line
2. for line in open('test.txt').xreadlines(): print line
3. for line in open('test.txt'): print line
xreadlines loads lines on demand, "3" relies on iterators, is equivalent of xreadlines

Chapter 11. Documenting Python Code
1. the python document interlude

dir(): Lists of attributes  available on objects
__doc__: In-file documentation attached to objects
docstrings
User-defined docstrings: strings at the top of file, method and class
built-in docstring:
import sys
print sys.__doc__
print int.__doc__, print open.__doc__
pydoc
help(sys)
2. common coding gotthas
Don't forget the colons
Start in column 1
Blank lines matter at the interactive prompt:
mean the end of compound statement
Indent consistently
Don't coding C in Python:
don't embed assignment statements in while loop tests
Use simple for loop instead of while or range:
for loop is simple and fast
Don't expect results from functions that change objects in-place:
append(), sort(), append(), extend()
myList = myList.append(X)  #set myList to None
Always use parenthesis to call a function
Don't use extensions or paths in imports and reloads

Oct 4, 2008

Learning python 读书笔记(6, 7章)

Chapter 6: Lists and Dictionaries
1. List
Methods: grow, sort, search, reverse, append, extend, append
del L[x:y], L[x:y]=[a,b,c]
range(x), xrange(x,y), L.pop()
2. List in Action
Slice assignment: delete + insert
L.append(x) < -- > L+[x]
3. Dictionaries
D.keys(), D.values(), D.items():
create a new list
D.copy(), D.get(k, default),
D2.update(D1): merges the keys and values of one directory into another, blindly overwriting values of the same key.
D.has_key(k), k in D
D = dict(zip(keyslist, valueslist))
4. Dictionaries in Action
sequence operation don’t work.
Assigning to new indexes adds entries.
keys need not always be strings.(can be other immutable object)
Using dictionaries to simulate flexible lists Using dictionaries for sparse data structures
Matrix = {}
Matrix[(2,3,4)] = 88: the number at position (2,3,4) is 88
Using dictionaries as “reconds”
Dictionary interfaces
import anydbm
file = anydbm.open(“filename”)
file [‘key’] = ‘data’ #store data by key
data = file[‘key’] #fetch data by key

Chapter 7: Tuples, Files, and Everything Else
1. Tuples

simple groups of objects, cannot change in place(immutable), don't support any method calls
>>> (1,2) + (3,4)
(1, 2, 3, 4)
>>> (1,2) * 3
(1, 2, 1, 2, 1, 2)
>>> T = ('1', '2', '3')
>>> tmp = list(T)
>>> tmp
['1', '2', '3']
>>> T2 = tuple(tmp)
>>> T2
('1', '2', '3')
>>> T = (1, [2,3], 4)
>>> T[1][0] = 5 #works
>>> T[0] = 0 #Fails
2. Files
input = open('path', 'r'), output = open ('path', 'w')  #mode: r, w, a
S = input.read(), S = input.read(N) # N bytes
S = input.readline(), S = input.readlines()
output.write(S), output.writelines(L), output.close()
seek: reset current position in file, flush: buffered output write into disk
3. Type Categories Revisited
Operator Overloading:

class MySquence:
    def __getitem__(self, index):
        #called on self[index]
    del __add__(self, other):
        #called on self + other
4. Object Generality
Lists, dictionaries, and tuples can: 1. hold any kind of object 2. arbitrarily nested
Lists and dictionaries can dynamically grow and shrink.
5. References Versus Copies
No reference to the same object:

A = L[:]    #instead of: A = L (or list(L))
B = D.copy()    #instead of: B = D
empty-limit slices and copy method only do a top-level copy!
nested copy:
import copy
X = copy.deepcopy(Y)
6. Comparisons, Equality, and Truth
L1 == L2, L1 is L2    #Equivalent? Same object?
But short string: S1 = 'spam', S2 = 'spam' # S1==S2: true, S1 is S2: true
Numbers are true if nonzero
Other objects are true if nonempty
None: much like NULL in C
L = [None] * 100
bool: True, False

Oct 2, 2008

Learning python 读书笔记(1--5章)

PART ONE: Getting Started
Chapter 1: A Python Q&A Session
Chapter 2: How python runs programs
1. Source(x.py) --> Byte Code(x.pyc) --> runtime(PVM)
2. CPython, Jython and Python.net
Psyco: Just-in-Time Compiler
3. Frozen Binaries: program + PVM = binary executable program.
Py2exe(for windows), Installer(Linux & Unix), freeze(original)
Chapter3: How you run programs
1. Unix executable scripts: start with #!/usr/local/bin/python
2. raw_input trick: pause the script
3. import and reload
imports are too expensive an operation to repeat, so when we use reload() instead.
4. Attributes: import xx & from xx import yy. yy are the attributes in the python file xx.
5. IDE: IDLE, Komodo, PythonWorks, PythonWin, Visual Python(plugin for VS).
6. text editor: see http://www.python.org/editors

PATR TWO: Types and Operations
Chapter4: Numbers
1. programs  modules  statements  expressions.
2. Numbers: Normal integers(C longs); Long integers(unlimited size); Floating-point(C doubles);Octal and hex literals; Complex number literals.
3. NumPy(Numeric Python).
4. When use a variable that has never been assigned, Pythons report an error rather than a default value.
5. Numeric representation:
>>>b/a #Auto echo output: more digits
>>>print b/a #print rounds off digits.
>>>repr(num) #Used by echos: as cod form
>>>str(num) #Used by print: user-friendly form
6. Division: Classic, Floor and True
x/y: Classic division
x//y: Floor devision
>>>from __future__import division
7. >>>import math
>>>math.pi, math.e
8. >>>int(‘0100’), int(‘0100’, 8), int(‘0x40’, 16) #100, 64, 64
>>>oct(64), hex(64) #’0100’, ‘0x40’
>>>eval(‘100’), eval(‘0100’), eval(‘0x40’) #100, 64, 64
9. >>>a=3 # a: Names (reference) 3: Object
10. >>>L1 = [2, 3, 4]
>>>L2 = L1
>>>L1 = 24 #L2 is not changed
>>>L1[0] = 24 #L2 = [24, 3, 4]
Chapter 5: Strings
1. >>> s = ‘a\nb\tc’
>>> s # output is the original sequence
>>> print s #output is translated sequence
>>>len(s) #5
2. If python doesn’t recognize the character after ‘\’, it simple keeps the backslash in the resulting string.
3. Raw stirng: r’xxxx’
4. u‘xxx’: Unicode string
5. >>> abc = 'abc'
>>> for i in abc:
print i
a
b
c
>>> for i in abc:
print i,
a b c
6. Index and slicing:
>>> num = '123456'
>>> num[1:3], num[5:], num[:-4]
('23', '6', '12')
>>> num[::2], num[::-1], num[0:-3:2]
('135', '654321', '13')
7. String conversion tools:
>>>int(“42”), str(42) #Convert from/to string
>>>string.atoi(“42”), ‘42’ #older techniques
8. You cannot change a string in place(by assigning an index).
9. String formatting
>>> "%s %d %s" %('I', 4, 'you')
'I 4 you'
%s: string
%r: s, but uses repr()
%c: character
%d: decimal (integer)
%i: integer
%u: unsigned integer
%o, %x, %X: Octal, Hex, Hex uppercase
%e, %E: floating-point exponent
%f: float point
%g, %G: floating-point e or f
%%: %
%[(names)][flag][width][.precision]code
>>> x = 1234
>>> res = "integers:...%d...%+d...%-6d...%06d" %(x, x, x, x)
>>> res
'integers:...1234...+1234...1234 ...001234'
10. String methods:
X.method(arguments) == string.method(X, arguments)
list(); join(); capitalize(); center(width)…