作業3月24號

2022-09-06 13:03:26 字數 3924 閱讀 1738

1、編寫課上講解的有參裝飾器準備明天默寫

def

auth(db_type):

defdeco(func):

name = input('

your name>>>:

').strip()

pwd = input('

your password>>>:

').strip()

if db_type == '

file':

print('

基於檔案的驗證')

if name == '

egon

'and pwd == '

123'

: res = func(*args, **kwargs) #

index(1,2)

return

res

else

:

print('

user or password error')

elif db_type == '

mysql':

print('

基於mysql的驗證')

elif db_type == '

ldap':

print('

基於ldap的驗證')

else

:

print('

不支援該db_type')

return

return

deco

@auth(db_type='

file')

defindex(x, y):

print('

index->>%s:%s

' %(x, y))

@auth(db_type='

mysql')

defhome(name):

print('

home->>%s

' %name)

@auth(db_type='

ldap

')

deftransfer():

print('

transfer')

index(1, 2)

home(

'egon')

transfer()

2:還記得我們用函式物件的概念,製作乙個函式字典的操作嗎,來來來,我們有更高大上的做法,在檔案開頭宣告乙個空字典,然後在每個函式前加上裝飾器,完成自動新增到字典的操作

def

def

def inner(*args, **kwargs):

index = len(cmd_dict) + 1cmd_dict[str(index)] =(tips, func)

return

inner

return

def my_func = }

for func in

my_func.values():

func()登入'

)def

login():

print('

this is login')

'註冊')

defregister():

print('

this is register')

while 1:

for k, v in

cmd_dict.items():

print(f'

-'.center(20, '-'

)) cmd = input('

請輸出編號:

').strip()

if cmd not

incmd_dict:

print('

請重新輸入')

continue

func = cmd_dict.get(cmd)[1]

func()

3、 編寫日誌裝飾器,實現功能如:一旦函式f1執行,則將訊息2017-07-21 11:12:11 f1 run寫入到日誌檔案中,日誌檔案路徑可以指定

注意:時間格式的獲取

import time

time.strftime('%y-%m-%d %x')

def

info(path):

defoutter(func):

now=time.strftime('

%y-%m-%d %x')

with open(

'path

','a

',encoding='

utf-8

') as f:

f.write(now)

res = func(*args, **kwargs)

return

res

return

return

outter

path=input('

請輸入路徑')

@info(path)

deff1():

f1()

4、基於迭代器的方式,用while迴圈迭代取值字串、列表、元組、字典、集合、檔案物件

#

字串st='

hello

'res=st.__iter__

()while 1:

try:

print(res.__next__

())

except

stopiteration:

break#列表

lis=[1,2,3,4]

res=lis.__iter__

()while 1:

try:

print(res.__next__

())

except

stopiteration:

break#元組

tup=(1,2,3,4)

res=tup.__iter__

()while 1:

try:

print(res.__next__

())

except

stopiteration:

break#字典

dic=

res=dic.__iter__

()while 1:

try:

print(res.__next__

())

except

stopiteration:

break#集合

se=(1,2,[3,4,])

res=se.__iter__

()while 1:

try:

print(res.__next__

())

except

stopiteration:

break

#檔案物件

with open('

a.txt

','w

',encoding='

utf-8

') as f1:

while 1:

try:

print(f1.__next__

())

except

stopiteration:

break

5、自定義迭代器實現range功能

def

my_range(start,stop,step):

while start yield

start

start+=step

g=my_range(1,9,1)

for n in

g:

print(n)

作業 3月24日

作業 1 編寫課上講解的有參裝飾器準備明天默寫 def auth db type def otter func username input your name strip password input your pwd strip if db type file print 基於檔案的登陸 if ...

作業3月25號

1 檔案內容如下,標題為 姓名,性別,年紀,薪資 egon male 18 3000 alex male 38 30000 wupeiqi female 28 20000 yuanhao female 28 10000 要求 從檔案中取出每一條記錄放入列表中,列表的每個元素都是的形式 dic lis...

作業3月17號

1 編寫檔案修改功能,呼叫函式時,傳入三個引數 修改的檔案路徑,要修改的內容,修改後的內容 既可完成檔案的修改 def info import os path input 請輸入路徑 front input 請輸入修改內容 later input 請輸入修改後內容 with open path,r ...