zoukankan      html  css  js  c++  java
  • Day7:Filesprocess,Iterator and Generator

    # r r+ w w+ a(add in the eend b+
    """

    # f = open('陈粒','r',encoding='utf-8')
    data = f.read()
    print(f.readable())
    print(f.writable())
    print(f.readline())
    # just read one line each time
    print(f.readlines())
    # read all the lines and put in a list
    print(data)
    f = open('陈粒1','r+',encoding='utf-8')
    f.write('I have seen the desert raining
    ')
    data = f.read()
    print(data)
    f.close()

    """

    with open('chenli','r',encoding='utf-8') as sdr_s,with open('chenli1','w',encoding='utf-8') as dtr_s:
    data = sdr_s.read()
    dtr_s.write(data)
    f = open('dtr_s','r',encoding='utf-8')
    data = f.read()
    print(data)

    #iterator and generator:satisfy the principle(__iter__(),means have the use of __next__()

    # 1:
    l = [1,2,3,4] iter_1 = l.__iter__() print(iter_1.__next__()) print(iter_1.__next__()) print(iter_1.__next__()) print(next(iter_1)) # next():the inner function of python;the same as iter_1.__next__() # 2: for i in l: #iter_1 = l.__iter__() print(iter_1.__next__()) print(i) # 3: print(l[2]) # 4:index index = 0 while index < len(l): print(l[index]) index += 1 # as for dict(return the key values) and files can also use the 'for' sentence

    # ***************generator is iterator :
    # 1:generator function(replace the return to yield and yield can retain the function's state)

    def test():
    print('start born:')
    print('start born:')
    print('start born:')
    yield 'me'
    print('born son')
    yield 'son'
    print('born grandson')
    yield 'grandson'
    g = test()
    print(g)
    print(g.__next__())
    print(g.__next__())
    print(g.__next__())

    # 2: ternary expression:

    # name = 'alex'
    name = 'zxver'
    res = 'SB' if name == 'alex' else 'beauty'
    print(res)
    l = ['egg%s' %i for i in range (10)]
    l1 = ['egg%s' %i for i in range (10) if i < 5]
    print(l)
    print(l1) 
    # use tuple
    l2 = ('egg%s' %i for i in range (10))
    print(l2)
    print(l2.__next__())
    print(l2.__next__())
    print(l2.__next__())
    print(l2.__next__())
    print(next(l2))

    # generator can save the memory: use tuple
    print(sum(i for i in range(10000000)))

    def produce_baozi():
    for i in range(100):
    # return i
    yield 'sale a baozi%s' %i
    
    pro_g = produce_baozi()
    baozi1 = pro_g.__next__()
    print(baozi1)
    baozi2 = pro_g.__next__()
    print(baozi2)
    baozi3 = pro_g.__next__()
    print(baozi3)
    # yield can return mutiple numbers and can use the return value directly
    def census():
    with open('pop.py') as f:
    for line in f:
    p = eval(line)
    yield p['population']
    c = census()
    #print(c.__next__())
    #print(c.__next__())
    #print(c.__next__())
    #print(c.__next__())
    all_pop = sum(i for i in c)
    print(all_pop)
    ###################calculate the rate:(no any return value cause the generator can just overall on time)
    for q in c:
    print(q/all_pop)

    # send(self,value),can send the value to the last yield

    def sen():
    print('start')
    # yield 1
    # firt = yield 1
    firt = yield
    print('the next',firt)
    yield 2
    s = sen()
    print(s.__next__())
    # print(s.__next__())
    # print(s.send(None))
    print(s.send('value'))

    # eat baozi:two separate section

    import time
    def consumer(name):
    print('I %s start to eat baozi' %name)
    while True:
    baozi = yield
    time.sleep(1)
    print('%s is eat %s' %(name,baozi))
    def producer():
    c1 = consumer('zxver')
    c2 = consumer('alex')
    c1.__next__()
    c2.__next__()
    # c1.send('baozi')
    for i in range(10):
    time.sleep(1)
    c1.send('baozi %s' %i)
    c2.send('baozi %s' %i)
    producer()
  • 相关阅读:
    Jquery实现类似百度的搜索框
    Spring mvc 初始化过程
    Git学习笔记(2)-Eclipse中Git插件使用
    Git学习笔记(1)
    Tomcat7设置环境变量供java代码读取
    webpack+gulp实现自动构建部署
    netty 粘包问题处理
    java 并发工具类CountDownLatch & CyclicBarrier
    add spring-boot modules to maven project
    Spring Boot (#1 quick start)
  • 原文地址:https://www.cnblogs.com/zxver/p/12124397.html
Copyright © 2011-2022 走看看