python-day11~12_装饰器

# 三元运算符
# 接收结果的变量 = 条件为真的结果 if 条件 else 条件为假的结果

a if a > b else b

1,装饰器

import time
def func():
    time.sleep(0.01)
    print('大家好!')

def timmer(f):  #装饰器函数
    def inner():
        start = time.time()
        f()       #被装饰的函数
        end = time.time()
        print(end - start)
    return inner

func = timmer(func)
func()

# 装饰器的作用 —— 不想修改函数的调用方式 但是还想在原来的函数前后添加功能
# timmer就是一个装饰器函数,只是对一个函数 有一些装饰作用

# 原则: 开放封闭原则
# 开放 : 对扩展是开放的
# 封闭 : 对修改是封闭的。原函数内部不能改

其设计核心就是整个软件框架,对扩展是开放的,对修改是封闭的。也就是说,当软件有新的需求变化的时候,只需要通过对软件框架进行扩展来适应新的需求,而不是对框架内部的代码进行修改。

2,语法糖

import time

def timmer(f):  #装饰器函数
    def inner(): start = time.time() f() #被装饰的函数 end = time.time() print(end - start) return inner

@timmer @语法糖,功能与func = timmer(func)一样。要紧贴被装饰的函数
def func(): time.sleep(0.01) print('大家好!') #func = timmer(func) func()

3,装饰器的固定模板

from functools import wraps  #以将原函数对象的指定属性复制给包装函数对象

def wrapper(func):  #qqxing

       @wraps(func)   #调用functool import
  def inner(*args,**kwargs):
    ret = func(*args,**kwargs)  #被装饰的函数
    return ret
  return inner

@wrapper  #qqxing = wrapper(qqxing)
def qqxing():
  print(123)

# print(wahaha.__name__) #查看字符串格式的函数名,依靠founctool import
# print(wahaha.__doc__) #document

4,

装饰器函数最多三层嵌套

#带参数的装饰器
#500个函数
# import time
# FLAGE = False   #用FLAG来控制装饰器是否生效
# def timmer_out(flag):
  # def timmer(func):
    # def inner(*args,**kwargs):
      # if flag:
        # start = time.time()
        # ret = func(*args,**kwargs)
        # end = time.time()
        # print(end-start)
        # return ret
      # else:
        # ret = func(*args, **kwargs)
        # return ret
    # return inner
  # return timmer
# # timmer = timmer_out(FLAGE)
# @timmer_out(FLAGE)   #wahaha = timmer(wahaha)
# def wahaha():
  # time.sleep(0.1)
  # print('wahahahahahaha')
#
# @timmer_out(FLAGE)
# def erguotou():
  # time.sleep(0.1)
  # print('erguotoutoutou')

# wahaha()
# erguotou()

<python核心编程-第二版>

5,

#多个装饰器装饰一个函数
def wrapper1(func):
  def inner1():
    print('wrapper1 ,before func')
    ret = func()
    print('wrapper1 ,after func')
    return ret
  return inner1

def wrapper2(func):
  def inner2():
    print('wrapper2 ,before func')
    ret = func()
    print('wrapper2 ,after func')
    return ret
  return inner2

def wrapper3(func):
  def inner3():
    print('wrapper3 ,before func')
    ret = func()
    print('wrapper3 ,after func')
    return ret
  return inner3

@wrapper3
@wrapper2  #第二步,wrapper1 = wrapper2(wrapper1)  = wrapper2(inner1) = inner2
@wrapper1  #第一步,f = wrapper1(f) = inner1
def f():
  print('in f')
  return '哈哈哈'

print(f())

作业:

'''
# 1.默写:带参数的装饰器。需要标注代码的执行步骤。
# 2.整理作业:函数的知识点以及装饰器相关作业。装饰器作业需要自己写一遍,并给作业加注释。
#1
# def wrapper(fu):
# def inner(*args,**kwargs):
# ret = fu(*args,**kwargs)
# return ret
# return inner
#
# @wrapper #func = wrapper(func)
# def func(*args,**kwargs):
# pass

#分析用户输入的命令,按空格进行分割
#1) select查询功能
#2)定义输出的内容和格式顺序
#3)where操作部分,找出对应的列
#4)按照本处关键字定位列
#5)判断部分,判断为真就输出
#6)判断,或要查找的参数

#1,输入的命令按空格分开
#2,如命令第1个字符串是select,调用select_func函数
#2.1 检查长度,如等4,调用judge函数
#2.1.1,judge(),检查字符串中的> < =,执行文件行中的判断查找,并调用show_data函数输出
#2.2,如长度大于4 ,第5个字符是'like',调用search函数
#2.2.1,search(),检查对应列中的参数是否存在,并调用show_data函数输出
#2.3,show_data(),按逗号分割参数,把文件中对应参数的部分打印输出;如是*星号,打印本行即可

#3,命令如是quit就退出
'''



def select_func(*args):
#分析指令和参数
#查找文件内容并输出
'''
用途:select函数
变量列表:
com_item为要判断的列
com_value为要判断的值
com_print_index为要打印输出列的索引值
命令行命名com_list:select com_print_index where com_item>com_value
com = 'select * where age>22'
:param args:
:return:
'''

def get_index(i):
# if count == 0: # 确认是第一行,即格式行,任务:1查找输出项索引值;2找到判断项索引值
context_head_list = i.split(',')
context_head_length = len(context_head_list)
count1 = 0
flage = False
com_item_index = 0
while flage == False and len(context_head_list) > count1: # 找到要判断列的索引值
if com_item in context_head_list[count1]:
com_item_index = count1 # 拿到判断项的索引值
flage = True
count1 += 1
if count1 == context_head_length:
print('你要找的项中,有的不存在')
# 找到输出项列的索引值
com_list_item = com_list[1].split(',')
if com_list[1] == '*':
for ii in range(0, len(context_head_list)):
com_print_index.append(ii)
else:
for ii in com_list_item: # 对命令行输出项格式进行循环
count2 = 0
flag = False
while flag == False and context_head_length > count2: # 找要查找的列的索引值
if ii == context_head_list[count2]:
com_print_index.append(count2) # 拿到要输出项列的索引值
flag = True
count2 += 1
if count2 == context_head_length:
print('你要找的项中,有的不存在')
return com_print_index, com_item_index

def show_data(com_fomat, line):
# print(com_fomat,line)
line_print2 = []
if com_fomat == '*':
print(line.replace('\n', ''))
else:
line_print1 = line.replace('\n', '').split(',')
for i in list(com_fomat):
line_print2.append(line_print1[i])
print(' '.join(line_print2))

com_list = list(args)
# print(com_list)
with open('员工信息表.txt', mode='r', encoding='utf-8') as f:
count = 0
com_print_index = []
com_item_index = 0
context_head_list = []
if len(com_list) == 4: # 说明是<>=的判断操作
if '>'in com_list[3]: #说明是>查找
com_item,com_value = com_list[3].split('>')
for i in f:
# print('这是i', i,'还没有判断')
if count == 0: # 确认是第一行,即格式行,任务:1查找输出项索引值;2找到判断项索引值
com_print_index, com_item_index = get_index(i)
else: # 正式数据,非格式行,进行内容判断查找并打印
# print(count,context_head_list)
# 满足select中的查找条件时打印
context_list = i.replace('\n', '').split(',')
if int(context_list[com_item_index]) > int(com_value): # <>=运算,转成整型后运算
show_data(com_print_index, i)
count += 1
elif '<'in com_list[3]:
# print('处理<=部分')
com_item, com_value = com_list[3].split('<')
for i in f:
# print('这是i', i,'还没有判断')
if count == 0: # 确认是第一行,即格式行,任务:1查找输出项索引值;2找到判断项索引值
com_print_index, com_item_index = get_index(i)
else: # 正式数据,非格式行,进行内容判断查找并打印
# print(count,context_head_list)
# 满足select中的查找条件时打印
context_list = i.replace('\n', '').split(',')
if int(context_list[com_item_index]) < int(com_value): # <>=运算,转成整型后运算
show_data(com_print_index, i)
count += 1
else:
# print('处理剩下的=部分,不用再if语句判断了')
com_item, com_value = com_list[3].split('=')
for i in f:
# print('这是i', i,'还没有判断')
if count == 0: # 确认是第一行,即格式行,任务:1查找输出项索引值;2找到判断项索引值
com_print_index, com_item_index = get_index(i)
else: # 正式数据,非格式行,进行内容判断查找并打印
# print(count,context_head_list)
# 满足select中的查找条件时打印
context_list = i.replace('\n', '').split(',')
if context_list[com_item_index] == com_value: # =运算,可能是str或int
show_data(com_print_index, i)
count += 1
elif len(com_list) == 6 and 'like' in com_list: # 说明是like的判断操作
com_item, com_value = com_list[3], com_list[5]
for i in f:
# print('这是like', com_item, com_value)
if count == 0: # 确认是第一行,即格式行,任务:1查找输出项索引值;2找到判断项索引值
com_print_index, com_item_index = get_index(i)
else: # 正式数据,非格式行,进行内容判断查找并打印
# print(count,context_head_list)
# 满足select中的查找条件时打印
context_list = i.replace('\n', '').split(',')
if com_value in context_list[com_item_index]: # <>=运算,转成整型后运算
show_data(com_print_index, i)
count += 1


us_command = input('**员工信息表程序**\n>>>').strip()
us_command = us_command.split()
# print(us_command,len(us_command))
if us_command[0].lower() == 'select':
select_func(*us_command)
else:
print('对不起,命令无法识别!')
 
 
 
 
 

猜你喜欢

转载自www.cnblogs.com/yygy/p/10055675.html