python class XII

# Review 
# work
# decorator Advanced
# functools.wraps
# arguments decorative KKD
# more decorative decorator same function
# decorator
# development principles: Open Closed Principle
# decorator role: without changing the original function in the case of a call mode, a function to add functionality around
nature # decorators: closure function
# Import from functools Wraps
# DEF warpper (FUNC): # = Holiday FUNC
# @wraps (FUNC)
# Inner DEF (* args, kwargs **):
# Print ( 'function is executed before being decorated to do')
# RET = FUNC (* args, ** kwargs)
# Print ( 'function is executed after being decorated to do')
# RET return
# Inner return
#
# # @wrapper warpper = Holiday (Holiday)
# DEF Holiday (day):
# '' 'which is a holiday notice' ''
# Print ( '% s all holiday days' day%)
# return 'love you Kai Li'
#
Print # (Holiday .__ name__)
# Print (Holiday .__ doc__ displays a)
# RET = Holiday (. 3) #inner
# Print (SET)
#
# # Outer DEF (* args, ** kwargs):
# # Print (args)
# # Print (* args)
# # Inner DEF (* args):
# # Print ( 'Inner:', args)
# # Inner (* args)
# #
# # Outer (1,2,3,4) == # Outer (* [1,2,3,4]) == # Outer (* (1,2,3,4))
#
# #. 1. decorators written as a function with the authentication function of a plurality of (the user's account password from file),
## requires a successful landing, the follow-up functions are no longer need to enter a user name and password
# FLAG = False
# DEF the login (FUNC):
# DEF Inner (* args, ** kwargs):
#, Ltd. Free Join FLAG
# '' 'landing procedures' ''
# IF FLAG:
# RET = FUNC (* args, ** kwargs) # FUNC is decorated function
Return RET #
# the else:
# username = INPUT ( 'username:')
# = INPUT password ( 'password:')
# username IF == 'boss_gold' and password == '222222':
# the FLAG = True
# RET = FUNC (* args, ** kwargs) # func is decorated function
# return RET
# the else:
# Print ( 'login failed')
# return Inner
# @login
# DEF shoplist_add ():
# Print ( 'increase an item' )
# @login
# DEF shoplist_del ():
# Print ( 'delete an item')
#
# shoplist_add ()
# shoplist_del ()

# 2 written decorator, plus call recording function for multiple functions.Each call requires function function name will be written to a file called
# def log (func):
Inner DEF # (* args, ** kwargs):
# with Open ( 'log', 'A', encoding = 'UTF-. 8') AS F:
# f.write (FUNC .__ name __ + '\ n-')
# FUNC = RET (* args, ** kwargs)
# return RET
# return Inner
# @log
# DEF shoplist_add ():
# Print ( 'increase an item')
# @log
# DEF shoplist_del ():
# Print ( 'delete an item ')
# shoplist_add ()
# shoplist_del ()

. # 1 written web content download function, function is required: users to pass a url, the function returns the result of the download page of the
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:# Judge whether the content file
f.read return ()
RET = FUNC (* args, ** kwargs) #get () requests a web page
with open ( 'web_cache', ' wb') as f: # result of the request written to a file
f.write ( B '****' + RET)
return RET
return Inner

@cache
DEF GET (URL):
code = the urlopen (URL) .read
return code
RET = GET ( 'http://www.baidu.com')
Print ( RET)
RET = GET ( 'http://www.baidu.com')
Print (RET)
Generator function # 
# DEF Generator ():
# Print (. 1)
# return 'A'
# RET = Generator ()
# Print (RET)

Function # Function long as it contains all yield keyword generator
#yield can not return work with and requires written in the function
# DEF generator ():
# Print (. 1)
# the yield 'a'
# # generator function: get a generator after the execution as a return value
# RET = generator ()
# Print (RET )
# Print (RET .__ Next __ ())

# DEF Generator ():
# Print (. 1)
# the yield 'A'
# Print (2)
# the yield 'B'
# G = Generator ()
# for I in G:
# Print (I)
# G RET = __ .__ Next ()
# Print (RET)
# G RET = __ .__ Next ()
# Print (RET)

# I wahaha%
# DEF Wahaha ():
# for i in range(100):
# yield '娃哈哈%s'%i
#
# g=wahaha()
# count=0
# for i in g:
# count +=1
# print(i)
# if count>50:
# break
# #print('*******',g.__next__())
# count=0
# for i in g:
# count +=1
# print(i)
# if count>100:
# break

#带参数的装饰器
#500个函数
import time
FLAG=True
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(FLAG)
@timmer_out(FLAG) #wahaha=timmer(wahaha)
def wahaha():
time.sleep(0.1)
print('wahawaha')
@timmer_out(FLAG)
def erguotou():
time.sleep(0.1)
print('ergoutoutoutotu')

wahaha()
erguotou()
def tail(filename):
f=open('file',encoding='utf-8')
while True:
line=f.readline()
if line.strip():
yield line.strip()
g=tail('file')
for i in g:
if '启丽' in i:
print('我喜欢',i)


Guess you like

Origin www.cnblogs.com/huangjianfeng/p/11334678.html
Recommended