-
Notifications
You must be signed in to change notification settings - Fork 20
/
colibricore_patterndict.pxi
59 lines (46 loc) · 1.43 KB
/
colibricore_patterndict.pxi
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
def __bool__(self):
return self.data.size() > 0
def __len__(self):
"""Return the total number of patterns in the dictionary"""
return self.data.size()
cpdef has(self, Pattern pattern):
return self.data.has(pattern.cpattern)
def __contains__(self, Pattern pattern):
"""Test if the pattern is in the dictionary"""
return self.has(pattern)
def __iter__(self):
"""Iterate over all patterns in the dictionary"""
it = self.data.begin()
cdef cPattern cpattern
it_end = self.data.end()
while it != it_end:
cpattern = deref(it).first
pattern = Pattern()
pattern.bind(cpattern)
yield pattern
inc(it)
def __getitem__(self, Pattern pattern):
"""Retrieve the value for a pattern in the dictionary
:param pattern: A pattern
:type pattern: Pattern
"""
return self.data[pattern.cpattern]
def items(self):
"""Iterate over all patterns and their values in the dictionary"""
it = self.data.begin()
cdef cPattern cpattern
it_end = self.data.end()
while it != it_end:
cpattern = deref(it).first
value = deref(it).second
pattern = Pattern()
pattern.bind(cpattern)
yield (pattern,value)
inc(it)
def read(self, str filename):
if os.path.exists(filename):
self.data.read(encode(filename))
else:
raise IOError
def write(self, str filename):
self.data.write(encode(filename))