day15作业

1、 编写日志装饰器,实现功能如:一旦函数f1执行,则将消息2017-07-21 11:12:11 f1 run写入到日志文件中,日志文件路径可以指定
注意:时间格式的获取

import time
time.strftime('%Y-%m-%d %X')

import time
from functools import wraps
def log(func):
    def wrapper(*args,**kwargs):
        res=func(*args,**kwargs)
        t = time.strftime('%Y-%m-%d %X')
        with open('access.log','a',encoding='utf-8')as f:
            log_time=f'f1启动时间为:{t}'
            f.write(log_time)
        return res
    return wrapper
@log
def intter(x,y):
    time.sleep(1)
    print('====>',x,y)
intter(3,4)

2、基于迭代器的方式,用while循环迭代取值字符串、列表、元组、字典、集合、文件对象

l=[1,2,3,4,5,6]
l=l.__iter__()
while True:
    try:
        res=l.__next__()
        print(res)
    except StopIteration:
        break

a='12313414'
a=a.__iter__()
while True:
    try:
        res=a.__next__()
        print(res)
    except StopIteration:
        break

d={'name':'egon','age':12,'sex':'male'}
d=d.__iter__()
while True:
    try:
        res=d.__next__()
        print(res)
    except StopIteration:
        break

t=(1,2,3,4,5,6)
t=t.__iter__()
while True:
    try:
        res=t.__next__()
        print(res)
    except StopIteration:
        break

s= {1, 2, 3, 4, 5, 6}
s=s.__iter__()
while True:
    try:
        res=s.__next__()
        print(res)
    except StopIteration:
        break

with open('access.log','r',encoding='utf-8')as f:
    line=f.readline()
    line=line.__iter__()
while True:
    try:
        res=line.__next__()
        print(res)
    except StopIteration:
        break

3、自定义迭代器实现range功能

def my_range(start,stop,step=1):
    while start

你可能感兴趣的:(day15作业)