forked from ACTCollaboration/enlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bunch.py
45 lines (45 loc) · 1.37 KB
/
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
"""My own version of bunch, since the standard one lacks tab completion
and has trouble printing sometimes."""
class Bunch:
def __init__(self, *args, **kwargs):
self._dict = {}
for args in args:
self._dict.update(args)
self._dict.update(kwargs)
def __getattr__(self, name):
if name in self.__dict__["_dict"].keys():
return self.__dict__["_dict"][name]
raise AttributeError
def __setattr__(self, name, value):
if "_dict" in self.__dict__:
self._dict[name] = value
else:
self.__dict__[name] = value
def __getitem__(self, name):
return self._dict[name]
def __setitem__(self, name, value):
self._dict[name] = value
def __delattr__(self, name):
if name in self._dict:
del self._dict[name]
else:
del self.__dict__[name]
def __contains__(self, name):
return name in self._dict
def __dir__(self):
return sorted(self.__dict__.keys() + self._dict.keys())
def __iter__(self):
for key in self._dict:
yield key
def __len__(self):
return len(self._dict)
def keys(self): return self._dict.keys()
def items(self): return self._dict.items()
def iteritems(self): return self._dict.iteritems()
def copy(self): return Bunch(self._dict.copy())
def update(self, val): return self._dict.update(val)
def __repr__(self):
keys = sorted(self._dict.keys())
return "Bunch(" + ", ".join([
"%s=%s" % (str(key),repr(self._dict[key])) for key in keys
])+")"