python學習之無參裝飾器作業

2022-06-12 13:12:11 字數 3997 閱讀 4689

一:編寫函式,(函式執行的時間用time.sleep(n)模擬)

import time

def my_index(x,y):

time.sleep(3)

print('{},{},welcome to my index page'.format(x,y)

return 100

二:編寫裝飾器,為函式加上統計時間的功能

import time

def timmer(func):

start_time = time.time()

res = func(*args, **kwargs)

end_time = time.time()

print('run time is {} s.'.format(end_time - start_time))

return res

@timmer

def my_index(x, y):

time.sleep(3)

print('{}, {}, welcome to my index.'.format(x,y))

return 100

res = my_index('egon','tank')

print(res)

三:編寫裝飾器,為函式加上認證的功能

import time

def auth(func):

inp_name = input('請輸入你的賬號:').strip()

inp_pwd = input('請輸入你的密碼:').strip()

if inp_name == 'vincent' and inp_pwd == '666':

res = func(*args,**kwargs)

return res

else:

print('賬號或密碼錯誤。')

@auth

def my_index(x,y):

time.sleep(3)

print('{}, {},welcome to my index.'.format(x,y))

return 100

my_index('egon','tank')

四:編寫裝飾器,為多個函式加上認證的功能(使用者的賬號密碼**於檔案),要求登入成功一次,後續的函式都無需再輸入使用者名稱和密碼;

注意:從檔案中讀出字串形式的字典,可以用eval('')轉成字典格式

def auth(func):

global login_status

if not login_status:

inp_name = input('請輸入你的賬號:').strip()

inp_pwd = input('請輸入你的密碼:').strip()

with open('db.txt','rt',encoding='utf8') as f:

for line in f:

user_info = eval(line.strip())

if inp_name == user_info.get('name') and inp_pwd == user_info.get('password'):

print('登入成功。')

login_status = 1

res = func(*args,**kwargs)

return res

else:

print('賬號或密碼錯誤,登入失敗。')

return

res = func(*args,**kwargs)

return res

import time

@auth

def index(x,y):

time.sleep(3)

print('{}, {},from index page.'.format(x,y))

return 'return from index'

@auth

def home(name):

time.sleep(2)

print('{} from home page.'.format(name))

return 'return from home'

login_status = 0

index('張三','李四')

home('王五')

index('張三','李四')

五:編寫裝飾器,為多個函式加上認證功能,要求登入成功一次,在超時時間內無需重複登入,超過了超時時間,則必須重新登入

def auth(func):

global login_status,login_time

if not login_status:

inp_name = input('請輸入你的賬號:').strip()

inp_pwd = input('請輸入你的密碼:').strip()

with open('db.txt','rt',encoding='utf8') as f:

for line in f:

user_info = eval(line.strip())

if inp_name == user_info.get('name') and inp_pwd == user_info.get('password'):

print('登入成功。')

login_status = 1

login_time = time.time()

res = func(*args,**kwargs)

return res

else:

print('賬號或密碼錯誤,登入失敗。')

return

else:

now_time = time.time()

if now_time - login_time <= 10:

login_status = 1

login_time = time.time()

res = func(*args, **kwargs)

return res

else:

login_status = 0

inp_name = input('請輸入你的賬號:').strip()

inp_pwd = input('請輸入你的密碼:').strip()

with open('db.txt', 'rt', encoding='utf8') as f:

for line in f:

user_info = eval(line.strip())

if inp_name == user_info.get('name') and inp_pwd == user_info.get('password'):

print('登入成功。')

login_status = 1

login_time = time.time()

res = func(*args, **kwargs)

return res

else:

print('賬號或密碼錯誤,登入失敗。')

return

import time

@auth

def index(x,y):

time.sleep(3)

print('{}, {},from index page.'.format(x,y))

return 'return from index'

@auth

def home(name):

time.sleep(2)

print('{} from home page.'.format(name))

return 'return from home'

login_status = 0

login_time = none

index('張三','李四')

time.sleep(5)

home('王五')

Python 有參裝飾器

由於語法糖 的限制,outter函式只能有乙個引數,並且該才是只用來接收 被裝飾物件的記憶體位址 def outter func func 函式的記憶體位址 def args,kwargs res func args,kwargs return res outter outter index def...

python之有參裝飾器

由於語法糖 的限制,outter函式只能有乙個引數,並且該引數只用來接收被裝飾物件的記憶體位址 def outter func func 函式的記憶體位址 res func args,kwargs return res outter outter outter index def index x,y...

python裝飾器 有殘裝飾器 無參裝飾器

裝飾器 修飾別人的工具,修飾新增功能,工具指的是函式 裝飾器本身可以是任何可呼叫物件,被裝飾的物件也可以是任意可呼叫物件 為什麼要用裝飾器 開放封閉原則 對修改是封閉的,對擴充套件是開放的 裝飾器就是為了在不修改被裝飾物件的源 以及呼叫方式的前提下,為期新增新功能 基本裝飾器 usr bin pyt...