Python_장식기 연습문제31

11907 단어
# 1.     ,            (            ),
#
FLAG = False
def login(func):
    def inner(*args,**kwargs):
        global FLAG
        '''    '''
        if FLAG:
            ret = func(*args, **kwargs)  # func       
            return ret
        else:
            username = input('username : ')
            password = input('password : ')
            if username == 'boss_gold' and password == '22222':
                FLAG = True
                ret = func(*args,**kwargs)      #func       
                return ret
            else:
                print('    ')
    return inner

@login
def shoplist_add():
    print('      ')

@login
def shoplist_del():
    print('      ')

shoplist_add()
shoplist_del()

 
 
def log(func):
    def inner(*args,**kwargs):
        with open('log','a',encoding='utf-8') as f:
            f.write(func.__name__+'
') ret = func(*args,**kwargs) return ret return inner @log def shoplist_add(): print(' ') @log def shoplist_del(): print(' ') shoplist_add() shoplist_del() shoplist_del() shoplist_del() shoplist_del() shoplist_del()

 
#     (  ):
# 1.           ,     :      url,           
# 2.   1     ,           :
#   :             ,       (      0),             ,  ,    ,       
import os
from urllib.request import urlopen
def cache(func):
    def inner(*args,**kwargs):
        if os.path.getsize('web_cache'):
            with open('web_cache','rb') as f:
                return f.read()
        ret = func(*args,**kwargs)  #get()
        with open('web_cache','wb') as f:
            f.write(b'*********'+ret)
        return ret
    return inner

@cache
def get(url):
    code = urlopen(url).read()
    return code


# {'  ':"   "}
ret = get('http://www.baidu.com')
print(ret)
ret = get('http://www.baidu.com')
print(ret)
ret = get('http://www.baidu.com')
print(ret)



import os
from urllib.request import urlopen
def cache(func):
    def inner(*args,**kwargs):
        if os.path.getsize('web_cache'):
            with open('web_cache','r',encoding='utf-8') as f:
                return f.read()
        ret = func(*args,**kwargs)  #get()
        with open('web_cache','w',encoding='utf-8') as f:
            f.write(b'*********'+ret)
        return ret
    return inner

@cache
def get(url):
    code = urlopen(url).read()
    return code


# {'  ':"   "}
ret = get('http://www.baidu.com')
print(ret)
ret = get('http://www.baidu.com')
print(ret)
ret = get('http://www.baidu.com')
print(ret)

 
전재 대상:https://www.cnblogs.com/LXL616/p/10666828.html

좋은 웹페이지 즐겨찾기