shelve python 的一个简易数据库包和hashlib生产md5加密的包

#!/usr/bin/python
#coding=utf-8

import hashlib

a = "a test string"
print 'md5:', hashlib.md5(a).hexdigest()

#!/usr/bin/env python
#coding=utf-8
'''
shelve python 的一个建议数据库
'''
import shelve
key = '0101'
def save(db):

    db[key] = 'my name is aircoder!'
    print ' I has save a data in python small db!'
def load(db):
    value = db[key]
    print 'value is:',value

if __name__ == '__main__':

    db = shelve.open('db');
    try:
        kind = raw_input('Enter your want:')
        if kind == 'save':
              save(db)
        elif kind == 'load':
              load(db)
        else:
              print 'you are gum!'
    finally:
        db.close()


import shelve

d = shelve.open(filename) # open -- file may get suffix added by low-level
                          # library

d[key] = data   # store data at key (overwrites old data if
                # using an existing key)
data = d[key]   # retrieve a COPY of data at key (raise KeyError if no
                # such key)
del d[key]      # delete data stored at key (raises KeyError
                # if no such key)
flag = d.has_key(key)   # true if the key exists
klist = d.keys() # a list of all existing keys (slow!)

# as d was opened WITHOUT writeback=True, beware:
d['xx'] = range(4)  # this works as expected, but...
d['xx'].append(5)   # *this doesn't!* -- d['xx'] is STILL range(4)!

# having opened d without writeback=True, you need to code carefully:
temp = d['xx']      # extracts the copy
temp.append(5)      # mutates the copy
d['xx'] = temp      # stores the copy right back, to persist it

# or, d=shelve.open(filename,writeback=True) would let you just code
# d['xx'].append(5) and have it work as expected, BUT it would also
# consume more memory and make the d.close() operation slower.

d.close() 

你可能感兴趣的:(python)