-
Notifications
You must be signed in to change notification settings - Fork 0
/
bunch.py
55 lines (41 loc) · 920 Bytes
/
bunch.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
#!/usr/bin/env python
class Bunch:
"""
## from http://code.activestate.com/recipes/52308/
# Now, you can create a Bunch whenever you want to group a few variables:
>>> point = Bunch(datum=2, squared=2*2, coord=1)
# and of course you can read/write the named
# attributes you just created, add others, del
# some of them, etc, etc:
>>> if point.squared > 3:
... point.isok = 1
...
>>> point.isok
1
>>> b=Bunch()
>>> b.foo = 'bar'
>>> b['bar'] = 'baz'
>>> b.foo
'bar'
>>> b['foo']
'bar'
>>> b.bar
'baz'
>>> b['bar']
'baz'
>>> 'foo' in b
True
>>> 'baz' in b
False
"""
def __init__(self, **kwds):
self.__dict__.update(kwds)
def __setitem__ (self, item, value):
self.__dict__[item] = value
def __getitem__ (self, item):
return self.__dict__[item]
def __iter__ (self):
return self.__dict__.__iter__()
if __name__ == "__main__":
import doctest
doctest.testmod()