forked from aosabook/500lines
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface.py
More file actions
43 lines (32 loc) · 992 Bytes
/
Copy pathinterface.py
File metadata and controls
43 lines (32 loc) · 992 Bytes
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
from dbdb.binary_tree import BinaryTree
from dbdb.physical import Storage
class DBDB(object):
def __init__(self, f):
self._storage = Storage(f)
self._tree = BinaryTree(self._storage)
def _assert_not_closed(self):
if self._storage.closed:
raise ValueError('Database closed.')
def close(self):
self._storage.close()
def commit(self):
self._assert_not_closed()
self._tree.commit()
def __getitem__(self, key):
self._assert_not_closed()
return self._tree.get(key)
def __setitem__(self, key, value):
self._assert_not_closed()
return self._tree.set(key, value)
def __delitem__(self, key):
self._assert_not_closed()
return self._tree.pop(key)
def __contains__(self, key):
try:
self[key]
except KeyError:
return False
else:
return True
def __len__(self):
return len(self._tree)