• day05函数部分


    一:编写函数,(函数执行的时间是随机的)
    import time
    import random
    
    def random_time():
        time.sleep(random.randrange(1,5))
        print(random.randrange(1,5))
    random_time()


    二:编写装饰器,为函数加上统计时间的功能
    import time
    def timmer(func):
        def wrapper(*args,**kwargs):
            start_time = time.time()
            res = func(*args,**kwargs)
            stop_time = time.time()
            print('run time is %s' % (stop_time-start_time))
            return  res
        return wrapper
    @timmer
    def foo():
        time.sleep(6)
        print('from foo')
    foo()
    
    
    


    三:编写装饰器,为函数加上认证的功能
    def auth(driver = 'file'):
        def auth2(func):
            def warpper(*args,**kwargs):
                nameuser = input('please input your username:').strip()
                password = input('please input your password:').strip()
                password = int(password)
                if driver == 'file':
                    if nameuser == 'jaker' and password == 18:
                        print('logo successful')
                        res = func(*args,**kwargs)
                        return res
                elif driver == 'ldap':
                    print('ldap')
            return warpper
        return  auth2
    @auth(driver = 'file')
    def foo(name):
        print(name)
    foo('jaker')
    
    
    


    四:编写装饰器,为多个函数加上认证的功能(用户的账号密码来源于文件),要求登录成功一次,后续的函数都无需再输入用户名和密码
    注意:从文件中读出字符串形式的字典,可以用eval('{"name":"egon","password":"123"}')转成字典格式
    db = 'db.txt'
    login_status = {'user':None,'status':False}
    def auth(auth_type = 'file'):
        def auth2(func):
            def wrapper(*args,**kwargs):
                if login_status['user'] and login_status['status']:
                    return func(*args,**kwargs)
                if auth_type == 'file':
                    with open(db,encoding = 'utf-8')as f:
                        dic = eval(f.read())
                    username = input('please input your username:').strip()
                    password = input('please input your password:').strip()
                    if username in dic and password in dic[username]:
                        login_status['username'] = username
                        login_status['password'] = True
                        res = func(*args,**kwargs)
                        return res
                    else:
                        print('username and password error')
                elif auth_type == 'aaa':
                    pass
                else:
                    pass
                return wrapper
            return auth2
    
    @auth()
    def index()
        print('index')
    
    @auth(auth_type='file')
    def home(name):
        print('welcome %s to home' %name)
    
    #index
    # home('jaker')


    五:编写装饰器,为多个函数加上认证功能,要求登录成功一次,在超时时间内无需重复登录,超过了超时时间,则必须重新登录
    import time,random
    user={'user':None,'login_time':None,'timeout':0.000003,}
    
    def timmer(func):
        def wrapper(*args,**kwargs):
            s1=time.time()
            res=func(*args,**kwargs)
            s2=time.time()
            print('%s' %(s2-s1))
            return res
        return wrapper
    
    
    def auth(func):
        def wrapper(*args,**kwargs):
            if user['user']:
                timeout=time.time()-user['login_time']
                if timeout < user['timeout']:
                    return func(*args,**kwargs)
            name=input('name>>: ').strip()
            password=input('password>>: ').strip()
            if name == 'egon' and password == '123':
                user['user']=name
                user['login_time']=time.time()
                res=func(*args,**kwargs)
                return res
        return wrapper
    
    @auth
    def index():
        time.sleep(random.randrange(3))
        print('welcome to index')
    
    @auth
    def home(name):
        time.sleep(random.randrange(3))
        print('welcome %s to home ' %name)
    
    index()
    home('jaker')



    六:编写下载网页内容的函数,要求功能是:用户传入一个url,函数返回下载页面的结果
    import requests
    def get(url):
    res=requests.get(url).text
    print(res)

    get('https://www.python.org')


    七:为题目五编写装饰器,实现缓存网页内容的功能:
    具体:实现下载的页面存放于文件中,如果文件内有值(文件大小不为0),就优先从文件中读取网页内容,否则,就去下载,然后存到文件中

    扩展功能:用户可以选择缓存介质/缓存引擎,针对不同的url,缓存到不同的文件中
    简单版
    import requests
    import os
    cache_file='cache.txt'
    def make_cache(func):
        def wrapper(*args,**kwargs):
            if not os.path.exists(cache_file):
                with open(cache_file,'w'):pass
    
            if os.path.getsize(cache_file):
                with open(cache_file,'r',encoding='utf-8') as f:
                    res=f.read()
            else:
                res=func(*args,**kwargs)
                with open(cache_file,'w',encoding='utf-8') as f:
                    f.write(res)
            return res
        return wrapper
    
    @make_cache
    def get(url):
        return requests.get(url).text
    
    
    # res=get('https://www.python.org')
    
    # print(res)

     



    八:还记得我们用函数对象的概念,制作一个函数字典的操作吗,来来来,我们有更高大上的做法,在文件开头声明一个空字典,然后在每个函数前加上装饰器,完成自动添加到字典的操作
    route_dic={}
    
    def make_route(name):
        def deco(func):
            route_dic[name]=func
        return deco
    @make_route('select')
    def func1():
        print('select')
    
    @make_route('insert')
    def func2():
        print('insert')
    
    @make_route('update')
    def func3():
        print('update')
    
    @make_route('delete')
    def func4():
        print('delete')
    
    print(route_dic)


    九 编写日志装饰器,实现功能如:一旦函数f1执行,则将消息2017-07-21 11:12:11 f1 run写入到日志文件中,日志文件路径可以指定
    注意:时间格式的获取
    import time
    time.strftime('%Y-%m-%d %X')
    import time
    import os
    
    def logger(logfile):
        def deco(func):
            if not os.path.exists(logfile):
                with open(logfile,'w'):pass
    
            def wrapper(*args,**kwargs):
                res=func(*args,**kwargs)
                with open(logfile,'a',encoding='utf-8') as f:
                    f.write('%s %s run
    ' %(time.strftime('%Y-%m-%d %X'),func.__name__))
                return res
            return wrapper
        return deco
    
    @logger(logfile='aaaaaaaaaaaaaaaaaaaaa.log')
    def index():
        print('index')
    
    index()
    
    
    
     
  • 相关阅读:
    7、8月刷题总结
    【POJ】2828 Buy Tickets(线段树+特殊的技巧/splay)
    [LeetCode] 459. Repeated Substring Pattern 重复子字符串模式
    [LeetCode] 268. Missing Number 缺失的数字
    [LeetCode] 190. Reverse Bits 翻转二进制位
    [LeetCode] 275. H-Index II H指数 II
    [LeetCode] 274. H-Index H指数
    [LeetCode] 387. First Unique Character in a String 字符串的第一个唯一字符
    [LeetCode] 415. Add Strings 字符串相加
    [LeetCode] 220. Contains Duplicate III 包含重复元素 III
  • 原文地址:https://www.cnblogs.com/2722127842qq-123/p/13456455.html
Copyright © 2020-2023  润新知