| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,6 +4,7 @@ | |||
| 4 | 4 | ||
| 5 | 5 | import random | |
| 6 | 6 | ||
| 7 | + | ||
| 7 | 8 | class PetShop: | |
| 8 | 9 | """A pet shop""" | |
| 9 | 10 | ||
@@ -22,41 +23,51 @@ def show_pet(self): | |||
| 22 | 23 | print("It says", pet.speak()) | |
| 23 | 24 | print("It eats", self.pet_factory.get_food()) | |
| 24 | 25 | ||
| 26 | + | ||
| 25 | 27 | # Stuff that our factory makes | |
| 26 | 28 | ||
| 27 | 29 | class Dog: | |
| 28 | 30 | def speak(self): | |
| 29 | 31 | return "woof" | |
| 32 | + | ||
| 30 | 33 | def __str__(self): | |
| 31 | 34 | return "Dog" | |
| 32 | 35 | ||
| 36 | + | ||
| 33 | 37 | class Cat: | |
| 34 | 38 | def speak(self): | |
| 35 | 39 | return "meow" | |
| 40 | + | ||
| 36 | 41 | def __str__(self): | |
| 37 | 42 | return "Cat" | |
| 38 | 43 | ||
| 44 | + | ||
| 39 | 45 | # Factory classes | |
| 40 | 46 | ||
| 41 | 47 | class DogFactory: | |
| 42 | 48 | def get_pet(self): | |
| 43 | 49 | return Dog() | |
| 50 | + | ||
| 44 | 51 | def get_food(self): | |
| 45 | 52 | return "dog food" | |
| 46 | 53 | ||
| 54 | + | ||
| 47 | 55 | class CatFactory: | |
| 48 | 56 | def get_pet(self): | |
| 49 | 57 | return Cat() | |
| 58 | + | ||
| 50 | 59 | def get_food(self): | |
| 51 | 60 | return "cat food" | |
| 52 | 61 | ||
| 62 | + | ||
| 53 | 63 | # Create the proper family | |
| 54 | 64 | def get_factory(): | |
| 55 | 65 | """Let's be dynamic!""" | |
| 56 | 66 | return random.choice([DogFactory, CatFactory])() | |
| 57 | 67 | ||
| 68 | + | ||
| 58 | 69 | # Show pets with various factories | |
| 59 | - if __name__== "__main__": | ||
| 70 | + if __name__ == "__main__": | ||
| 60 | 71 | shop = PetShop() | |
| 61 | 72 | for i in range(3): | |
| 62 | 73 | shop.pet_factory = get_factory() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,34 +2,39 @@ | |||
| 2 | 2 | ||
| 3 | 3 | import os | |
| 4 | 4 | ||
| 5 | + | ||
| 5 | 6 | class Dog(object): | |
| 6 | 7 | def __init__(self): | |
| 7 | 8 | self.name = "Dog" | |
| 8 | 9 | ||
| 9 | 10 | def bark(self): | |
| 10 | 11 | return "woof!" | |
| 11 | 12 | ||
| 13 | + | ||
| 12 | 14 | class Cat(object): | |
| 13 | 15 | def __init__(self): | |
| 14 | 16 | self.name = "Cat" | |
| 15 | 17 | ||
| 16 | 18 | def meow(self): | |
| 17 | 19 | return "meow!" | |
| 18 | 20 | ||
| 21 | + | ||
| 19 | 22 | class Human(object): | |
| 20 | 23 | def __init__(self): | |
| 21 | 24 | self.name = "Human" | |
| 22 | 25 | ||
| 23 | 26 | def speak(self): | |
| 24 | 27 | return "'hello'" | |
| 25 | 28 | ||
| 29 | + | ||
| 26 | 30 | class Car(object): | |
| 27 | 31 | def __init__(self): | |
| 28 | 32 | self.name = "Car" | |
| 29 | 33 | ||
| 30 | 34 | def make_noise(self, octane_level): | |
| 31 | 35 | return "vroom%s" % ("!" * octane_level) | |
| 32 | 36 | ||
| 37 | + | ||
| 33 | 38 | class Adapter(object): | |
| 34 | 39 | """ | |
| 35 | 40 | Adapts an object by replacing methods. | |
@@ -46,6 +51,7 @@ def __getattr__(self, attr): | |||
| 46 | 51 | """All non-adapted calls are passed to the object""" | |
| 47 | 52 | return getattr(self.obj, attr) | |
| 48 | 53 | ||
| 54 | + | ||
| 49 | 55 | def main(): | |
| 50 | 56 | objects = [] | |
| 51 | 57 | dog = Dog() | |
@@ -55,11 +61,12 @@ def main(): | |||
| 55 | 61 | human = Human() | |
| 56 | 62 | objects.append(Adapter(human, dict(make_noise=human.speak))) | |
| 57 | 63 | car = Car() | |
| 58 | - car_noise = lambda : car.make_noise(3) | ||
| 64 | + car_noise = lambda: car.make_noise(3) | ||
| 59 | 65 | objects.append(Adapter(car, dict(make_noise=car_noise))) | |
| 60 | 66 | ||
| 61 | 67 | for obj in objects: | |
| 62 | 68 | print("A", obj.name, "goes", obj.make_noise()) | |
| 63 | 69 | ||
| 70 | + | ||
| 64 | 71 | if __name__ == "__main__": | |
| 65 | 72 | main() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,6 +7,7 @@ def __init__(self): | |||
| 7 | 7 | def __str__(self): | |
| 8 | 8 | return self.state | |
| 9 | 9 | ||
| 10 | + | ||
| 10 | 11 | class YourBorg(Borg): | |
| 11 | 12 | pass | |
| 12 | 13 | ||
@@ -33,4 +34,3 @@ class YourBorg(Borg): | |||
| 33 | 34 | print('rm1:', rm1) | |
| 34 | 35 | print('rm2:', rm2) | |
| 35 | 36 | print('rm3:', rm3) | |
| 36 | - | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,15 +1,18 @@ | |||
| 1 | 1 | # http://en.wikibooks.org/wiki/Computer_Science_Design_Patterns/Bridge_Pattern#Python | |
| 2 | 2 | ||
| 3 | + | ||
| 3 | 4 | # ConcreteImplementor 1/2 | |
| 4 | 5 | class DrawingAPI1: | |
| 5 | 6 | def drawCircle(self, x, y, radius): | |
| 6 | 7 | print('API1.circle at {}:{} radius {}'.format(x, y, radius)) | |
| 7 | - | ||
| 8 | + | ||
| 9 | + | ||
| 8 | 10 | # ConcreteImplementor 2/2 | |
| 9 | 11 | class DrawingAPI2: | |
| 10 | 12 | def drawCircle(self, x, y, radius): | |
| 11 | 13 | print('API2.circle at {}:{} radius {}'.format(x, y, radius)) | |
| 12 | - | ||
| 14 | + | ||
| 15 | + | ||
| 13 | 16 | # Refined Abstraction | |
| 14 | 17 | class CircleShape: | |
| 15 | 18 | def __init__(self, x, y, radius, drawingAPI): | |
@@ -25,16 +28,18 @@ def draw(self): | |||
| 25 | 28 | # high-level i.e. Abstraction specific | |
| 26 | 29 | def resizeByPercentage(self, pct): | |
| 27 | 30 | self.__radius *= pct | |
| 28 | - | ||
| 31 | + | ||
| 32 | + | ||
| 29 | 33 | def main(): | |
| 30 | 34 | shapes = ( | |
| 31 | 35 | CircleShape(1, 2, 3, DrawingAPI1()), | |
| 32 | 36 | CircleShape(5, 7, 11, DrawingAPI2()) | |
| 33 | - ) | ||
| 37 | + ) | ||
| 34 | 38 | ||
| 35 | 39 | for shape in shapes: | |
| 36 | 40 | shape.resizeByPercentage(2.5) | |
| 37 | 41 | shape.draw() | |
| 38 | 42 | ||
| 43 | + | ||
| 39 | 44 | if __name__ == "__main__": | |
| 40 | 45 | main() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,6 +6,7 @@ | |||
| 6 | 6 | https://gist.github.com/420905#file_builder_python.py | |
| 7 | 7 | """ | |
| 8 | 8 | ||
| 9 | + | ||
| 9 | 10 | # Director | |
| 10 | 11 | class Director(object): | |
| 11 | 12 | def __init__(self): | |
@@ -19,6 +20,7 @@ def construct_building(self): | |||
| 19 | 20 | def get_building(self): | |
| 20 | 21 | return self.builder.building | |
| 21 | 22 | ||
| 23 | + | ||
| 22 | 24 | # Abstract Builder | |
| 23 | 25 | class Builder(object): | |
| 24 | 26 | def __init__(self): | |
@@ -27,21 +29,24 @@ def __init__(self): | |||
| 27 | 29 | def new_building(self): | |
| 28 | 30 | self.building = Building() | |
| 29 | 31 | ||
| 32 | + | ||
| 30 | 33 | # Concrete Builder | |
| 31 | 34 | class BuilderHouse(Builder): | |
| 32 | 35 | def build_floor(self): | |
| 33 | - self.building.floor ='One' | ||
| 36 | + self.building.floor = 'One' | ||
| 34 | 37 | ||
| 35 | 38 | def build_size(self): | |
| 36 | 39 | self.building.size = 'Big' | |
| 37 | 40 | ||
| 41 | + | ||
| 38 | 42 | class BuilderFlat(Builder): | |
| 39 | 43 | def build_floor(self): | |
| 40 | - self.building.floor ='More than One' | ||
| 44 | + self.building.floor = 'More than One' | ||
| 41 | 45 | ||
| 42 | 46 | def build_size(self): | |
| 43 | 47 | self.building.size = 'Small' | |
| 44 | 48 | ||
| 49 | + | ||
| 45 | 50 | # Product | |
| 46 | 51 | class Building(object): | |
| 47 | 52 | def __init__(self): | |
@@ -51,8 +56,9 @@ def __init__(self): | |||
| 51 | 56 | def __repr__(self): | |
| 52 | 57 | return 'Floor: %s | Size: %s' % (self.floor, self.size) | |
| 53 | 58 | ||
| 59 | + | ||
| 54 | 60 | # Client | |
| 55 | - if __name__== "__main__": | ||
| 61 | + if __name__ == "__main__": | ||
| 56 | 62 | director = Director() | |
| 57 | 63 | director.builder = BuilderHouse() | |
| 58 | 64 | director.construct_building() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,30 +1,35 @@ | |||
| 1 | 1 | # http://www.testingperspective.com/wiki/doku.php/collaboration/chetan/designpatternsinpython/chain-of-responsibilitypattern | |
| 2 | 2 | ||
| 3 | + | ||
| 3 | 4 | class Handler: | |
| 4 | 5 | def successor(self, successor): | |
| 5 | 6 | self.successor = successor | |
| 6 | 7 | ||
| 8 | + | ||
| 7 | 9 | class ConcreteHandler1(Handler): | |
| 8 | 10 | def handle(self, request): | |
| 9 | 11 | if request > 0 and request <= 10: | |
| 10 | 12 | print("in handler1") | |
| 11 | 13 | else: | |
| 12 | 14 | self.successor.handle(request) | |
| 13 | 15 | ||
| 16 | + | ||
| 14 | 17 | class ConcreteHandler2(Handler): | |
| 15 | 18 | def handle(self, request): | |
| 16 | 19 | if request > 10 and request <= 20: | |
| 17 | 20 | print("in handler2") | |
| 18 | 21 | else: | |
| 19 | 22 | self.successor.handle(request) | |
| 20 | 23 | ||
| 24 | + | ||
| 21 | 25 | class ConcreteHandler3(Handler): | |
| 22 | 26 | def handle(self, request): | |
| 23 | 27 | if request > 20 and request <= 30: | |
| 24 | 28 | print("in handler3") | |
| 25 | 29 | else: | |
| 26 | 30 | print('end of chain, no handler for {}'.format(request)) | |
| 27 | 31 | ||
| 32 | + | ||
| 28 | 33 | class Client: | |
| 29 | 34 | def __init__(self): | |
| 30 | 35 | h1 = ConcreteHandler1() | |
@@ -38,5 +43,6 @@ def __init__(self): | |||
| 38 | 43 | for request in requests: | |
| 39 | 44 | h1.handle(request) | |
| 40 | 45 | ||
| 41 | - if __name__== "__main__": | ||
| 46 | + | ||
| 47 | + if __name__ == "__main__": | ||
| 42 | 48 | client = Client() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,6 @@ | |||
| 1 | 1 | import os | |
| 2 | 2 | ||
| 3 | + | ||
| 3 | 4 | class MoveFileCommand(object): | |
| 4 | 5 | def __init__(self, src, dest): | |
| 5 | 6 | self.src = src | |
@@ -32,5 +33,5 @@ def undo(self): | |||
| 32 | 33 | ||
| 33 | 34 | # and can also be undone on will | |
| 34 | 35 | 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 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -19,18 +19,20 @@ def normalize(val): | |||
| 19 | 19 | to a Python object """ | |
| 20 | 20 | ||
| 21 | 21 | if val.find('-') != -1: | |
| 22 | - val = val.replace('-','_') | ||
| 22 | + val = val.replace('-', '_') | ||
| 23 | 23 | ||
| 24 | 24 | return val | |
| 25 | 25 | ||
| 26 | + | ||
| 26 | 27 | def denormalize(val): | |
| 27 | 28 | """ De-normalize a string """ | |
| 28 | - | ||
| 29 | + | ||
| 29 | 30 | if val.find('_') != -1: | |
| 30 | - val = val.replace('_','-') | ||
| 31 | + val = val.replace('_', '-') | ||
| 31 | 32 | ||
| 32 | 33 | return val | |
| 33 | 34 | ||
| 35 | + | ||
| 34 | 36 | class SpecialDict(dict): | |
| 35 | 37 | """ A dictionary type which allows direct attribute | |
| 36 | 38 | access to its keys """ | |
@@ -64,24 +66,25 @@ def __setattr__(self, name, value): | |||
| 64 | 66 | # New attribute | |
| 65 | 67 | self[name] = value | |
| 66 | 68 | ||
| 69 | + | ||
| 67 | 70 | class CompositeDict(SpecialDict): | |
| 68 | 71 | """ A class which works like a hierarchical dictionary. | |
| 69 | 72 | This class is based on the Composite design-pattern """ | |
| 70 | - | ||
| 73 | + | ||
| 71 | 74 | ID = 0 | |
| 72 | - | ||
| 75 | + | ||
| 73 | 76 | def __init__(self, name=''): | |
| 74 | 77 | ||
| 75 | 78 | if name: | |
| 76 | 79 | self._name = name | |
| 77 | 80 | else: | |
| 78 | - self._name = ''.join(('id#',str(self.__class__.ID))) | ||
| 81 | + self._name = ''.join(('id#', str(self.__class__.ID))) | ||
| 79 | 82 | self.__class__.ID += 1 | |
| 80 | - | ||
| 83 | + | ||
| 81 | 84 | self._children = [] | |
| 82 | 85 | # Link back to father | |
| 83 | 86 | self._father = None | |
| 84 | - self[self._name] = SpecialDict() | ||
| 87 | + self[self._name] = SpecialDict() | ||
| 85 | 88 | ||
| 86 | 89 | def __getattr__(self, name): | |
| 87 | 90 | ||
@@ -101,7 +104,8 @@ def __getattr__(self, name): | |||
| 101 | 104 | return child | |
| 102 | 105 | else: | |
| 103 | 106 | attr = getattr(self[self._name], name) | |
| 104 | - if attr: return attr | ||
| 107 | + if attr: | ||
| 108 | + return attr | ||
| 105 | 109 | ||
| 106 | 110 | raise AttributeError('no attribute named %s' % name) | |
| 107 | 111 | ||
@@ -306,17 +310,18 @@ def addChild2(self, child): | |||
| 306 | 310 | self._children.append(child) | |
| 307 | 311 | self.__setChildDict(child) | |
| 308 | 312 | ||
| 309 | - if __name__=="__main__": | ||
| 313 | + | ||
| 314 | + if __name__ == "__main__": | ||
| 310 | 315 | window = CompositeDict('Window') | |
| 311 | 316 | frame = window.addChild('Frame') | |
| 312 | 317 | tfield = frame.addChild('Text Field') | |
| 313 | - tfield.setAttribute('size','20') | ||
| 318 | + tfield.setAttribute('size', '20') | ||
| 314 | 319 | ||
| 315 | 320 | btn = frame.addChild('Button1') | |
| 316 | - btn.setAttribute('label','Submit') | ||
| 321 | + btn.setAttribute('label', 'Submit') | ||
| 317 | 322 | ||
| 318 | 323 | btn = frame.addChild('Button2') | |
| 319 | - btn.setAttribute('label','Browse') | ||
| 324 | + btn.setAttribute('label', 'Browse') | ||
| 320 | 325 | ||
| 321 | 326 | # print(window) | |
| 322 | 327 | # print(window.Frame) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments