来自CodernityDB.database导入数据库 来自CodernityDB.hash_index导入HashIndex
class WithXIndex(HashIndex):
def __init__(self, *args, **kwargs):
kwargs['key_format'] = '16s'
super(WithXIndex, self).__init__(*args, **kwargs)
def make_key_value(self, data):
username = data['username']
# if not isinstance(login, basestring):
# login = str(login)
return md5(username).digest(), None
def make_key(self, key):
return md5(key).digest()
def main(): db =数据库('l1.db')
if db.exists():
db.open()
else:
db.create()
x_ind = WithXIndex(db.path, 'username')
db.add_index(x_ind)
db.insert(dict( username='lamar', age='33', frm='new jersey'))
for dt in db.all('username',with_doc=True):
print dt
print db.get('username', 'lamar') # throws an exception