Python打卡第四周
這一周鴿了好久, 因?yàn)樽罱跍?zhǔn)備比賽。課程一直沒(méi)跟上。。。
今天整理完本周的知識(shí)之后休息一周復(fù)習(xí)之前的
好了。
上總結(jié)
?
第一天
定義函數(shù)bar,在foo中調(diào)用
# def foo(): # # print('in the foo') # # bar() # # foo()def bar():print('in the bar')def foo():print('in the foo')bar()foo()?
在函數(shù)中嵌套一層,最內(nèi)層的函數(shù)結(jié)束后必須在下面執(zhí)行一次,要不然bar函數(shù)不會(huì)執(zhí)行
def foo():print('in the foo')def bar():print('in the bar')bar() foo()?
使用timer裝飾器裝飾函數(shù),并輸出運(yùn)行時(shí)間
import timedef timmer(func):def warpper(*args,**kwargs):start_time = time.time()func()stop_time = time.time()print('the func run time is %s'%(stop_time-start_time))return warpper@timmer def test1():time.sleep(3)print('in the test1')test1()?
?
import time def timer(func):def deco(*args,**kwargs):star_time = time.time()func(*args,**kwargs)stop_time = time.time()print("the func run time is %s" %(stop_time-star_time))return deco@timer #test1 = timer(test1) def test1():time.sleep(3)print('in the test1')@timer def test2(name):print("test2 : ",name)test1() test2("Louis")?
?
第二天
?
生成器,可以直接把結(jié)果生成在一個(gè)函數(shù)中,也可以使用生成器的方式,將規(guī)則寫出來(lái)然后賦值給一個(gè)函數(shù),這個(gè)函數(shù)只會(huì)保留地址,不會(huì)真的一次性賦值。等到輸出的時(shí)候使用__next__進(jìn)行調(diào)用。
a=[0,1,2,3,4,5,6,7,8,9]c = (i*2 for i in range(1000))# for i in c: # print(i)print(c) c.__next__()?
將字符串類型轉(zhuǎn)換成迭代形式。使用iter函數(shù)
a = [1,2,3] b = iter(a) b.__next__()?
斐波那契數(shù)列
next:返回到迭代器
return:在程序函數(shù)中返回某個(gè)值,返回之后函數(shù)不在繼續(xù)執(zhí)行,徹底結(jié)束。
yield:?帶有yield的函數(shù)是一個(gè)迭代器,函數(shù)返回某個(gè)值時(shí),會(huì)停留在某個(gè)位置,返回函數(shù)值后,會(huì)在前面停留的位置繼續(xù)執(zhí)行,直到程序結(jié)束
def fib(max):n,a,b=0,0,1while n<max:#print(b)yield ba,b =b,a+b # t = (b,a+b)n=n+1return '-----done-----'#f = fib(10) g = fib(10) while True:try:x = next(g)print('g:',x)except StopIteration as e:print('Generator return value:',e.value)break # print(f.__next__()) # print("------") # print(f.__next__()) # print(f.__next__()) # print(f.__next__()) # # print("=====start loop=====") # for i in f: # print(i)?
迭代器并行
import time def consumer(name):print("%s 準(zhǔn)備吃包子"%name)while True:baozi = yieldprint("包子[%s]來(lái)了,被[%s]吃了" %(baozi,name))c = consumer("xiaoming") c.__next__() # b1="韭菜餡" # c.send(b1) #c.__next__()def producer(name):c = consumer('A')c2 = consumer('B')c.__next__()c2.__next__()print("老子開(kāi)始準(zhǔn)備做包子了!")for i in range(10):time.sleep(1)print("做了2個(gè)包子!")c.send(i)c2.send(i)producer("louis")?
第三天
json模塊:只適用于簡(jiǎn)單的數(shù)據(jù)類型,是一種跨平臺(tái)的模塊。
pickle模塊:能夠轉(zhuǎn)換傳遞復(fù)雜的數(shù)據(jù)類型,是Python特有的一種數(shù)據(jù)類型。
#import json#不同語(yǔ)言的程序進(jìn)行交互 import pickle def sayhi(name):print("hello",name)info ={'name':'louis','age':18'func':sayhi } f = open("test.txt","wb") #f.write(str(info))#f.write(json.dumps(info)) f.write(pickle.dumps(info)) f.close()?
反序列化
使用json的時(shí)候我們可以使用load方法來(lái)代替loads方法:只需要將f.write(json.dumps(info))替換成json.dump(info,f)就可以了
#import json import pickledef sayhi(name):print("hello",name) f = open("test.txt","rb")# data = eval(f.read()) # f.close() # print(data['age']) data = pickle.loads(f.read()) print(data["func"]("louis"))?
import pickledef sayhi(name):print("hello2",name)f = open("test.txt","rb") data = pickle.load(f) print(data["func"]("louis"))?
第四天
?
第五天
軟件目錄結(jié)構(gòu)規(guī)范
bin/: 存放項(xiàng)目的一些可執(zhí)行文件,當(dāng)然你可以起名script/之類的也行。
"自定義文件名"/: 存放項(xiàng)目的所有源代碼。
docs/: 存放一些文檔。
setup.py: 安裝、部署、打包的腳本。
requirements.txt: 存放軟件依賴的外部Python包列表。
README: 項(xiàng)目說(shuō)明文件。
?
總結(jié):這一周不怎么在狀態(tài),決定休息一周,重新復(fù)習(xí)一遍之前所學(xué)的內(nèi)容。盲目的前進(jìn)會(huì)事倍功半。
轉(zhuǎn)載于:https://www.cnblogs.com/yuanjun93/p/10758666.html
總結(jié)
以上是生活随笔為你收集整理的Python打卡第四周的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Saiku Table展示数据合并bug
- 下一篇: 去死吧!USB转串口!!!