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
90 changes: 41 additions & 49 deletions patterns/behavioral/observer.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,52 +64,44 @@ def update(self, subject):

# Example usage...
def main():
data1 = Data('Data 1')
data2 = Data('Data 2')
view1 = DecimalViewer()
view2 = HexViewer()
data1.attach(view1)
data1.attach(view2)
data2.attach(view2)
data2.attach(view1)

print(u"Setting Data 1 = 10")
data1.data = 10
print(u"Setting Data 2 = 15")
data2.data = 15
print(u"Setting Data 1 = 3")
data1.data = 3
print(u"Setting Data 2 = 5")
data2.data = 5
print(u"Detach HexViewer from data1 and data2.")
data1.detach(view2)
data2.detach(view2)
print(u"Setting Data 1 = 10")
data1.data = 10
print(u"Setting Data 2 = 15")
data2.data = 15


if __name__ == '__main__':
main()


OUTPUT = """
Setting Data 1 = 10
DecimalViewer: Subject Data 1 has data 10
HexViewer: Subject Data 1 has data 0xa
Setting Data 2 = 15
HexViewer: Subject Data 2 has data 0xf
DecimalViewer: Subject Data 2 has data 15
Setting Data 1 = 3
DecimalViewer: Subject Data 1 has data 3
HexViewer: Subject Data 1 has data 0x3
Setting Data 2 = 5
HexViewer: Subject Data 2 has data 0x5
DecimalViewer: Subject Data 2 has data 5
Detach HexViewer from data1 and data2.
Setting Data 1 = 10
DecimalViewer: Subject Data 1 has data 10
Setting Data 2 = 15
DecimalViewer: Subject Data 2 has data 15
"""
"""
>>> data1 = Data('Data 1')
>>> data2 = Data('Data 2')
>>> view1 = DecimalViewer()
>>> view2 = HexViewer()
>>> data1.attach(view1)
>>> data1.attach(view2)
>>> data2.attach(view2)
>>> data2.attach(view1)

>>> data1.data = 10
DecimalViewer: Subject Data 1 has data 10
HexViewer: Subject Data 1 has data 0xa

>>> data2.data = 15
HexViewer: Subject Data 2 has data 0xf
DecimalViewer: Subject Data 2 has data 15

>>> data1.data = 3
DecimalViewer: Subject Data 1 has data 3
HexViewer: Subject Data 1 has data 0x3

>>> data2.data = 5
HexViewer: Subject Data 2 has data 0x5
DecimalViewer: Subject Data 2 has data 5

# Detach HexViewer from data1 and data2
>>> data1.detach(view2)
>>> data2.detach(view2)

>>> data1.data = 10
DecimalViewer: Subject Data 1 has data 10

>>> data2.data = 15
DecimalViewer: Subject Data 2 has data 15
"""


if __name__ == "__main__":
import doctest
doctest.testmod()
63 changes: 36 additions & 27 deletions patterns/creational/lazy_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,30 +70,39 @@ def parents(self):


def main():
Jhon = Person('Jhon', 'Coder')
print(u"Name: {0} Occupation: {1}".format(Jhon.name, Jhon.occupation))
print(u"Before we access `relatives`:")
print(Jhon.__dict__)
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__':
main()

### OUTPUT ###
# Name: Jhon Occupation: Coder
# Before we access `relatives`:
# {'call_count2': 0, 'name': 'Jhon', 'occupation': 'Coder'}
# Jhon's relatives: Many relatives.
# After we've accessed `relatives`:
# {'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'} # noqa flake8
# Father and mother
# 1
"""
>>> Jhon = Person('Jhon', 'Coder')

>>> Jhon.name
'Jhon'
>>> Jhon.occupation
'Coder'

# Before we access `relatives`
>>> sorted(Jhon.__dict__.items())
[('call_count2', 0), ('name', 'Jhon'), ('occupation', 'Coder')]

>>> Jhon.relatives
'Many relatives.'

# After we've accessed `relatives`
>>> sorted(Jhon.__dict__.items())
[('call_count2', 0), ..., ('relatives', 'Many relatives.')]

>>> Jhon.parents
'Father and mother'

>>> sorted(Jhon.__dict__.items())
[('_lazy__parents', 'Father and mother'), ('call_count2', 1), ..., ('relatives', 'Many relatives.')]

>>> Jhon.parents
'Father and mother'

>>> Jhon.call_count2
1
"""


if __name__ == "__main__":
import doctest
doctest.testmod(optionflags=doctest.ELLIPSIS)
3 changes: 0 additions & 3 deletions tests/test_outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

import pytest

from patterns.behavioral.observer import main as observer_main
from patterns.behavioral.observer import OUTPUT as observer_output
from patterns.behavioral.publish_subscribe import main as publish_subscribe_main
from patterns.behavioral.publish_subscribe import OUTPUT as publish_subscribe_output
from patterns.behavioral.specification import main as specification_main
Expand All @@ -21,7 +19,6 @@
@pytest.mark.skipif(sys.version_info < (3,4),
reason="requires python3.4 or higher")
@pytest.mark.parametrize("main,output", [
(observer_main, observer_output),
(publish_subscribe_main, publish_subscribe_output),
(specification_main, specification_output),
(state_main, state_output),
Expand Down