Skip to content

Commit efe44d6

Browse files
committed
Fixed PEP-8 voilations.
* add python headers. * Remove extra whitespaces.
1 parent f0aacac commit efe44d6

23 files changed

+151
-90
lines changed

‎README.md‎

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
python-patterns
22
===============
33

4-
A collection of design patterns implemented (by other people) in python
4+
A collection of design patterns implemented (by other people) in python.
55

66
Current Patterns:
77

8-
* 3-tier
9-
* composite
8+
* 3-tier
9+
* composite
1010
* mvc
11-
* decorator
11+
* decorator
1212
* null
13-
* facade
13+
* facade
1414
* observer
15-
* abstract_factory
16-
* factory_method
15+
* abstract_factory
16+
* factory_method
1717
* pool
18-
* adapter
19-
* flyweight
18+
* adapter
19+
* flyweight
2020
* prototype
21-
* borg
21+
* borg
2222
* proxy
23-
* bridge
24-
* graph_search
23+
* bridge
24+
* graph_search
2525
* state
26-
* builder
27-
* iterator
26+
* builder
27+
* iterator
2828
* strategy
29-
* chain
30-
* mediator
29+
* chain
30+
* mediator
3131
* template
32-
* command
33-
* memento
32+
* command
33+
* memento
3434
* visitor
35-
* publish_subscribe
35+
* publish_subscribe

‎abstract_factory.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
14
# http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/
25

36
"""Implementation of the abstract factory pattern"""

‎adapter.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
# http://ginstrom.com/scribbles/2008/11/06/generic-adapter-class-in-python/
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""http://ginstrom.com/scribbles/2008/11/06/generic-adapter-class-in-python/"""
25

36
importos
47

‎borg.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
14
classBorg:
25
__shared_state={}
36

‎bridge.py‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
# http://en.wikibooks.org/wiki/Computer_Science_Design_Patterns/Bridge_Pattern#Python
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""http://en.wikibooks.org/wiki/Computer_Science_Design_Patterns/Bridge_Pattern#Python"""
25

36

47
# ConcreteImplementor 1/2
@@ -20,11 +23,11 @@ def __init__(self, x, y, radius, drawing_api):
2023
self._y=y
2124
self._radius=radius
2225
self._drawing_api=drawing_api
23-
26+
2427
# low-level i.e. Implementation specific
2528
defdraw(self):
2629
self._drawing_api.draw_circle(self._x, self._y, self._radius)
27-
30+
2831
# high-level i.e. Abstraction specific
2932
defscale(self, pct):
3033
self._radius*=pct
@@ -35,11 +38,11 @@ def main():
3538
CircleShape(1, 2, 3, DrawingAPI1()),
3639
CircleShape(5, 7, 11, DrawingAPI2())
3740
)
38-
41+
3942
forshapeinshapes:
4043
shape.scale(2.5)
4144
shape.draw()
42-
45+
4346

4447
if__name__=='__main__':
4548
main()

‎catalog.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
14
"""
25
A class that uses different static function depending of a parameter passed in init
36
Note the use of a single dictionnary instead of multiple conditions

‎chain.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
# http://www.testingperspective.com/wiki/doku.php/collaboration/chetan/designpatternsinpython/chain-of-responsibilitypattern
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""http://www.testingperspective.com/wiki/doku.php/collaboration/chetan/designpatternsinpython/chain-of-responsibilitypattern"""
25

36

47
classHandler:

‎command.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
14
importos
25

36

@@ -34,4 +37,4 @@ def main():
3437
cmd.undo()
3538

3639
if__name__=="__main__":
37-
main()
40+
main()

‎composite.py‎

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
14
"""
25
A class which defines a composite object which can store
36
hieararchical dictionaries with names.
47
58
This class is same as a hiearchical dictionary, but it
6-
provides methods to add/access/modify children by name,
9+
provides methods to add/access/modify children by name,
710
like a Composite.
811
912
Created Anand B Pillai <[email protected]>
@@ -17,7 +20,7 @@
1720
defnormalize(val):
1821
""" Normalize a string so that it can be used as an attribute
1922
to a Python object """
20-
23+
2124
ifval.find('-') !=-1:
2225
val=val.replace('-', '_')
2326

@@ -65,7 +68,7 @@ def __setattr__(self, name, value):
6568
else:
6669
# New attribute
6770
self[name] =value
68-
71+
6972

7073
classCompositeDict(SpecialDict):
7174
""" A class which works like a hierarchical dictionary.
@@ -106,15 +109,15 @@ def __getattr__(self, name):
106109
attr=getattr(self[self._name], name)
107110
ifattr:
108111
returnattr
109-
112+
110113
raiseAttributeError('no attribute named %s'%name)
111114

112115
defisRoot(self):
113116
""" Return whether I am a root component or not """
114117

115118
# If I don't have a parent, I am root
116119
returnnotself._father
117-
120+
118121
defisLeaf(self):
119122
""" Return whether I am a leaf component or not """
120123

@@ -128,15 +131,15 @@ def getName(self):
128131

129132
defgetIndex(self, child):
130133
""" Return the index of the child ConfigInfo object 'child' """
131-
134+
132135
ifchildinself._children:
133136
returnself._children.index(child)
134137
else:
135138
return-1
136139

137140
defgetDict(self):
138141
""" Return the contained dictionary """
139-
142+
140143
returnself[self._name]
141144

142145
defgetProperty(self, child, key):
@@ -156,25 +159,25 @@ def setProperty(self, child, key, value):
156159
childDict=self.getInfoDict(child)
157160
ifchildDict:
158161
childDict[key] =value
159-
162+
160163
defgetChildren(self):
161164
""" Return the list of immediate children of this object """
162-
165+
163166
returnself._children
164167

165168
defgetAllChildren(self):
166169
""" Return the list of all children of this object """
167-
170+
168171
l= []
169172
forchildinself._children:
170173
l.append(child)
171174
l.extend(child.getAllChildren())
172-
175+
173176
returnl
174177

175178
defgetChild(self, name):
176179
""" Return the immediate child object with the given name """
177-
180+
178181
forchildinself._children:
179182
ifchild.getName() ==name:
180183
returnchild
@@ -185,7 +188,7 @@ def findChild(self, name):
185188
# Note - this returns the first child of the given name
186189
# any other children with similar names down the tree
187190
# is not considered.
188-
191+
189192
forchildinself.getAllChildren():
190193
ifchild.getName() ==name:
191194
returnchild
@@ -195,33 +198,33 @@ def findChildren(self, name):
195198

196199
# Note: this returns a list of all the children of a given
197200
# name, irrespective of the depth of look-up.
198-
201+
199202
children= []
200-
203+
201204
forchildinself.getAllChildren():
202205
ifchild.getName() ==name:
203206
children.append(child)
204207

205208
returnchildren
206-
209+
207210
defgetPropertyDict(self):
208211
""" Return the property dictionary """
209-
212+
210213
d=self.getChild('__properties')
211214
ifd:
212215
returnd.getDict()
213216
else:
214217
return{}
215-
218+
216219
defgetParent(self):
217220
""" Return the person who created me """
218221

219222
returnself._father
220-
223+
221224
def__setChildDict(self, child):
222225
""" Private method to set the dictionary of the child
223226
object 'child' in the internal dictionary """
224-
227+
225228
d=self[self._name]
226229
d[child.getName()] =child.getDict()
227230

@@ -236,25 +239,25 @@ def setParent(self, father):
236239
# child is orphaned - see addChild and addChild2
237240
# methods !
238241
self._father=father
239-
242+
240243
defsetName(self, name):
241-
""" Set the name of this ConfigInfo object to 'name' """
244+
""" Set the name of this ConfigInfo object to 'name' """
242245

243246
self._name=name
244247

245248
defsetDict(self, d):
246249
""" Set the contained dictionary """
247-
250+
248251
self[self._name] =d.copy()
249-
252+
250253
defsetAttribute(self, name, value):
251254
""" Set a name value pair in the contained dictionary """
252-
255+
253256
self[self._name][name] =value
254257

255258
defgetAttribute(self, name):
256259
""" Return value of an attribute from the contained dictionary """
257-
260+
258261
returnself[self._name][name]
259262

260263
defaddChild(self, name, force=False):
@@ -264,10 +267,10 @@ def addChild(self, name, force=False):
264267
265268
This function returns the child object, whether
266269
new or existing """
267-
270+
268271
iftype(name) !=str:
269272
raiseValueError('Argument should be a string!')
270-
273+
271274
child=self.getChild(name)
272275
ifchild:
273276
# print 'Child %s present!' % name
@@ -278,22 +281,22 @@ def addChild(self, name, force=False):
278281
child=self.__class__(name)
279282
self._children[index] =child
280283
child.setParent(self)
281-
284+
282285
self.__setChildDict(child)
283286
returnchild
284287
else:
285288
child=self.__class__(name)
286289
child.setParent(self)
287-
290+
288291
self._children.append(child)
289292
self.__setChildDict(child)
290293

291294
returnchild
292-
295+
293296
defaddChild2(self, child):
294297
""" Add the child object 'child'. If it is already present,
295298
it is overwritten by default """
296-
299+
297300
currChild=self.getChild(child.getName())
298301
ifcurrChild:
299302
index=self.getIndex(currChild)
@@ -303,10 +306,10 @@ def addChild2(self, child):
303306
# Unset the existing child's parent
304307
currChild.setParent(None)
305308
delcurrChild
306-
309+
307310
self.__setChildDict(child)
308311
else:
309-
child.setParent(self)
312+
child.setParent(self)
310313
self._children.append(child)
311314
self.__setChildDict(child)
312315

@@ -316,7 +319,7 @@ def addChild2(self, child):
316319
frame=window.addChild('Frame')
317320
tfield=frame.addChild('Text Field')
318321
tfield.setAttribute('size', '20')
319-
322+
320323
btn=frame.addChild('Button1')
321324
btn.setAttribute('label', 'Submit')
322325

‎decorator.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
# http://stackoverflow.com/questions/3118929/implementing-the-decorator-pattern-in-python
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""http://stackoverflow.com/questions/3118929/implementing-the-decorator-pattern-in-python"""
25

36

47
classfoo_decorator(object):

0 commit comments

Comments
(0)