This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import time | |
class SalesManager: | |
def work(self): | |
print("Sales Manager working...") | |
def talk(self): | |
print("Sales Manager ready to talk") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
"""http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/ | |
Implementation of the iterator pattern with a generator""" | |
def count_to(count): | |
"""Counts by word numbers, up to a maximum of five""" | |
numbers = ["one", "two", "three", "four", "five"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/""" | |
class State(object): | |
"""Base state. This is to share functionality""" | |
def scan(self): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import os | |
class MoveFileCommand(object): | |
def __init__(self, src, dest): | |
self.src = src | |
self.dest = dest | |
def execute(self): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
"""http://code.activestate.com/recipes/413838-memento-closure/""" | |
import copy | |
def Memento(obj, deep=False): | |
state = (copy.copy, copy.deepcopy)[bool(deep)](obj.__dict__) | |
def Restore(): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
"""http://en.wikibooks.org/wiki/Computer_Science_Design_Patterns/Bridge_Pattern#Python""" | |
# ConcreteImplementor 1/2 | |
class DrawingAPI1(object): | |
def draw_circle(self, x, y, radius): | |
print('API1.circle at {}:{} radius {}'.format(x, y, radius)) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
A class which defines a composit object which can store | |
hierarchical dictionaries with names. | |
This class is same as a hierarchical dictionary, but it | |
provides method to add/accesss/modify children by name, | |
like a Composit. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import os | |
import fnmatch | |
class Expression(object): | |
def __and__(self, other): | |
return And(self, other) | |
def __or__(self, other): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/ | |
"""Implementation of the abstract factory pattern""" | |
import random | |
class PetShop: | |
"""A pet shop""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""http://peter-hoffmann.com/2010/extrinsic-visitor-pattern-python-inheritance.html""" | |
class Node(object): | |
pass | |
class A(Node): | |
pass | |
class B(Node): | |
pass |