<P> Read the remote page
From urllib import urlretrievedef printfile (URL): F = open (URL) for line in F: If line. strip (): print line F. close () def LoadFile (url = r 'HTTP: // www.idkin.com/contact.html', process = printfile): Try: filename = urlretrieve(url,'tmp.html ') [0] print (filename) Begin t baseexception, E: print (STR (E) If filename: Process (filename) If _ name _ = "_ main _": LoadFile () # The returned value of urlretrieve is a tuple. The first element is the target file name. # If urlretrieve obtains only one parameter, the first element of the returned value is the temporary file name generated.
Filter Function, similar to array # filter of javascript1.7. The filter function is equivalent to a filter. Call a Boolean function FN to iterate over the elements in each seq, and return a sequence of elements that make FN return true.
From Random import randintdef filter (FN, arr): ret = []; for I in arr: If FN (I): Ret. append (I) return ret; arr = [1, 3, 4, 5, 6]; print filter (Lambda N: N % 2, arr)
Map function, similar to the array # map of javascript1.7. The map function func acts on each element of a given sequence and uses a list to provide the return value.
From Random import randintdef map (FN, arr): ret = []; for I in arr: Ret. append (FN (I) return ret; arr = [1, 2, 3, 4, 5, 6]; print map (Lambda N: N + 10, arr)
We can also implement this iterator with the same thought:
Def RMAP (fun, list): If list = []: return [] else: return [fun (list [0])] + RMAP (fun, list [1:])
Reduce function, similar to array # reduce of javascript1.7. FN is a binary function that acts on the elements of the seq sequence, each carrying a pair (previous results and the elements of the next sequence ), continuously apply the existing results and the next value to the subsequent results, and finally reduce our sequence into a single return value.
From Random import randintdef reduce (FN, seq, initial = none): lseq = List (SEQ) if initial is none: res = lseq. pop (0) else: res = initial for I in lseq: res = FN (Res, I) Return resarr = [1, 2, 3, 4, 5, 6]; print reduce (lambda, b: A + B, arr)
Trace closure lexical Variables
Output = 'int % R id = % # 0x val = % d' W = x = y = z = 1def F1 (): x = y = z = 2 def F2 (): Y = z = 3 def F3 (): Z = 4 Print Output % ('w', ID (W), w) Print Output % ('x ', ID (x), x) Print Output % ('y', ID (Y), Y) Print Output % ('Z', ID (z), Z) clo = f3.func _ closure if clo: Print "F3 closure vars:", [STR (c) for C in clo] else: Print "No F3 closure vars" F3 () clo = f2.func _ closure if clo: Print "F2 closure vars:", [STR (c) for C in clo] else: Print "No F2 closure vars" f2 () clo = f1.func _ closureif clo: Print "F1 closure vars:", [STR (c) for C in clo] else: Print "No F1 closure vars" F1 ()
Print logs
# Coding = 'utf-8' from time import timedef logged (when): def log (F, * ARGs, ** kargs): Print '''called: function: % s ARGs: % R kargs: % R ''' % (F, argS, kargs) def pre_logged (F): def wrapper (* ARGs, ** kargs ): log (F, * ARGs, ** kargs) return F (* ARGs, ** kargs) return wrapper def post_logged (F): def wrapper (* ARGs, ** kargs ): now = Time () Try: Return F (* ARGs, ** kargs) Finally: log (F, * ARGs, ** kargs) print "time delta: % s "% (Time ()-Now) return wrapper try: Return {" pre ": pre_logged," Post ": post_logged} [when] failed t keyerror, E: raise valueerror (E), 'must be "pre" or "Post" '@ logged ("Post") def Hello (name): Print "hello ,", namehello ("world ")
Generator
# Coding = 'utf-8' def counter (start_at = 10 ): count = start_at while (true): val = (yield count) if (Val is not none): Count = Val else: Count + = 1 If (COUNT = 10 ): breakfor I in counter (3): print I