-
Notifications
You must be signed in to change notification settings - Fork 2
/
cnt_dict.py
52 lines (40 loc) · 913 Bytes
/
cnt_dict.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
#!/usr/bin/python2.7
## you can inherit built-in data types - say dict
class CntDict(dict):
def __init__(self):
self.cnt = 0
def get_cnt(self):
return self.cnt
def __setitem__(self, var, val):
if var not in self:
self.cnt += 1
super(CntDict, self).__setitem__(var, val)
def __delitem__(self, var):
self.cnt -= 1
super(CntDict, self).__delitem__(var)
def update(self, d):
for k, v in d.iteritems():
self[k] = v
def __len__(self):
return self.get_cnt()
d = CntDict()
d[0] = 0
d[1] = 1
d[1] = 2
d[1] = 3
d[0] = 1
d[2] = 2
d[2] = 2
print d.get_cnt()
assert d.get_cnt() == 3
del d[0]
assert d.get_cnt() == 2
del d[1]
assert d.get_cnt() == 1
del d[2]
assert d.get_cnt() == 0
d[100] = 100
d[101] = 101
assert d.get_cnt() == 2
d.update(dict([(x, x) for x in xrange(5)]))
assert d.get_cnt() == 2+5