# 复习
# day homework
# 装饰器进阶
# 记现象
# functools wraps
# 带参数的装饰器
# 多个装饰器装饰同一个函数
# weekly homework
# 文件操作
# 字符串处理
# 输入输出
# 流程控制
# 装饰器
# 开发原则:开放封闭元祖
# 装饰器的作用:在不改变原函数的调用方式的情况下,在函数的前后添加功能
# 装饰器的本质:闭包函数
# def wrapper(f):
# def inner(*args,**kwargs):
# print('在被装饰的函数执行之前做的事情')
# ret = f(*args,**kwargs)
# print('在被装饰的函数执行之后做的事情')
# return ret
# return inner
#
# @wrapper # holiday = wrapper(holiday)
# def holiday(day):
# print('放假%s'%day)
# return 'Happy'
#
# ret = holiday('12')
# print(ret)
# # *args接受是聚合,调用是打散
# def outer(*args,**kwargs):
# print(args) # (1, 2, 3, 4)
# print(*args) # 1 2 3 4
# print(kwargs) # {}
# def inner(*args):#(1,2,3,4)
# print(args)
# print(*args)
# inner(*args)#1 2 3 4
# # outer(1,2,3,4) # == outer(*(1,2,3,4))
# 装饰器精讲
# def strtest():
# '''
# 呵呵哒
# :return:
# '''
# print('testqwe')
# print(strtest.__name__) # 查看字符串式的函数名
# print(strtest.__doc__) # 查看函数的注释
# from functools import wraps
# def wrapper(f):
# @wraps(f)
# def inner(*args,**kwargs):
# print('在被装饰的函数执行之前做的事情')
# ret = f(*args,**kwargs)
# print('在被装饰的函数执行之后做的事情')
# return ret
# return inner
#
# @wrapper # holiday = wrapper(holiday)
# def holiday(day):
# ''' 这是一个放假通知 '''
# print('放假%s'%day)
# return 'Happy'
# print(holiday.__name__)
# print(holiday.__doc__)
# print(holiday(55))
'''
打印结果
holiday
这是一个放假通知
在被装饰的函数执行之前做的事情
放假55
在被装饰的函数执行之后做的事情
Happy
'''
# 带参数的装饰器
# 500个函数
# import time
# FLAG = True
# def timer_out(flag):
# def timer(f):
# def inner(*args,**kwargs):
# if flag:
# start = time.time()
# ret = f(*args,**kwargs)
# end = time.time()
# print(end - start)
# return ret
# else:
# ret = f(*args,**kwargs)
# return ret
# return inner
# return timer
#
# @timer_out(FLAG)
# def fun1():
# time.sleep(0.1)
# print('fun1')
#
# @timer_out(FLAG)
# def fun2():
# time.sleep(0.1)
# print('fun2')
#
# def fun3():
# time.sleep(1)
# print('fun3')
#
# fun1()
# fun2()
# fun3()
#
# '''
# 打印结果
# fun1
# 0.10155510902404785
# fun2
# 0.10382699966430664
# fun3
# '''
# # 多个装饰器装饰一个函数
# def wrapper1(f): # func->f # 执行1
# def inner1():
# print('start-inner1')
# f()# f
# print('end-inner1')
# return inner1
#
# def wrapper2(f):#func->inner1 # 执行2
# def inner2():
# print('start-inner2')
# f()# inner1
# print('end-inner2')
# return inner2
#
# @wrapper2 # wrapper2(f) = wrapper2(inner1) ==inner2 # 执行4
# @wrapper1 # wrapper1(f) = inner1 # 执行3
# def f():
# print('f')
# f() # inner2()
# '''
# 输出结果
# start-inner2
# start-inner1
# f
# end-inner1
# end-inner2
# '''
# # 记录
# 1、编写装饰器,为多个函数加上认证的功能(用户的账号密码来源于文件)要求登录成功依次,后续的函数都无需再输入用户名和密码
# FLAG = False
# def login(f):
# def inner(*args,**kwargs):
# global FLAG
# if FLAG:
# ret = f(*args, **kwargs)
# return ret
# else:
# username = input('username:')
# password = input('password:')
# if username == 'testqa' and password == '123':
# FLAG = True
# ret = f(*args,**kwargs)
# else:
# print('login failed')
# return ret
# return inner
#
# @login
# def shoplist_add():
# print('add new goods')
#
# @login
# def shoplist_del():
# print('delete goods')
#
# shoplist_add()
# shoplist_del()
# 2、编写装饰器,为多个函数加上记录调用的功能,要求每次调用函数都讲被调用的函数名称写入文件
# def log(f):
# def inner(*args,**kwargs):
# with open('log','a',encoding='UTF-8')as file:
# file.write(f.__name__ + '
')
# ret = f(*args,**kwargs)
# return ret
# return inner
#
# @log
# def shoplist_add():
# print('add new goods')
# @log
# def shoplist_del():
# print('delete goods')
# shoplist_add()
# shoplist_del()
# 进阶作业
# 1、编写下载网页内容的函数,要求功能是:用户传入一个url,函数返回下载页面的结果
# 2、未题目1编写装饰器,实现缓存网页内容的功能:
# 具体:实现下载的页面存放于文件中,如果文件内有值(文件大小不为0),就优先从文件中读取网页内容,否则,就去下载
# import os
# from urllib.request import urlopen
# def cache(f):
# def inner(*args,**kwargs):
# if os.path.getsize('webcache'):
# with open('webcache','rb') as f1:
# return f1.read()
# ret = f(*args,**kwargs)
# with open('webcache','wb') as f2:
# f2.write(b'*****'+ret)
# return ret
# return inner
#
# @cache
# def get(url):
# response = urlopen(url).read()
# return response
#
#
# # {'网址':'文件名'}
# response1 = get('http://www.baidu.com')
# response2 = get('http://www.baidu.com')
# response3 = get('http://www.baidu.com')
# print(response1)
# print(response2)
# print(response3)
shopping_car = {}
list = [
{'name':'苹果','price':10},
{'name':'香蕉','price':20},
{'name':'西瓜','price':30},
]
# 把货物放在货架上
print('welcome to ff friuts shop')
money = input('please input your money:')
if money.isdigit() and int(money)>0:
for i,k in enumerate(list):
print('序号{},商品{},价格{}'.format(i,k['name'],k['price']))
choose = input('please input your goods id:')
if choose.isdigit() and int(choose) <len(list) and int(choose)>=0:
num = input('please input your goods number:')
if num.isdigit():
if int(money) > list[int(choose)]['price']*int(num):
money = int(money) - list[int(choose)]['price']*int(num)
if list[int(choose)]['name'] in shopping_car:
shopping_car[list[int(choose)]['name']] = int(num) + shopping_car[list[int(choose)]['name']]
else:
shopping_car[list[int(choose)]['name']] = int(num)
print('your shopping car has {},your money has {}'.format(shopping_car,money))
else:
print('your money is not enough')
else:
print('your goods id is invalidate')
# 打印输出
# welcome to ff friuts shop
# please input your money:900
# 序号0,商品苹果,价格10
# 序号1,商品香蕉,价格20
# 序号2,商品西瓜,价格30
# please input your goods id:0
# please input your goods number:1
# your shopping car has {'苹果': 1},your money has 890