- Notifications
You must be signed in to change notification settings - Fork 31.8k
/
Copy pathdelegator.py
34 lines (28 loc) · 1.02 KB
/
delegator.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
classDelegator:
def__init__(self, delegate=None):
self.delegate=delegate
self.__cache=set()
# Cache is used to only remove added attributes
# when changing the delegate.
def__getattr__(self, name):
attr=getattr(self.delegate, name) # May raise AttributeError
setattr(self, name, attr)
self.__cache.add(name)
returnattr
defresetcache(self):
"Removes added attributes while leaving original attributes."
# Function is really about resetting delegator dict
# to original state. Cache is just a means
forkeyinself.__cache:
try:
delattr(self, key)
exceptAttributeError:
pass
self.__cache.clear()
defsetdelegate(self, delegate):
"Reset attributes and change delegate."
self.resetcache()
self.delegate=delegate
if__name__=='__main__':
fromunittestimportmain
main('idlelib.idle_test.test_delegator', verbosity=2)