Today's Overview:
1. Built-in module
2, the function of the co-process
3. Recursion
4. Process-oriented programming and function programming
5. Module
6. Package
7. Re Regular
One, built-in module
1. anonymous function lambda
Definition: An anonymous function usually creates a function that can be called, returns a function, and does not name the function
#不使用匿名函数def func (x, y): return x+yfunc #使用匿名函数f =lambda x,y:x+yprint (f ())
2, Max,zip (zipper function), sorted usage
age={' dragon ':, ' Panda ':, ' banana ': +, ' Lili ': 30}print (max (age)) #默认是以字典的key进行排序, key if the string is in the first order, if the first is consistent, A recursive comparison # method one, using zip to invert key and values Res=zip (Age.values (), Age.keys ()) Print (max (res))
def foo (k): return Age[k]print (Max (Age,key=foo)) print (max (Age,key=lambda k:age[k)) print (min (age,key=lambda k: Age[k]) print (sorted (age)) #默认的排序结果是从小到大print (sorted (Age,key=lambda x:age[x)) #默认的排序结果是从小到大print (sorted (age,key= Lambda x:age[x],reverse=true)) #默认的排序结果是从小到大, resverse inversion from large to small
3, Map,reduce,filter
1. Map
#map, map the relationship of the lambda function to the list l=[' dragon ', ' banana ', ' water ']res=map (lambda x:x+ ' _guess ', l) print (res) # Return value is iterator print (list (res))
2. Reduce
#1, default does not add parameters, take a value from the list as an element, then remove the second value to the last operation with the first element, and finally the final value of # #, plus the parameters from the parameters of the first value from Functools import reducel=[ 1,2,3,4,5]print (Reduce (lambda x,y:x+y,l,10))
3. Filter
#filter过滤规则, extract the matching elements out of l=[' test123 ', ' goto123 ', ' run123 ', ' Pass ']res=filter (lambda x:x.endswith (' 123 '), L) Print (list (res))
Second, the function of the co-process
Python because of the Gil reason, resulting in its thread can not play multi-core parallel computing ability (later with multiprocessing, can achieve multi-process parallelism), looks more chicken. Since under the Gil, only one thread is running at the same time, the switching overhead between threads is a drag for CPU-intensive programs, and the I/O bottleneck is what the process is good at: multitasking concurrency (non-parallelism), where each task hangs at the right time (initiating i/ o) and recovery (I/O end)
#!/usr/bin/python#-*-coding:utf-8-*-#例子: Def F1 (func): def f2 (*args,**kwargs): g = func (*args,**kwargs) Next (g) return G return f2@f1def Eater (name): print ('%s ready to eat '% (name)) and True: Food = yield print ('%s,%s '% (name,food)) G = Eater (' Alex ') g.send (' JJJ ')
Third, recursion
Definition: The function itself is called directly or indirectly during a function call, which is the recursive invocation of a function
Notice:python default maximum recursive number is 1000,sys.getrecursionlimit () view
L = List (range) def to (info,g): If Len (info) = = 0: print (' Not exit ') return mid = Int (len (info)/2) if Info[mid] > G: print ('%s '% (Info[0:mid]) to (info[0:mid],g) elif Info[mid] < g: print (' %s '% (info[mid+1:])) to (info[mid+1:],g) elif info[mid] = = G: print (Info[mid]) to (l,25)
Iv. Process-oriented
Definition: Process-oriented Programming: It is a kind of pipeline to become the idea, is the mechanical
Advantages: The program structure is clear, can simplify the complex problem
Cons: Poor scalability
Applicable scenarios:
Git program, httpd service, Linux kernel
Import os#1, defines an adorner def init (func) that initializes yield: Def wrapper (*args,**kwargs): Res=func (*args,**kwargs) Next ( RES) return res return wrapper# first step through directory @initdef search (target): While True:search_path=yield g =os.walk (Search_path) for par_dir,_,files in g:for file in Files:file_abs_path=r '%s\%s ' % (par_dir,file) # print (File_abs_path) target.send (File_abs_path) #第二步打开文件 @initdef opener (Ta Rget): While True:file_abs_path=yield # print (' opener func==> ', File_abs_path) with open (file_a bs_path,encoding= ' Utf-8 ') as F:target.send ((file_abs_path,f)) #第三步读文件里的行 @initdef Cat (target): while True: File_abs_path,f=yield # (FILE_ABS_PATH,F) for line in F:tag=target.send ((File_abs_path,line)) If tag:break# The fourth step grep filtering, if first exists, then skip the file, go to the next file to Judge @initdef grep (Target,pattern): Tag=false whi Le True:file_abs_path,line=yield tag Tag=false if pattern in Line:tag=true target.send (File_abs_path) #第五步, print the name of the matching content @initdef printer (): While True:file_abs_path=yield print (file_abs_path) x=r ' Path ' g=search (op Ener (Cat (grep (printer (), ' Python '))) print (g) g.send (x)
Five, module
1. Import Module
Create a new namespace, create the name space for the new global namespace, execute the code for the file, get a module name, execute the module name. Py generated Namespace
2. From ... import ...
Generate a new namespace, with the new namespace as the global namespace, execute the file's code, and directly get the module name. py name in the generated namespace
Pros: Convenient without adding prefixes
Cons: Easy to conflict with the name space of the current file
3, the module search order:
Memory----> Built-in---->sys.path
4
Python Automation Development-[fifth day]-oriented process, module, package