first edited October 28, 2017, Saturday
Summary
I. Last Class review
Two. Association function initialization Adorner
Three. Send Implementation crawl Web page
Four. Process-oriented programming
Five. List-generated
Six. three-dimensional expression
Seven. Generator expressions
Eight. Declarative programming
Homework
Summary today
Summary
Citation: Baidu
- List-Generated
- Builder expression
- Process-oriented programming
- Recursive
I. Last Class review
- Iterative: Objects that have __iter__ methods are objects that can be iterated
- Iterator: Object. ITER () The result is an iterator
- Characteristics of iterators:
- Iterators. Next () Remove a value
- Advantages:
- Provides a uniform way of iterating over an object, independent of the index
- Lazy calculation
- Disadvantages:
- Unable to get the length of the iterator
- One-time, can only be back to the value, can not go forward, not like the index to get the value of a position
- Generator: function with the yield keyword, then the result of this function is the generator
- The essence of the generator is the iterator
def func(): n=0 while True: yield n n += 1g=func()res=next(g)for i in g: pass
- Summarize yield function:
- Equivalent to encapsulating the __iter__ and __next__ methods inside the function
- Return returns can only be returned once, and yield can be returned more than once
- The state of the function pause and continue running is saved through yield
- The expression form of yield:
food= yield
def eater(name): print(‘%s start to eat ‘%name) while True: food=yield print(‘%s eat %s‘%(name,food))e=eater(‘zhejiangF4‘)
- The difference between E.send and next (e)
- If the yield in the function is an expression, you must first next (e)
- The common point of both is that the function can continue to run where it was last paused, except that send will pass a value to yield when the next execution of the code is triggered.
- Another e.send (None) is the same as next (e) effect
Two. Association function initialization Adorner
- Simple decorator
def foo(func): def foo1(*args,**kwargs): res = func(*args,**kwargs) next(res) return res #此处理解 生成器是一次性的 return foo1@foo #eater = foo(eater) = foo1def eater(name): print(‘%s start to eat food ‘% name) food_list = [] while True: food = yield food_list print(‘%s get %s, to start eat ‘% (name, food)) food_list.append(food) print(‘Done‘)e = eater(‘钢蛋‘) #foo1(‘钢蛋‘)print(e.send(‘123‘))
Three. Send Implementation crawl Web page
from urllib.request import urlopendef my_next(func): def foo(*args,**kwargs): res = func(*args,**kwargs) next(res) return res return foo@my_nextdef get(): while True: url = yield res = urlopen(url).read() #爬网页返回值 print(res) #输出爬网页结果g=get()g.send(‘http://www.baidu.com‘)g.send(‘http://www.python.org‘)
Four. Process-oriented programming
- process-oriented programming idea: Pipelining programming idea, in programming, need to design the whole process
- advantages:
- architecture clearer
- simplifying program complexity
- disadvantage:
- scalability is extremely poor, so the process-oriented scenario is: Software that does not require frequent changes
- implement #grep-rl ' python ' c:\egon
# grep-rl ' python ' C:\egonimport os# adorner, will generator next initialize DEF init (func): def foo (*args,**kwargs): res = func (*args,** Kwargs) Next (RES) return res return FOO@INITDEF search (target): ' Find file absolute path ' while true:dir_p Ath = yield #此yield放在while外还是内有疑问: Yield in the while outside will cause a dead loop, unable to make the next yield g = Os.walk (Dir_path) #g为迭代器 For i in G: # print (i) for J in i[-1]: File_path = '%s\\%s '% (i[0],j) Target.send (File_path) @initdef opener (target): ' Open file Get file handle ' while True:file_path = yield with open (f Ile_path) as F:target.send ((F,file_path)) #send传递两个文件时, parentheses are required @initdef cat (target): ' Read file contents ' while T Rue:f,file_path = yield for line in F: #读取一行文件 Target.send ((line,file_path)) @initdef grep (Target,pattern): #传递两个参数, where pattern is the string to be filtered ' filter the file row for Python ' while true:line,file_path = yield I F Pattern in line: Target.send (File_path) #需传递文件路径 @initdef printer (): ' Print file path ' while True:file_path = yield Print (File_path) G=search (Opener (Cat (grep (printer (), ' Python '))) g.send (' C:\\egon ')
Five. List-generated
- General expression
egg_list = []for i in range(100): egg_list.append(‘egg%s‘% i)print(egg_list)
- List-generated expressions common
egg_list = [‘egg%s‘%i for i in range(100)]print(egg_list)
- List generation expression if judgment
#列表内只会添加大于50的数字egg_list = [‘egg%s‘%i for i in range(100) if i >50]print(egg_list)
- Grammar
[expression for item1 in interable if condition1 for item2 in interable if condition2 … for itemN in interable if conditionN ]
Similar to
res = []for item1 in interable: if condition1: for item2 in interable: if condition2: … for itemN in interable: if conditionN: res.append(expression )
- Implements the Find file absolute path, expressed as a list
import osg = os.walk(‘C:\\egon‘)l = [‘%s\\%s‘% (i[0],j) for i in g for j in i[-1]]print(l)#[‘C:\\egon\\a.txt - 副本.txt‘, ‘C:\\egon\\a.txt.txt‘, ‘C:\\egon\\a\\a.txt.txt‘, ‘C:\\egon\\a\\a2.txt.txt‘, ‘C:\\egon\\b\\a.txt.txt‘, ‘C:\\egon\\b\\a1.txt.txt‘]
Six. three-dimensional expression
name = ‘alex‘name = ‘egon‘res = ‘sb‘ if name ==‘alex‘ else ‘shuai‘print(res) #输出shuai
Seven. Generator expressions
- Syntax: Similar to list-generated
(expression for item1 in interable if condition1 for item2 in interable if condition2 … for itemN in interable if conditionN )
- Advantage: Save memory, generate only one value in memory at a time
- Application: Reads all the contents of a large file, and the row processing
#读取文件,并去掉每一行两头空格f = open(‘a.txt‘)g= (line.strip() for line in f)print(next(g))
Note: Because G is iterative, it can be converted into a listlist(g)
Eight. Declarative programming
- Total consumption calculation
#鸡蛋 5 3#特斯拉 10000000.2 5#上衣 1000 3#裤子 2000 3#袜子 100#读取包含以上信息的文件,并计算总共花费#第一种传统写法total = []with open(‘b.txt‘,‘r‘,encoding=‘utf-8‘) as f: for line in f: goods=line.split() #split用法及返回值需加强 # print(goods) res = float(goods[1])*float(goods[-1]) total.append(res)print(total)print(sum(total))#第二种声明式编程写法f=open(‘b.txt‘,‘r‘,encoding=‘utf-8‘) #不能用with 否则会IO操作报错total=(float(line.split()[1])*float(line.split()[-1]) for line in f)print(total)print(sum(total))
- The contents of the file are nested in the list in a dictionary form
#[{‘name‘: ‘袜子‘, ‘price‘: ‘100‘, ‘num‘: ‘3‘}, {‘name‘: ‘袜子‘, ‘price‘: ‘100‘, ‘num‘: ‘3‘}, {‘name‘: ‘袜子‘, ‘price‘: ‘100‘, ‘num‘: ‘3‘}, {‘name‘: ‘袜子‘, ‘price‘: ‘100‘, ‘num‘: ‘3‘}, {‘name‘: ‘袜子‘, ‘price‘: ‘100‘, ‘num‘: ‘3‘}]# 基本写法res = []d = {}with open(‘b.txt‘,‘r‘,encoding=‘utf-8‘) as f: for line in f: l = line.split() d[‘name‘] = l[0] d[‘price‘] = l[1] d[‘num‘] = l[2] res.append(d)print(res)
- Simulating database queries
with open(‘b.txt‘,‘r‘,encoding=‘utf-8‘) as f: res = (line.split() for line in f) dic_g = ({‘name‘:line[0],‘price‘:line[1],‘num‘:line[2]} for line in res) goods_dic = next(dic_g) print(goods_dic[‘num‘])
- About with open () error
with open(‘b.txt‘) as f: d = fprint(d) #有内存地址,不是很理解print(next(d)) #报错
Homework
Summary today
Python Full Stack road Day22