No reference decorator practice

# A: write function (time function performed by time.sleep (n) analog) 
'' '
Import Time
DEF info ():
START_TIME the time.time = ()
the time.sleep (. 3)
Print (' Welcome king ! glory ')
stop_time the time.time = ()
Print ('% S iS RUN time '% (stop_time-START_TIME))

info ()
' ''
# II: preparation of decorators, as a function of time together with statistical features
' '
Import Time
DEF Timer (FUNC):
DEF warpper (* args, ** kwargs):
START_TIME the time.time = ()
RES = FUNC (* args, ** kwargs)
stop_time the time.time = ()
Print (' RUN Time % S IS '% (stop_time - START_TIME))
return RES
return warpper
@timer
DEF info ():
Time.sleep(3)
print ( 'Welcome to the king of glory!')

info ()
'' '
# three: write a decorator, as a function of plus certified function
' ''
DEF auth (FUNC):
DEF wrapper (* args, ** kwargs) :
name = iNPUT ( 'enter user name:') .strip ()
pwd = iNPUT ( 'enter user password:') .strip ()
IF name == 'Tank' and pwd == '123':
RES = FUNC (* args, ** kwargs)
return RES
the else:
Print ( 'account number or password is incorrect')
return wrapper

@auth
DEF index ():
Print ( '! certification success')

index ()
' ''
# four: decorative writing , a multiple function combined with certified function (from the user's account password file) require a login is successful, the follow-up functions are no longer need to enter a user name and password
# Note: read a string from a dictionary file, can eval ( '{ "name": "egon","password": "123"} ') turn into a dictionary format
' ''
DIC = {}
USER_INFO DEF (FUNC):
DEF warpper (* args, ** kwargs):
with Open ( 'db.txt', MODE = 'RT', encoding = 'UTF-. 8') AS F:
for in Line F:
User, pwd, Balance = line.strip () Split ( ':').
DIC [user] = [pwd, Balance]
username = iNPUT ( 'enter user name:') Strip ().
password = iNPUT ( 'enter user password: '.) Strip ()
IF username and password == == User DIC [User] [0]:
RES = FUNC (* args, ** kwargs)
return RES
the else:
Print ('! account number or password ')
warpper return

DEF InCount ():
( '! recharge') Print

DEF outcount ():
print ( 'transfer function!')
@user_info
DEF Users ():
the while True:
MSG = " '0': 'exit', '1': 'recharge', '2': 'Transfer'"
Print (MSG)
cmd = INPUT ( 'Enter command number >>: ') .strip ()
IF not cmd.isdigit ():
Print (' You must enter the command number of digital ')
the Continue
elif cmd not in msg:
Print (' enter the command does not exist ')
the Continue
the else:
IF cmd == '0':
BREAK
elif cmd == '. 1':
InCount ()
elif cmd == '2':
outcount ()

Users ()
'' '
# Five: write a decorator for several functions plus authentication, requires a successful login, without having to repeat the login within the timeout period, exceeding the timeout, you must log in again
'' '
Time Import
DIC = {}
DEF USER_INFO (FUNC):
DEF warpper (* args, ** kwargs):
with Open ( 'db.txt', MODE = 'RT', encoding = 'UTF-. 8') AS F:
for in F Line:
user, pwd, Balance = line.strip () Split. ( ':')
DIC [user] = [pwd, Balance]
username = iNPUT ( 'enter user name:') Strip ().
password = input ( 'enter user password:'). Strip ()
IF username and password == == user DIC [user] [0]:
RES = FUNC (* args, ** kwargs)
return RES
the else:
Print ( 'account or password is incorrect ')!
return wrapper

DEF the Timer (FUNC):
def wrapper(*args,**kwargs):
= time.time start_time ()
RES = FUNC (* args, ** kwargs)
stop_time = time.time ()
Times = stop_time - start_time
IF Times <= 1:
return RES
the else:
Print ( 'login timeout, please log in again! ')
return warpper

@timer
DEF InCount ():
! Print (' recharge ')

@timer
DEF outcount ():
Print (' transfer function! ')

@user_info
DEF Users ():
the while True:
MSG = "' 0 ' : 'exit', '1': 'recharge', '2': 'transfer' "
Print (msg)
cmd = the iNPUT ( 'enter the command number >>:') .strip ()
IF not cmd.isdigit () :
print ( 'command must be input digital number')
Continue
Not in cmd MSG elif:
Print ( 'input command does not exist')
Continue
the else:
IF cmd == '0':
BREAK
elif cmd == '. 1':
InCount ()
elif cmd == '2':
outcount ()

Users ()
'' '

Guess you like

Origin www.cnblogs.com/0B0S/p/12555089.html