zoukankan      html  css  js  c++  java
  • 生成器

    # 生成器与return的区别:
    # return只能返回一次值,yield可以返回多次值
    # yield到底做了什么?
    # yield把函数变成生成器---》迭代器

    from collections import Iterator
    # 生成器就是一个函数,这个函数包含yield这个关键字
    # def test():
    # print("first")
    # yield 1 #return 1
    # print("second")
    # yield 2
    #
    # g=test()
    # print(g)
    # # 看看是不是迭代器
    # print(isinstance(g,Iterator))
    #
    # print(next(g))
    # print(next(g))

    # next(g) # 没有1输出是因为没有发返回值保存下来
    # next(g)

    # for i in g:
    # next(i)

    def countdown(n):
    print('start countdown')
    while n>0:
    yield n
    n-=1
    print('done')

    g=countdown(5)
    # print(g)
    # print(next(g))
    # print(next(g))
    # print(next(g))
    # print(next(g))
    # print(next(g))
    # print(next(g))
    # next(g)

    for i in g:
    print(i)

    while True:
    try:
    print(next(g))
    except StopIteration:
    break

    while True:
    try:
    print(next(g))
    except StopIteration:
    break

    # 在nginx下,写下面的命令,可以得到文件内的内容
    tail -f /var/log/nginx/access.log

    echo 'hoeel' >> /var/log/nginx/access.log

     






    # /user/bin/env python
    import time
    def tail(file_path):
    with open(file_path,'r') as f:
    f.seek(0,2)
    line=f.readline()
    if not line:
    time.sleep(0.3)
    print("=======>")
    continue
    else:
    # print(line,end='')
    yield line

    # g=tail('a.txt')
    # print(g)
    # print(next(g))

    # 要打印带error那行:
    def grep(pattern,lines):
    for line in lines:
    if pattern in line:
    print('33[45m%s33[0m' %line)

    g=tail('a.txt')
    grep('error',g)


  • 相关阅读:
    整理一些将窗口显示在前台办法
    工具
    [Windows Api 学习] Error Handling Functions
    Windows实用快捷键
    程序化交易资料汇总
    compile libpng
    zlib 1.2.8 编译笔记
    Cryptopp Usage Note
    linux环境中Java服务通过shell脚本重启(升级)自己
    搭建自己的maven库---nexus
  • 原文地址:https://www.cnblogs.com/jensenxie/p/8972953.html
Copyright © 2011-2022 走看看