Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
__pycache__
*.pyc
.idea
29 changes: 27 additions & 2 deletions creational/lazy_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,35 @@ def __get__(self, obj, type_):
return val


def lazy_property2(fn):
attr = '_lazy__' + fn.__name__

@property
def _lazy_property(self):
if not hasattr(self, attr):
setattr(self, attr, fn(self))
return getattr(self, attr)
return _lazy_property


class Person(object):

def __init__(self, name, occupation):
self.name = name
self.occupation = occupation
self.call_count2 = 0

@lazy_property
def relatives(self):
# Get all relatives, let's assume that it costs much time.
relatives = "Many relatives."
return relatives

@lazy_property2
def parents(self):
self.call_count2 += 1
return "Father and mother"


def main():
Jhon = Person('Jhon', 'Coder')
Expand All @@ -58,6 +75,10 @@ def main():
print(u"Jhon's relatives: {0}".format(Jhon.relatives))
print(u"After we've accessed `relatives`:")
print(Jhon.__dict__)
print(Jhon.parents)
print(Jhon.__dict__)
print(Jhon.parents)
print(Jhon.call_count2)


if __name__ == '__main__':
Expand All @@ -66,7 +87,11 @@ def main():
### OUTPUT ###
# Name: Jhon Occupation: Coder
# Before we access `relatives`:
# {'name': 'Jhon', 'occupation': 'Coder'}
# {'call_count2': 0, 'name': 'Jhon', 'occupation': 'Coder'}
# Jhon's relatives: Many relatives.
# After we've accessed `relatives`:
# {'relatives': 'Many relatives.', 'name': 'Jhon', 'occupation': 'Coder'}
# {'relatives': 'Many relatives.', 'call_count2': 0, 'name': 'Jhon', 'occupation': 'Coder'}
# Father and mother
# {'_lazy__parents': 'Father and mother', 'relatives': 'Many relatives.', 'call_count2': 1, 'name': 'Jhon', 'occupation': 'Coder'}
# Father and mother
# 1
19 changes: 14 additions & 5 deletions tests/test_lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,27 @@ def setUp(self):
self.John = Person('John', 'Coder')

def test_innate_properties(self):
self.assertDictEqual({'name': 'John', 'occupation': 'Coder'},
self.John.__dict__)
self.assertDictEqual(
{'name': 'John', 'occupation': 'Coder', 'call_count2': 0},
self.John.__dict__
)

def test_relatives_not_in_properties(self):
self.assertNotIn('relatives', self.John.__dict__)

def test_extended_properties(self):
print(u"John's relatives: {0}".format(self.John.relatives))
self.assertDictEqual({'name': 'John', 'occupation': 'Coder',
'relatives': 'Many relatives.'},
self.John.__dict__)
self.assertDictEqual(
{'name': 'John', 'occupation': 'Coder',
'relatives': 'Many relatives.', 'call_count2': 0},
self.John.__dict__
)

def test_relatives_after_access(self):
print(u"John's relatives: {0}".format(self.John.relatives))
self.assertIn('relatives', self.John.__dict__)

def test_parents(self):
for _ in range(2):
self.assertEqual(self.John.parents, "Father and mother")
self.assertEqual(self.John.call_count2, 1)