Skip to content

Commit 32a0264

Browse files
author
amosio
committed
proxy project
1 parent 92ad105 commit 32a0264

1 file changed

Lines changed: 27 additions & 15 deletions

File tree

python3/koans/about_proxy_object_project.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,39 @@
1818

1919
from runner.koan import *
2020

21+
2122
class Proxy:
2223
def __init__(self, target_object):
2324
# WRITE CODE HERE
24-
object.__setattr__(self, '_messages', list())
25-
#initialize '_obj' attribute last. Trust me on this!
26-
#self._obj = target_object
25+
object.__setattr__(self, '_messages', dict())
2726
object.__setattr__(self, '_obj', target_object)
2827

2928
def messages(self):
30-
return self._messages
29+
return list(self._messages.keys())
30+
31+
def was_called(self, method_name):
32+
return method_name in self._messages.keys()
33+
34+
def number_of_times_called(self, method_name):
35+
self.number = 0
36+
if self.was_called(method_name):
37+
self.number = self._messages[method_name]
38+
return self.number
3139

32-
# WRITE CODE HERE
33-
def __getattr__(self, attr_name):
34-
print("get: " + attr_name)
35-
print( self._messages)
36-
self._messages.append(attr_name)
37-
return object.__getattribute__(self._obj, attr_name)
40+
def _update_messages(self, attr_name):
41+
if attr_name not in self._messages.keys():
42+
self._messages[attr_name] = 1
43+
else:
44+
self._messages[attr_name] += 1
45+
46+
def __getattr__(self, attr_name):
47+
self._update_messages(attr_name)
48+
return object.__getattribute__(self._obj, attr_name)
3849

3950
def __setattr__(self, attr_name, value):
40-
print("set: " + attr_name)
41-
print(self._messages)
42-
self._messages.append(attr_name)
51+
self._update_messages(attr_name)
4352
object.__setattr__(self._obj, attr_name, value)
44-
53+
4554

4655
# The proxy object should pass the following Koan:
4756
#
@@ -75,7 +84,6 @@ def test_proxy_handles_invalid_messages(self):
7584
with self.assertRaises(AttributeError):
7685
tv.no_such_method()
7786

78-
7987
def test_proxy_reports_methods_have_been_called(self):
8088
tv = Proxy(Television())
8189

@@ -113,6 +121,8 @@ def test_proxy_can_record_more_than_just_tv_objects(self):
113121
# changes should be necessary to anything below this comment.
114122

115123
# Example class using in the proxy testing above.
124+
125+
116126
class Television:
117127
def __init__(self):
118128
self._channel = None
@@ -136,6 +146,8 @@ def is_on(self):
136146
return self._power == 'on'
137147

138148
# Tests for the Television class. All of theses tests should pass.
149+
150+
139151
class TelevisionTest(Koan):
140152
def test_it_turns_on(self):
141153
tv = Television()

0 commit comments

Comments
 (0)