Last active
December 10, 2015 05:58
-
-
Save AJRenold/4391590 to your computer and use it in GitHub Desktop.
Solution from about_proxy_object_project.py https://github.com/gregmalcolm/python_koans
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## call as: | |
## Proxy(YourObject()) | |
class Proxy: | |
def __init__(self, target_object): | |
print("initializing proxy for "+target_object.__class__.__name__) | |
self._log = [] | |
self._obj = target_object | |
def __getattr__(self,attr): | |
if '_obj' == attr: | |
raise AttributeError | |
self._log.append(attr) | |
return self._obj.__getattribute__(attr) | |
def __setattr__(self, name, value): | |
if hasattr(self, '_obj'): | |
self._log.append(name) | |
object.__setattr__(self._obj, name, value) | |
else: | |
object.__setattr__(self, name, value) | |
def messages(self): | |
return self._log | |
def was_called(self,attr): | |
return attr in self._log | |
def number_of_times_called(self,attr): | |
count_attr = 0 | |
for item in self._log: | |
if item == attr: count_attr += 1 | |
return count_attr |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment