标签:关键字 col exce 信息 输入 set stop 无限 pattern
初次编辑2017年10月27日,星期五
引用:Alex
import time
from functools import wraps #注意这个
def timmer(func):
@wraps(func) #还有这个
def wrapper():
start_time = time.time()
func()
stop_time = time.time()
print(‘run time is %s ‘ % (stop_time - start_time))
return wrapper
@timmer #index=timmer(index)
def index():
‘come from index‘
print(‘welcome to oldboy‘)
index() #wrapper()
print(index.__doc__) #.__doc__打印函数描述信息,具体不是很了解
b = {‘a‘:1,‘b‘:2,‘c‘:3}
i = iter(b)
while True:
print(next(i))
#注意:此方法会while会无限循环,报错异常 StopIteration
b = {‘a‘:1,‘b‘:2,‘c‘:3}
i = iter(b)
while True:
try: #注意此行
print(next(i))
except StopIteration: #and this
break # and this
for key in d: #d即为d.__iter__() 迭代器放入原位置
print(key) #并且for循环具有异常捕捉的功能
b = {‘a‘:1,‘b‘:2,‘c‘:3}
for i in b:
print(i)
from collections import Iterable,Iterator
s = ‘hello‘
l = [1,2,3]
t = (1,2,3)
d = {‘a‘:1}
set1 = (1,2,3,4)
f = open(‘a.txt‘)
s.__iter__()
l.__iter__()
t.__iter__()
d.__iter__()
set1.__iter__()
f.__iter__()
#Iterable 为可迭代的
print(isinstance(s,Iterable)) #isinstance() 函数来判断一个对象是否是一个已知的类型,类似 type()。
print(isinstance(l,Iterable))
print(isinstance(t,Iterable))
print(isinstance(d,Iterable))
print(isinstance(set1,Iterable))
print(isinstance(f,Iterable))
print(isinstance(s,Iterator)) #Iterator 为迭代器
print(isinstance(l,Iterator))
print(isinstance(t,Iterator))
print(isinstance(d,Iterator))
print(isinstance(set1,Iterator))
print(isinstance(f,Iterator))
def test():
print(‘one‘)
yield 1
print(‘two‘)
yield 2
print(‘three‘)
yield 3
g = test() #无输出,此时g为生成器,即把函数变成一个迭代器
print(g) #<generator object test at 0x00000000027E91A8>
res = next(g) #one next触发生成器的执行
print(res) #1
def countdown(n):
print(‘start countdown‘)
while n >0:
yield n
n -= 1
print(‘done‘)
g= countdown(5)
print(next(g)) #start countdown 5
print(next(g)) #4
print(next(g)) #3
print(next(g)) #2
print(next(g)) #1
print(next(g)) #done #会报错
#for循环写法
for i in g:
print(i)
#while循环写法
while True:
try:
print(next(g))
except StopIteration:
break
#惰性计算
def func():
n = 0
while True:
yield n
n += 1
f = func()
print(next(f))
import time
def tail(file_path):
with open(file_path,‘r‘) as f:
f.seek(0,2) #光标移到最后
while True:
line = f.readline() #读这一行
if not line:
time.sleep(0.5)
continue
else:
print line, #print 输出光标不换行
tail(‘/tmp/a.txt‘)
import time
def tail(file_path):
with open(file_path,‘r‘) as f:
f.seek(0,2) #光标移到最后
while True:
line = f.readline() #读这一行
if not line:
time.sleep(0.5)
print ‘=======‘
continue
else:
yield line
g = tail(‘/tmp/a.txt‘) #函数变成一个生成器
for line in g:
print line,
import time
def tail(file_path):
with open(file_path,‘r‘) as f:
f.seek(0,2) #光标移到最后
while True:
line = f.readline() #读这一行
if not line:
time.sleep(0.5)
continue
else:
yield line
g = tail(‘/tmp/a.txt‘)
for line in g:
if ‘error‘ in line:
print line,
import time
def tail(file_path):
with open(file_path,‘r‘) as f:
f.seek(0,2) #光标移到最后
while True:
line = f.readline() #读这一行
if not line:
time.sleep(0.5)
continue
else:
yield line
def grep(pattern,lines): #pattern 为grep所抓取的 lines为输入源
for line in lines:
if pattern in line:
print line,
g = tail(‘/tmp/a.txt‘)
grep(‘error‘,g)
import time
#定义阶段:定义两个生成器函数
def tail(file_path):
with open(file_path,‘r‘) as f:
f.seek(0,2) #光标移到最后
while True:
line = f.readline() #读这一行
if not line:
time.sleep(0.5)
continue
else:
yield line
def grep(pattern,lines): #pattern 为grep所抓取的 lines为输入源
for line in lines:
if pattern in line:
yield line
#调用阶段:得到俩生成器对象
g1 = tail(‘/tmp/a.txt‘)
g2 = grep(‘error‘,g1)
#使用阶段:next触发执行g2生成器函数
for i in g2:
print i,
def 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(‘钢蛋‘)
print(next(e))
print(e.send(‘包子‘))
print(e.send(‘韭菜包子‘)) #send 等同于next 有返回值,但是会把后面的参数传给当前行的yield
print(e.send(‘馅饼‘))
标签:关键字 col exce 信息 输入 set stop 无限 pattern
原文地址:http://www.cnblogs.com/sama/p/7825403.html