-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathredisprob.py
84 lines (73 loc) · 1.81 KB
/
redisprob.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
from nltk.probability import ConditionalFreqDist
from rediscollections import RedisHashMap, encode_key
class RedisHashFreqDist(RedisHashMap):
'''
>>> from redis import Redis
>>> r = Redis()
>>> rhfd = RedisHashFreqDist(r, 'test')
>>> rhfd.items()
[]
>>> rhfd.values()
[]
>>> len(rhfd)
0
>>> rhfd['foo']
0
>>> rhfd['foo'] += 1
>>> rhfd['foo']
1
>>> rhfd.items()
[(b'foo', 1)]
>>> rhfd.values()
[1]
>>> len(rhfd)
1
>>> rhfd.clear()
'''
def N(self):
return int(sum(self.values()))
def __missing__(self, key):
return 0
def __getitem__(self, key):
return int(RedisHashMap.__getitem__(self, key) or 0)
def values(self):
return [int(v) for v in RedisHashMap.values(self)]
def items(self):
return [(k, int(v)) for (k, v) in RedisHashMap.items(self)]
class RedisConditionalHashFreqDist(ConditionalFreqDist):
'''
>>> from redis import Redis
>>> r = Redis()
>>> rchfd = RedisConditionalHashFreqDist(r, 'condhash')
>>> rchfd.N()
0
>>> rchfd.conditions()
[]
>>> rchfd['cond1']['foo'] += 1
>>> rchfd.N()
1
>>> rchfd['cond1']['foo']
1
>>> rchfd.conditions()
['cond1']
>>> rchfd.clear()
'''
def __init__(self, r, name, cond_samples=None):
self._r = r
self._name = name
ConditionalFreqDist.__init__(self, cond_samples)
for key in self._r.keys(encode_key('%s:*' % name)):
condition = key.split(b':')[1].decode()
self[condition] # calls self.__getitem__(condition)
def __getitem__(self, condition):
if condition not in self:
key = '%s:%s' % (self._name, condition)
val = RedisHashFreqDist(self._r, key)
super(RedisConditionalHashFreqDist, self).__setitem__(condition, val)
return super(RedisConditionalHashFreqDist, self).__getitem__(condition)
def clear(self):
for fdist in self.values():
fdist.clear()
if __name__ == '__main__':
import doctest
doctest.testmod()