Skip to content

Commit 533c868

Browse files
committed
Fixed a number of pep8 violations.
1 parent 7e28863 commit 533c868

25 files changed

+306
-197
lines changed

abstract_factory.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import random
66

7+
78
class PetShop:
89
"""A pet shop"""
910

@@ -22,41 +23,51 @@ def show_pet(self):
2223
print("It says", pet.speak())
2324
print("It eats", self.pet_factory.get_food())
2425

26+
2527
# Stuff that our factory makes
2628

2729
class Dog:
2830
def speak(self):
2931
return "woof"
32+
3033
def __str__(self):
3134
return "Dog"
3235

36+
3337
class Cat:
3438
def speak(self):
3539
return "meow"
40+
3641
def __str__(self):
3742
return "Cat"
3843

44+
3945
# Factory classes
4046

4147
class DogFactory:
4248
def get_pet(self):
4349
return Dog()
50+
4451
def get_food(self):
4552
return "dog food"
4653

54+
4755
class CatFactory:
4856
def get_pet(self):
4957
return Cat()
58+
5059
def get_food(self):
5160
return "cat food"
5261

62+
5363
# Create the proper family
5464
def get_factory():
5565
"""Let's be dynamic!"""
5666
return random.choice([DogFactory, CatFactory])()
5767

68+
5869
# Show pets with various factories
59-
if __name__== "__main__":
70+
if __name__ == "__main__":
6071
shop = PetShop()
6172
for i in range(3):
6273
shop.pet_factory = get_factory()

adapter.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,39 @@
22

33
import os
44

5+
56
class Dog(object):
67
def __init__(self):
78
self.name = "Dog"
89

910
def bark(self):
1011
return "woof!"
1112

13+
1214
class Cat(object):
1315
def __init__(self):
1416
self.name = "Cat"
1517

1618
def meow(self):
1719
return "meow!"
1820

21+
1922
class Human(object):
2023
def __init__(self):
2124
self.name = "Human"
2225

2326
def speak(self):
2427
return "'hello'"
2528

29+
2630
class Car(object):
2731
def __init__(self):
2832
self.name = "Car"
2933

3034
def make_noise(self, octane_level):
3135
return "vroom%s" % ("!" * octane_level)
3236

37+
3338
class Adapter(object):
3439
"""
3540
Adapts an object by replacing methods.
@@ -46,6 +51,7 @@ def __getattr__(self, attr):
4651
"""All non-adapted calls are passed to the object"""
4752
return getattr(self.obj, attr)
4853

54+
4955
def main():
5056
objects = []
5157
dog = Dog()
@@ -55,11 +61,12 @@ def main():
5561
human = Human()
5662
objects.append(Adapter(human, dict(make_noise=human.speak)))
5763
car = Car()
58-
car_noise = lambda : car.make_noise(3)
64+
car_noise = lambda: car.make_noise(3)
5965
objects.append(Adapter(car, dict(make_noise=car_noise)))
6066

6167
for obj in objects:
6268
print("A", obj.name, "goes", obj.make_noise())
6369

70+
6471
if __name__ == "__main__":
6572
main()

borg.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ def __init__(self):
77
def __str__(self):
88
return self.state
99

10+
1011
class YourBorg(Borg):
1112
pass
1213

@@ -33,4 +34,3 @@ class YourBorg(Borg):
3334
print('rm1:', rm1)
3435
print('rm2:', rm2)
3536
print('rm3:', rm3)
36-

bridge.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
# http://en.wikibooks.org/wiki/Computer_Science_Design_Patterns/Bridge_Pattern#Python
22

3+
34
# ConcreteImplementor 1/2
45
class DrawingAPI1:
56
def drawCircle(self, x, y, radius):
67
print('API1.circle at {}:{} radius {}'.format(x, y, radius))
7-
8+
9+
810
# ConcreteImplementor 2/2
911
class DrawingAPI2:
1012
def drawCircle(self, x, y, radius):
1113
print('API2.circle at {}:{} radius {}'.format(x, y, radius))
12-
14+
15+
1316
# Refined Abstraction
1417
class CircleShape:
1518
def __init__(self, x, y, radius, drawingAPI):
@@ -25,16 +28,18 @@ def draw(self):
2528
# high-level i.e. Abstraction specific
2629
def resizeByPercentage(self, pct):
2730
self.__radius *= pct
28-
31+
32+
2933
def main():
3034
shapes = (
3135
CircleShape(1, 2, 3, DrawingAPI1()),
3236
CircleShape(5, 7, 11, DrawingAPI2())
33-
)
37+
)
3438

3539
for shape in shapes:
3640
shape.resizeByPercentage(2.5)
3741
shape.draw()
3842

43+
3944
if __name__ == "__main__":
4045
main()

builder.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
https://gist.github.com/420905#file_builder_python.py
77
"""
88

9+
910
# Director
1011
class Director(object):
1112
def __init__(self):
@@ -19,6 +20,7 @@ def construct_building(self):
1920
def get_building(self):
2021
return self.builder.building
2122

23+
2224
# Abstract Builder
2325
class Builder(object):
2426
def __init__(self):
@@ -27,21 +29,24 @@ def __init__(self):
2729
def new_building(self):
2830
self.building = Building()
2931

32+
3033
# Concrete Builder
3134
class BuilderHouse(Builder):
3235
def build_floor(self):
33-
self.building.floor ='One'
36+
self.building.floor = 'One'
3437

3538
def build_size(self):
3639
self.building.size = 'Big'
3740

41+
3842
class BuilderFlat(Builder):
3943
def build_floor(self):
40-
self.building.floor ='More than One'
44+
self.building.floor = 'More than One'
4145

4246
def build_size(self):
4347
self.building.size = 'Small'
4448

49+
4550
# Product
4651
class Building(object):
4752
def __init__(self):
@@ -51,8 +56,9 @@ def __init__(self):
5156
def __repr__(self):
5257
return 'Floor: %s | Size: %s' % (self.floor, self.size)
5358

59+
5460
# Client
55-
if __name__== "__main__":
61+
if __name__ == "__main__":
5662
director = Director()
5763
director.builder = BuilderHouse()
5864
director.construct_building()

chain.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,35 @@
11
# http://www.testingperspective.com/wiki/doku.php/collaboration/chetan/designpatternsinpython/chain-of-responsibilitypattern
22

3+
34
class Handler:
45
def successor(self, successor):
56
self.successor = successor
67

8+
79
class ConcreteHandler1(Handler):
810
def handle(self, request):
911
if request > 0 and request <= 10:
1012
print("in handler1")
1113
else:
1214
self.successor.handle(request)
1315

16+
1417
class ConcreteHandler2(Handler):
1518
def handle(self, request):
1619
if request > 10 and request <= 20:
1720
print("in handler2")
1821
else:
1922
self.successor.handle(request)
2023

24+
2125
class ConcreteHandler3(Handler):
2226
def handle(self, request):
2327
if request > 20 and request <= 30:
2428
print("in handler3")
2529
else:
2630
print('end of chain, no handler for {}'.format(request))
2731

32+
2833
class Client:
2934
def __init__(self):
3035
h1 = ConcreteHandler1()
@@ -38,5 +43,6 @@ def __init__(self):
3843
for request in requests:
3944
h1.handle(request)
4045

41-
if __name__== "__main__":
46+
47+
if __name__ == "__main__":
4248
client = Client()

command.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22

3+
34
class MoveFileCommand(object):
45
def __init__(self, src, dest):
56
self.src = src
@@ -32,5 +33,5 @@ def undo(self):
3233

3334
# and can also be undone on will
3435
for cmd in undo_stack:
35-
undo_stack.pop().undo() # Now it's bar.txt
36-
undo_stack.pop().undo() # and back to foo.txt
36+
undo_stack.pop().undo() # Now it's bar.txt
37+
undo_stack.pop().undo() # and back to foo.txt

composite.py

+18-13
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,20 @@ def normalize(val):
1919
to a Python object """
2020

2121
if val.find('-') != -1:
22-
val = val.replace('-','_')
22+
val = val.replace('-', '_')
2323

2424
return val
2525

26+
2627
def denormalize(val):
2728
""" De-normalize a string """
28-
29+
2930
if val.find('_') != -1:
30-
val = val.replace('_','-')
31+
val = val.replace('_', '-')
3132

3233
return val
3334

35+
3436
class SpecialDict(dict):
3537
""" A dictionary type which allows direct attribute
3638
access to its keys """
@@ -64,24 +66,25 @@ def __setattr__(self, name, value):
6466
# New attribute
6567
self[name] = value
6668

69+
6770
class CompositeDict(SpecialDict):
6871
""" A class which works like a hierarchical dictionary.
6972
This class is based on the Composite design-pattern """
70-
73+
7174
ID = 0
72-
75+
7376
def __init__(self, name=''):
7477

7578
if name:
7679
self._name = name
7780
else:
78-
self._name = ''.join(('id#',str(self.__class__.ID)))
81+
self._name = ''.join(('id#', str(self.__class__.ID)))
7982
self.__class__.ID += 1
80-
83+
8184
self._children = []
8285
# Link back to father
8386
self._father = None
84-
self[self._name] = SpecialDict()
87+
self[self._name] = SpecialDict()
8588

8689
def __getattr__(self, name):
8790

@@ -101,7 +104,8 @@ def __getattr__(self, name):
101104
return child
102105
else:
103106
attr = getattr(self[self._name], name)
104-
if attr: return attr
107+
if attr:
108+
return attr
105109

106110
raise AttributeError('no attribute named %s' % name)
107111

@@ -306,17 +310,18 @@ def addChild2(self, child):
306310
self._children.append(child)
307311
self.__setChildDict(child)
308312

309-
if __name__=="__main__":
313+
314+
if __name__ == "__main__":
310315
window = CompositeDict('Window')
311316
frame = window.addChild('Frame')
312317
tfield = frame.addChild('Text Field')
313-
tfield.setAttribute('size','20')
318+
tfield.setAttribute('size', '20')
314319

315320
btn = frame.addChild('Button1')
316-
btn.setAttribute('label','Submit')
321+
btn.setAttribute('label', 'Submit')
317322

318323
btn = frame.addChild('Button2')
319-
btn.setAttribute('label','Browse')
324+
btn.setAttribute('label', 'Browse')
320325

321326
# print(window)
322327
# print(window.Frame)

0 commit comments

Comments
 (0)