-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathddb.py
More file actions
92 lines (73 loc) · 2.1 KB
/
ddb.py
File metadata and controls
92 lines (73 loc) · 2.1 KB
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
#
# ultralight dictionary database
# Ben Southgate (bsouthga@gmail.com)
# 10/24/14
# license : MIT
#
import itertools
def match(tree, query):
'''
return boolean indicating if all
elements of query match tree
@param tree : current record being scanned
@param query : the query to be executed on the data
'''
# loop through keys in query
for k in query:
# store value for current key
v = query[k]
# check if the current query key exists in the data
if k not in tree:
return False
# if the value for this key is itself a dictionary,
# recurse deeper into the query
if type(v) == dict:
if not match(tree[k], v):
return False
else:
# if the value is a function, call the function
# on the corresponding value in the data
if hasattr(v, '__call__'):
result = v(tree[k])
# necessitate that the function returns a boolean
if type(result) == bool:
if not result:
return False
else:
e = 'matching function for {} must return boolean.'.format(k)
raise Exception(e)
# if the value simply doesn't equal the data,
# no match found
elif not v == tree[k]:
return False
return True
class DDB(object):
def __init__(self, data):
self.d = data
def __repr__(self):
return str(self.to_list())
def __iter__(self):
for item in self.d: yield item
def __len__(self):
return len(self.to_list())
def __eq__(self, other):
return self.to_list() == other
def to_list(self):
view = list(self.d)
self.d = (x for x in view)
return view
def split(self):
view, copy = itertools.tee(self.d)
self.d = view
return copy
def select(self, query=None):
if query == None:
return DDB(self.split())
return DDB(x for x in self.split() if match(x, query))
def map(self, transform):
return DDB(transform(x) for x in self.split())
def insert(self, item):
if type(item) == dict or not hasattr(item, '__iter__'):
item = [item]
self.d = itertools.chain(self.split(), item)
return self