Mercurial > repos > rliterman > csp2
annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/idlelib/delegator.py @ 68:5028fdace37b
planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author | jpayne |
---|---|
date | Tue, 18 Mar 2025 16:23:26 -0400 |
parents | |
children |
rev | line source |
---|---|
jpayne@68 | 1 class Delegator: |
jpayne@68 | 2 |
jpayne@68 | 3 def __init__(self, delegate=None): |
jpayne@68 | 4 self.delegate = delegate |
jpayne@68 | 5 self.__cache = set() |
jpayne@68 | 6 # Cache is used to only remove added attributes |
jpayne@68 | 7 # when changing the delegate. |
jpayne@68 | 8 |
jpayne@68 | 9 def __getattr__(self, name): |
jpayne@68 | 10 attr = getattr(self.delegate, name) # May raise AttributeError |
jpayne@68 | 11 setattr(self, name, attr) |
jpayne@68 | 12 self.__cache.add(name) |
jpayne@68 | 13 return attr |
jpayne@68 | 14 |
jpayne@68 | 15 def resetcache(self): |
jpayne@68 | 16 "Removes added attributes while leaving original attributes." |
jpayne@68 | 17 # Function is really about resetting delegator dict |
jpayne@68 | 18 # to original state. Cache is just a means |
jpayne@68 | 19 for key in self.__cache: |
jpayne@68 | 20 try: |
jpayne@68 | 21 delattr(self, key) |
jpayne@68 | 22 except AttributeError: |
jpayne@68 | 23 pass |
jpayne@68 | 24 self.__cache.clear() |
jpayne@68 | 25 |
jpayne@68 | 26 def setdelegate(self, delegate): |
jpayne@68 | 27 "Reset attributes and change delegate." |
jpayne@68 | 28 self.resetcache() |
jpayne@68 | 29 self.delegate = delegate |
jpayne@68 | 30 |
jpayne@68 | 31 if __name__ == '__main__': |
jpayne@68 | 32 from unittest import main |
jpayne@68 | 33 main('idlelib.idle_test.test_delegator', verbosity=2) |