-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnumpydb.py
76 lines (59 loc) · 2.06 KB
/
numpydb.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
# -*- coding: utf-8 -*-
import UserDict
import numpy as np
import plyvel
class NumpyDB(UserDict.DictMixin):
def __init__(self, filename, create_if_missing=True, sync=True):
self.db = plyvel.DB(filename, create_if_missing=create_if_missing)
self.sync = sync
def __getitem__(self, key):
if isinstance(key, unicode):
key = key.encode('utf-8')
try:
return self.unpack(self.db.get(key))
except Exception:
return None
def __setitem__(self, key, value):
if isinstance(key, unicode):
key = key.encode('utf-8')
self.db.put(key, self.pack(value), sync=self.sync)
def __delitem__(self, key):
if isinstance(key, unicode):
key = key.encode('utf-8')
self.db.delete(key, sync=self.sync)
def pack(self, s):
return '-||-'.join([str(s.dtype), str(s.shape), s.tostring()])
def unpack(self, s):
dtype, shape, array = s.split('-||-')
data = np.fromstring(array, dtype=dtype)
data.shape = eval(shape)
return data
def setdefault(self, key, default=None):
if isinstance(key, unicode):
key = key.encode('utf-8')
try:
return self.unpack(self.db.get(key))
except Exception:
if default.any():
self[key] = default
return default
return None
def write_batch(self, *args, **kwargs):
return self.db.write_batch(*args, **kwargs)
def close(self):
self.db.close()
def iterkeys(self):
for key in self.db:
yield key.decode('utf-8')
def itervalues(self):
for key in self.db:
yield self[key]
def iteritems(self):
for key in self.db:
yield key.decode('utf-8'), self[key]
def keys(self):
return [key.decode('utf-8') for key in self.iterkeys()]
def values(self):
return [value for value in self.itervalues()]
def items(self):
return [(key.decode('utf-8'), value) for (key, value) in self.iteritems()]