This repository has been archived by the owner on Jan 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathsimpledb.py
106 lines (77 loc) · 2.61 KB
/
simpledb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# simpledb.py -- a nice and simple database
# Written by Joe Salisbury <[email protected]>
#
# You are free to use this code in anyway you see fit, on the basis
# that if it is used, modified, or distributed, proper accreditation
# of the original author remains.
""" A nice and simple database class. """
# In a nutshell, the Database class acts like a dictionary, and
# implements most of the built-in dictionaries functions. Except its
# persistent!
# As bsddb can only accept strings for keys and values, we need to
# pickle everything before we use it. Therefore, most of the functions
# dump the data, interface with the dict, then load the results.
from bsddb import hashopen
from pickle import dumps, loads
class Database():
""" A wrapper around a bsddb database, acting as a dictionary.
Can accept all Python datatypes as keys, and values. """
def __init__(self, dbname, flag="c"):
""" Read the database given by dbname. """
self.data = hashopen(dbname, flag)
def __contains__(self, key):
""" Return true if the database contains the key. """
key = dumps(key)
boolean = self.data.has_key(key) # Returns 1 or 0.
return bool(boolean)
def __getitem__(self, key):
""" Return the value held by the key. """
key = dumps(key)
value = self.data[key]
return loads(value)
has_key = __contains__
get = __getitem__
def __setitem__(self, key, value):
""" Set the value of key to the value given. """
key = dumps(key)
value = dumps(value)
self.data[key] = value
def __repr__(self):
""" Represent the database. """
keys = self.data.keys()
items = [(loads(key), loads(self.data[key])) for key in keys]
return str(dict(items))
def clear(self):
""" Remove all data in the database. """
self.data.clear()
def items(self):
""" Return a list of tuples of the keys and values. """
keys = self.data.keys()
items = [(loads(key), loads(self.data[key])) for key in keys]
return items
def keys(self):
""" Return a list of keys. """
keys = [loads(key) for key in self.data.keys()]
return keys
def values(self):
""" Return a list of values. """
values = [loads(value) for value in self.data.values()]
return values
def pop(self, key):
""" Return the value given by key, and remove it. """
key = dumps(key)
value = self.data[key]
del self.data[key]
return loads(value)
def setdefault(self, key, default):
""" Return the value held by key, or default if it isn't in
the database. """
key = dumps(key)
try:
value = self.data[key]
except KeyError:
return default
return loads(value)
def __del__(self):
""" Sync the database. """
self.data.sync()