Skip to content

Commit ebf5de6

Browse files
committed
Minor enhancements on behavioral patterns
1 parent ba1ef2a commit ebf5de6

File tree

15 files changed

+57
-31
lines changed

15 files changed

+57
-31
lines changed

behavioral/catalog.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3-
43
"""
54
A class that uses different static function depending of a parameter passed in
65
init. Note the use of a single dictionary instead of multiple conditions
@@ -22,7 +21,7 @@ def __init__(self, param):
2221
# to be executed but that will be also used to store possible param
2322
# value
2423
self._static_method_choices = {'param_value_1': self._static_method_1,
25-
'param_value_2': self._static_method_2}
24+
'param_value_2': self._static_method_2}
2625

2726
# simple test to validate param value
2827
if param in self._static_method_choices.keys():
@@ -72,7 +71,6 @@ def _instance_method_2(self):
7271
_instance_method_choices = {'param_value_1': _instance_method_1,
7372
'param_value_2': _instance_method_2}
7473

75-
7674
def main_method(self):
7775
"""
7876
will execute either _instance_method_1 or _instance_method_2
@@ -123,6 +121,7 @@ class CatalogStatic:
123121
catalog of multiple static methods that are executed depending on an init
124122
parameter
125123
"""
124+
126125
def __init__(self, param):
127126
# simple test to validate param value
128127
if param in self._static_method_choices:
@@ -139,7 +138,7 @@ def _static_method_2():
139138
print("executed method 2!")
140139

141140
_static_method_choices = {'param_value_1': _static_method_1,
142-
'param_value_2': _static_method_2}
141+
'param_value_2': _static_method_2}
143142

144143
def main_method(self):
145144
"""
@@ -148,6 +147,7 @@ def main_method(self):
148147
"""
149148
self._static_method_choices[self.param].__get__(None, self.__class__)()
150149

150+
151151
def main():
152152
"""
153153
>>> c = Catalog('param_value_1').main_method()

behavioral/chain.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3-
43
"""http://www.dabeaz.com/coroutines/"""
54

65
import time
76
import os
87
import sys
98
from contextlib import contextmanager
109

10+
1111
class Handler:
12+
1213
def __init__(self, successor=None):
1314
self._successor = successor
15+
1416
def handle(self, request):
1517
res = self._handle(request)
1618
if not res:
1719
self._successor.handle(request)
20+
1821
def _handle(self, request):
1922
raise NotImplementedError('Must provide implementation in subclass.')
2023

@@ -25,30 +28,37 @@ def _handle(self, request):
2528
if 0 < request <= 10:
2629
print('request {} handled in handler 1'.format(request))
2730
return True
28-
31+
32+
2933
class ConcreteHandler2(Handler):
30-
34+
3135
def _handle(self, request):
3236
if 10 < request <= 20:
3337
print('request {} handled in handler 2'.format(request))
3438
return True
35-
39+
40+
3641
class ConcreteHandler3(Handler):
37-
42+
3843
def _handle(self, request):
3944
if 20 < request <= 30:
4045
print('request {} handled in handler 3'.format(request))
4146
return True
47+
48+
4249
class DefaultHandler(Handler):
43-
50+
4451
def _handle(self, request):
4552
print('end of chain, no handler for {}'.format(request))
4653
return True
4754

4855

4956
class Client:
57+
5058
def __init__(self):
51-
self.handler = ConcreteHandler1(ConcreteHandler3(ConcreteHandler2(DefaultHandler())))
59+
self.handler = ConcreteHandler1(
60+
ConcreteHandler3(ConcreteHandler2(DefaultHandler())))
61+
5262
def delegate(self, requests):
5363
for request in requests:
5464
self.handler.handle(request)
@@ -61,6 +71,7 @@ def start(*args, **kwargs):
6171
return cr
6272
return start
6373

74+
6475
@coroutine
6576
def coroutine1(target):
6677
while True:
@@ -70,6 +81,7 @@ def coroutine1(target):
7081
else:
7182
target.send(request)
7283

84+
7385
@coroutine
7486
def coroutine2(target):
7587
while True:
@@ -79,6 +91,7 @@ def coroutine2(target):
7991
else:
8092
target.send(request)
8193

94+
8295
@coroutine
8396
def coroutine3(target):
8497
while True:
@@ -88,20 +101,24 @@ def coroutine3(target):
88101
else:
89102
target.send(request)
90103

104+
91105
@coroutine
92106
def default_coroutine():
93-
while True:
94-
request = yield
95-
print('end of chain, no coroutine for {}'.format(request))
107+
while True:
108+
request = yield
109+
print('end of chain, no coroutine for {}'.format(request))
110+
96111

97112
class ClientCoroutine:
113+
98114
def __init__(self):
99115
self.target = coroutine1(coroutine3(coroutine2(default_coroutine())))
100116

101117
def delegate(self, requests):
102118
for request in requests:
103119
self.target.send(request)
104120

121+
105122
def timeit(func):
106123

107124
def count(*args, **kwargs):
@@ -111,6 +128,7 @@ def count(*args, **kwargs):
111128
return res
112129
return count
113130

131+
114132
@contextmanager
115133
def suppress_stdout():
116134
try:
@@ -126,7 +144,7 @@ def suppress_stdout():
126144
requests = [2, 5, 14, 22, 18, 3, 35, 27, 20]
127145

128146
client1.delegate(requests)
129-
print('-'*30)
147+
print('-' * 30)
130148
client2.delegate(requests)
131149

132150
requests *= 10000

behavioral/chaining_method.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from __future__ import print_function
55

6+
67
class Person(object):
78

89
def __init__(self, name, action):
@@ -13,6 +14,7 @@ def do_action(self):
1314
print(self.name, self.action.name, end=' ')
1415
return self.action
1516

17+
1618
class Action(object):
1719

1820
def __init__(self, name):

behavioral/command.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
from os.path import lexists
66

7+
78
class MoveFileCommand(object):
89

910
def __init__(self, src, dest):

behavioral/iterator.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3-
4-
"""http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/
5-
6-
Implementation of the iterator pattern with a generator"""
3+
"""
4+
http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/
5+
Implementation of the iterator pattern with a generator
6+
"""
77

88
from __future__ import print_function
99

behavioral/mediator.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3-
43
"""http://dpip.testingperspective.com/?p=28"""
54

65
import random

behavioral/memento.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3-
43
"""http://code.activestate.com/recipes/413838-memento-closure/"""
54

65
from copy import copy, deepcopy
@@ -57,6 +56,7 @@ def transaction(*args, **kwargs):
5756

5857

5958
class NumObj(object):
59+
6060
def __init__(self, value):
6161
self.value = value
6262

behavioral/observer.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3-
43
"""http://code.activestate.com/recipes/131499-observer-pattern/"""
54

65

behavioral/publish_subscribe.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3-
43
"""
5-
Reference: http://www.slideshare.net/ishraqabd/publish-subscribe-model-overview-13368808
4+
Reference:
5+
http://www.slideshare.net/ishraqabd/publish-subscribe-model-overview-13368808
66
Author: https://github.com/HanWenfang
77
"""
88

behavioral/registry.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,14 @@ class ClassRegistree(BaseRegisteredClass):
3838

3939
def __init__(self, *args, **kwargs):
4040
pass
41-
4241

4342
print("After subclassing: ")
4443
for k in RegistryHolder.REGISTRY:
4544
print(k)
4645

4746
### OUTPUT ###
48-
# Before subclassing:
47+
# Before subclassing:
4948
# BaseRegisteredClass
50-
# After subclassing:
49+
# After subclassing:
5150
# BaseRegisteredClass
5251
# ClassRegistree

0 commit comments

Comments
 (0)