Python Full Stack road Day22

Source: Internet
Author: User

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

    1. List-Generated
    2. Builder expression
    3. Process-oriented programming
    4. Recursive
I. Last Class review
    1. Iterative: Objects that have __iter__ methods are objects that can be iterated
    2. Iterator: Object. ITER () The result is an iterator
    3. Characteristics of iterators:
      1. Iterators. Next () Remove a value
      2. Advantages:
        1. Provides a uniform way of iterating over an object, independent of the index
        2. Lazy calculation
      3. Disadvantages:
        1. Unable to get the length of the iterator
        2. One-time, can only be back to the value, can not go forward, not like the index to get the value of a position
    4. Generator: function with the yield keyword, then the result of this function is the generator
    5. 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
    1. Summarize yield function:
      1. Equivalent to encapsulating the __iter__ and __next__ methods inside the function
      2. Return returns can only be returned once, and yield can be returned more than once
      3. The state of the function pause and continue running is saved through yield
    2. 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‘)
    1. The difference between E.send and next (e)
      1. If the yield in the function is an expression, you must first next (e)
      2. 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.
      3. Another e.send (None) is the same as next (e) effect
Two. Association function initialization Adorner
    1. 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
    1. process-oriented programming idea: Pipelining programming idea, in programming, need to design the whole process
      1. advantages:
        1. architecture clearer
        2. simplifying program complexity
      2. disadvantage:
        1. scalability is extremely poor, so the process-oriented scenario is: Software that does not require frequent changes
    2. 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
    1. General expression
egg_list = []for i in range(100):    egg_list.append(‘egg%s‘% i)print(egg_list)
    1. List-generated expressions common
egg_list = [‘egg%s‘%i for i in range(100)]print(egg_list)
    1. List generation expression if judgment
#列表内只会添加大于50的数字egg_list = [‘egg%s‘%i for i in range(100) if i >50]print(egg_list)
    1. 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 )
    1. 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
    1. Syntax: Similar to list-generated
(expression for item1 in interable if condition1                    for item2 in interable if condition2                    …                    for itemN in interable if conditionN                    )
    1. Advantage: Save memory, generate only one value in memory at a time
    2. 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
    1. 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))
    1. 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)
    1. 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‘])
    1. About with open () error
with open(‘b.txt‘) as f:    d = fprint(d)        #有内存地址,不是很理解print(next(d))    #报错
Homework
Summary today

Python Full Stack road Day22

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.