comparison CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/idlelib/delegator.py @ 69:33d812a61356

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 17:55:14 -0400
parents
children
comparison
equal deleted inserted replaced
67:0e9998148a16 69:33d812a61356
1 class Delegator:
2
3 def __init__(self, delegate=None):
4 self.delegate = delegate
5 self.__cache = set()
6 # Cache is used to only remove added attributes
7 # when changing the delegate.
8
9 def __getattr__(self, name):
10 attr = getattr(self.delegate, name) # May raise AttributeError
11 setattr(self, name, attr)
12 self.__cache.add(name)
13 return attr
14
15 def resetcache(self):
16 "Removes added attributes while leaving original attributes."
17 # Function is really about resetting delegator dict
18 # to original state. Cache is just a means
19 for key in self.__cache:
20 try:
21 delattr(self, key)
22 except AttributeError:
23 pass
24 self.__cache.clear()
25
26 def setdelegate(self, delegate):
27 "Reset attributes and change delegate."
28 self.resetcache()
29 self.delegate = delegate
30
31 if __name__ == '__main__':
32 from unittest import main
33 main('idlelib.idle_test.test_delegator', verbosity=2)