|
這幾天接觸了Python中的shelve這個(gè)module,感覺(jué)比pickle用起來(lái)更簡(jiǎn)單一些,它也是一個(gè)用來(lái)持久化Python對(duì)象的簡(jiǎn)單工具。當(dāng)我們寫(xiě)程序的時(shí)候如果不想用關(guān)系數(shù)據(jù)庫(kù)那么重量級(jí)的東東去存儲(chǔ)數(shù)據(jù),不妨可以試試用shelve。shelf也是用key來(lái)訪(fǎng)問(wèn)的,使用起來(lái)和字典類(lèi)似。shelve其實(shí)用anydbm去創(chuàng)建DB并且管理持久化對(duì)象的。
創(chuàng)建一個(gè)新的shelf直接使用shelve.open()就可以創(chuàng)建了 1 import shelve 2 3 s = shelve.open('test_shelf.db') 4 try: 5 s['key1'] = { 'int': 10, 'float':9.5, 'string':'Sample data' } 6 finally: 7 s.close()
如果想要再次訪(fǎng)問(wèn)這個(gè)shelf,只需要再次shelve.open()就可以了,然后我們可以像使用字典一樣來(lái)使用這個(gè)shelf 1 import shelve 2 3 s = shelve.open('test_shelf.db') 4 try: 5 existing = s['key1'] 6 finally: 7 s.close() 8 9 print existing
當(dāng)我們運(yùn)行以上兩個(gè)py,我們將得到如下輸出: $ python shelve_create.py
$ python shelve_existing.py
{'int': 10, 'float': 9.5, 'string': 'Sample data'}
dbm這個(gè)模塊有個(gè)限制,它不支持多個(gè)應(yīng)用同一時(shí)間往同一個(gè)DB進(jìn)行寫(xiě)操作。所以當(dāng)我們知道我們的應(yīng)用如果只進(jìn)行讀操作,我們可以讓shelve通過(guò)只讀方式打開(kāi)DB: 1 import shelve 2 3 s = shelve.open('test_shelf.db', flag='r') 4 try: 5 existing = s['key1'] 6 finally: 7 s.close() 8 9 print existing 當(dāng)我們的程序試圖去修改一個(gè)以只讀方式打開(kāi)的DB時(shí),將會(huì)拋一個(gè)訪(fǎng)問(wèn)錯(cuò)誤的異常。異常的具體類(lèi)型取決于anydbm這個(gè)模塊在創(chuàng)建DB時(shí)所選用的DB。
寫(xiě)回(Write-back)由于shelve在默認(rèn)情況下是不會(huì)記錄待持久化對(duì)象的任何修改的,所以我們?cè)趕helve.open()時(shí)候需要修改默認(rèn)參數(shù),否則對(duì)象的修改不會(huì)保存。 1 import shelve 2 3 s = shelve.open('test_shelf.db') 4 try: 5 print s['key1'] 6 s['key1']['new_value'] = 'this was not here before' 7 finally: 8 s.close() 9 10 s = shelve.open('test_shelf.db', writeback=True) 11 try: 12 print s['key1'] 13 finally: 14 s.close() 上面這個(gè)例子中,由于一開(kāi)始我們使用了缺省參數(shù)shelve.open()了,因此第6行修改的值即使我們s.close()也不會(huì)被保存。 執(zhí)行結(jié)果如下: $ python shelve_create.py
$ python shelve_withoutwriteback.py
{'int': 10, 'float': 9.5, 'string': 'Sample data'}
{'int': 10, 'float': 9.5, 'string': 'Sample data'}
所以當(dāng)我們?cè)噲D讓shelve去自動(dòng)捕獲對(duì)象的變化,我們應(yīng)該在打開(kāi)shelf的時(shí)候?qū)riteback設(shè)置為T(mén)rue。當(dāng)我們將writeback這個(gè)flag設(shè)置為T(mén)rue以后,shelf將會(huì)將所有從DB中讀取的對(duì)象存放到一個(gè)內(nèi)存緩存。當(dāng)我們close()打開(kāi)的shelf的時(shí)候,緩存中所有的對(duì)象會(huì)被重新寫(xiě)入DB。 1 import shelve 2 3 s = shelve.open('test_shelf.db', writeback=True) 4 try: 5 print s['key1'] 6 s['key1']['new_value'] = 'this was not here before' 7 print s['key1'] 8 finally: 9 s.close() 10 11 s = shelve.open('test_shelf.db', writeback=True) 12 try: 13 print s['key1'] 14 finally: 15 s.close() writeback方式有優(yōu)點(diǎn)也有缺點(diǎn)。優(yōu)點(diǎn)是減少了我們出錯(cuò)的概率,并且讓對(duì)象的持久化對(duì)用戶(hù)更加的透明了;但這種方式并不是所有的情況下都需要,首先,使用writeback以后,shelf在open()的時(shí)候會(huì)增加額外的內(nèi)存消耗,并且當(dāng)DB在close()的時(shí)候會(huì)將緩存中的每一個(gè)對(duì)象都寫(xiě)入到DB,這也會(huì)帶來(lái)額外的等待時(shí)間。因?yàn)閟helve沒(méi)有辦法知道緩存中哪些對(duì)象修改了,哪些對(duì)象沒(méi)有修改,因此所有的對(duì)象都會(huì)被寫(xiě)入。 1 $ python shelve_create.py 2 $ python shelve_writeback.py 3 4 {'int': 10, 'float': 9.5, 'string': 'Sample data'} 5 {'int': 10, 'new_value': 'this was not here before', 'float': 9.5, 'string': 'Sample data'} 6 {'int': 10, 'new_value': 'this was not here before', 'float': 9.5, 'string': 'Sample data'}
最后再來(lái)個(gè)復(fù)雜一點(diǎn)的例子: 1 #!/bin/env python 2 3 import time 4 import datetime 5 import md5 6 import shelve 7 8 LOGIN_TIME_OUT = 60 9 db = shelve.open('user_shelve.db', writeback=True) 10 11 def newuser(): 12 global db 13 prompt = "login desired: " 14 while True: 15 name = raw_input(prompt) 16 if name in db: 17 prompt = "name taken, try another: " 18 continue 19 elif len(name) == 0: 20 prompt = "name should not be empty, try another: " 21 continue 22 else: 23 break 24 pwd = raw_input("password: ") 25 db[name] = {"password": md5_digest(pwd), "last_login_time": time.time()} 26 #print '-->', db 27 28 def olduser(): 29 global db 30 name = raw_input("login: ") 31 pwd = raw_input("password: ") 32 try: 33 password = db.get(name).get('password') 34 except AttributeError, e: 35 print "\033[1;31;40mUsername '%s' doesn't existed\033[0m" % name 36 return 37 if md5_digest(pwd) == password: 38 login_time = time.time() 39 last_login_time = db.get(name).get('last_login_time') 40 if login_time - last_login_time < LOGIN_TIME_OUT: 41 print "\033[1;31;40mYou already logged in at: <%s>\033[0m" % datetime.datetime.fromtimestamp(last_login_time).isoformat() 42 43 db[name]['last_login_time'] = login_time 44 print "\033[1;32;40mwelcome back\033[0m", name 45 else: 46 print "\033[1;31;40mlogin incorrect\033[0m" 47 48 def md5_digest(plain_pass): 49 return md5.new(plain_pass).hexdigest() 50 51 def showmenu(): 52 #print '>>>', db 53 global db 54 prompt = """ 55 (N)ew User Login 56 (E)xisting User Login 57 (Q)uit 58 Enter choice: """ 59 done = False 60 while not done: 61 chosen = False 62 while not chosen: 63 try: 64 choice = raw_input(prompt).strip()[0].lower() 65 except (EOFError, KeyboardInterrupt): 66 choice = "q" 67 print "\nYou picked: [%s]" % choice 68 if choice not in "neq": 69 print "invalid option, try again" 70 else: 71 chosen = True 72 73 if choice == "q": done = True 74 if choice == "n": newuser() 75 if choice == "e": olduser() 76 db.close() 77 78 if __name__ == "__main__": 79 showmenu()
|
|
|