shelve模块是一个简单的k,v将内存数据经过文件持久化的模块,能够持久化任何pickle可支持的python数据格式html
建立一个新的shelfpython
直接使用shelve.open()就能够建立了
复制代码web
import shelve
s = shelve.open('test_shelf.db')
try:
s['key1'] = { 'int': 10, 'float':9.5, 'string':'Sample data' }
finally:
s.close()
若是想要再次访问这个shelf,只须要再次shelve.open()就能够了,而后咱们能够像使用字典同样来使用这个shelf
复制代码缓存
import shelve
s = shelve.open('test_shelf.db')
try:
existing = s['key1']
finally:
s.close()
print existing
当咱们运行以上两个py,咱们将获得以下输出:ruby
$ python shelve_create.py
$ python shelve_existing.py
{'int': 10, 'float': 9.5, 'string': 'Sample data'}
dbm这个模块有个限制,它不支持多个应用同一时间往同一个DB进行写操做。因此当咱们知道咱们的应用若是只进行读操做,咱们可让shelve经过只读方式打开DB:svg
import shelve
s = shelve.open('test_shelf.db', flag='r')
try:
existing = s['key1']
finally:
s.close()
print existing
当咱们的程序试图去修改一个以只读方式打开的DB时,将会抛一个访问错误的异常。异常的具体类型取决于anydbm这个模块在建立DB时所选用的DB。ui
写回(Write-back)this
因为shelve在默认状况下是不会记录待持久化对象的任何修改的,因此咱们在shelve.open()时候须要修改默认参数,不然对象的修改不会保存。
复制代码spa
import shelve
s = shelve.open('test_shelf.db')
try:
print s['key1']
s['key1']['new_value'] = 'this was not here before'
finally:
s.close()
s = shelve.open('test_shelf.db', writeback=True)
try:
print s['key1']
finally:
s.close()
上面这个例子中,因为一开始咱们使用了缺省参数shelve.open()了,所以第6行修改的值即便咱们s.close()也不会被保存。code
执行结果以下:
$ python shelve_create.py
$ python shelve_withoutwriteback.py
{'int': 10, 'float': 9.5, 'string': 'Sample data'}
{'int': 10, 'float': 9.5, 'string': 'Sample data'}
因此当咱们试图让shelve去自动捕获对象的变化,咱们应该在打开shelf的时候将writeback设置为True。当咱们将writeback这个flag设置为True之后,shelf将会将全部从DB中读取的对象存放到一个内存缓存。当咱们close()打开的shelf的时候,缓存中全部的对象会被从新写入DB。
复制代码
import shelve
s = shelve.open('test_shelf.db', writeback=True)
try:
print s['key1']
s['key1']['new_value'] = 'this was not here before'
print s['key1']
finally:
s.close()
s = shelve.open('test_shelf.db', writeback=True)
try:
print s['key1']
finally:
s.close()
writeback方式有优势也有缺点。优势是减小了咱们出错的几率,而且让对象的持久化对用户更加的透明了;但这种方式并非全部的状况下都须要,首先,使用writeback之后,shelf在open()的时候会增长额外的内存消耗,而且当DB在close()的时候会将缓存中的每个对象都写入到DB,这也会带来额外的等待时间。由于shelve没有办法知道缓存中哪些对象修改了,哪些对象没有修改,所以全部的对象都会被写入。
复制代码
$ python shelve_create.py
$ python shelve_writeback.py
{'int': 10, 'float': 9.5, 'string': 'Sample data'}
{'int': 10, 'new_value': 'this was not here before', 'float': 9.5, 'string': 'Sample data'}
{'int': 10, 'new_value': 'this was not here before', 'float': 9.5, 'string': 'Sample data'}
最后再来个复杂一点的例子:
复制代码
#!/bin/env python
import time
import datetime
import md5
import shelve
LOGIN_TIME_OUT = 60
db = shelve.open('user_shelve.db', writeback=True)
def newuser():
global db
prompt = "login desired: "
while True:
name = raw_input(prompt)
if name in db:
prompt = "name taken, try another: "
continue
elif len(name) == 0:
prompt = "name should not be empty, try another: "
continue
else:
break
pwd = raw_input("password: ")
db[name] = {"password": md5_digest(pwd), "last_login_time": time.time()}
#print '-->', db
def olduser():
global db
name = raw_input("login: ")
pwd = raw_input("password: ")
try:
password = db.get(name).get('password')
except AttributeError, e:
print "\033[1;31;40mUsername '%s' doesn't existed\033[0m" % name
return
if md5_digest(pwd) == password:
login_time = time.time()
last_login_time = db.get(name).get('last_login_time')
if login_time - last_login_time < LOGIN_TIME_OUT:
print "\033[1;31;40mYou already logged in at: <%s>\033[0m" % datetime.datetime.fromtimestamp(last_login_time).isoformat()
db[name]['last_login_time'] = login_time
print "\033[1;32;40mwelcome back\033[0m", name
else:
print "\033[1;31;40mlogin incorrect\033[0m"
def md5_digest(plain_pass):
return md5.new(plain_pass).hexdigest()
def showmenu():
#print '>>>', db
global db
prompt = """ (N)ew User Login (E)xisting User Login (Q)uit Enter choice: """
done = False
while not done:
chosen = False
while not chosen:
try:
choice = raw_input(prompt).strip()[0].lower()
except (EOFError, KeyboardInterrupt):
choice = "q"
print "\nYou picked: [%s]" % choice
if choice not in "neq":
print "invalid option, try again"
else:
chosen = True
if choice == "q": done = True
if choice == "n": newuser()
if choice == "e": olduser()
db.close()
if __name__ == "__main__":
showmenu()
转自:https://www.cnblogs.com/frankzs/p/5949645.html