From 5d794ceecbbbb6be7ead9a655ecd91d4d05df108 Mon Sep 17 00:00:00 2001 From: Arnold Schwarzenegger Date: Wed, 19 Mar 2014 03:02:06 -0400 Subject: [PATCH] Your code is too weak for PEP8. You lack DISCIPLINE --- 3-tier.py | 2 +- abstract_factory.py | 5 +++++ adapter.py | 6 ++++++ bridge.py | 3 +++ builder.py | 7 ++++++- catalog.py | 1 + chain.py | 5 +++++ command.py | 1 + composite.py | 2 ++ decorator.py | 3 +++ facade.py | 4 ++++ factory_method.py | 4 ++++ flyweight.py | 1 + graph_search.py | 5 +++-- iterator.py | 4 ++-- mediator.py | 6 +++++- memento.py | 6 +++++- mvc.py | 8 ++++---- null.py | 1 + observer.py | 4 ++++ pool.py | 1 + prototype.py | 1 + proxy.py | 2 ++ publish_subscribe.py | 3 +++ state.py | 5 +++++ strategy.py | 1 + visitor.py | 7 ++++--- 27 files changed, 83 insertions(+), 15 deletions(-) diff --git a/3-tier.py b/3-tier.py index c644e0b9..97f159ed 100644 --- a/3-tier.py +++ b/3-tier.py @@ -62,7 +62,7 @@ def main(): # eggs # milk # cheese -# +# # PRODUCT INFORMATION: # Name: Cheese, Price: 2.00, Quantity: 10 # PRODUCT INFORMATION: diff --git a/abstract_factory.py b/abstract_factory.py index 269c09e9..91807222 100644 --- a/abstract_factory.py +++ b/abstract_factory.py @@ -9,6 +9,7 @@ class PetShop: + """A pet shop""" def __init__(self, animal_factory=None): @@ -30,6 +31,7 @@ def show_pet(self): # Stuff that our factory makes class Dog: + def speak(self): return "woof" @@ -38,6 +40,7 @@ def __str__(self): class Cat: + def speak(self): return "meow" @@ -48,6 +51,7 @@ def __str__(self): # Factory classes class DogFactory: + def get_pet(self): return Dog() @@ -56,6 +60,7 @@ def get_food(self): class CatFactory: + def get_pet(self): return Cat() diff --git a/adapter.py b/adapter.py index c316c001..2db7faa1 100644 --- a/adapter.py +++ b/adapter.py @@ -7,6 +7,7 @@ class Dog(object): + def __init__(self): self.name = "Dog" @@ -15,6 +16,7 @@ def bark(self): class Cat(object): + def __init__(self): self.name = "Cat" @@ -23,6 +25,7 @@ def meow(self): class Human(object): + def __init__(self): self.name = "Human" @@ -31,6 +34,7 @@ def speak(self): class Car(object): + def __init__(self): self.name = "Car" @@ -39,6 +43,7 @@ def make_noise(self, octane_level): class Adapter(object): + """ Adapts an object by replacing methods. Usage: @@ -63,6 +68,7 @@ class Adapter(object): A Human goes 'hello' A Car goes vroom!!! """ + def __init__(self, obj, adapted_methods): """We set the adapted methods in the object's dict""" self.obj = obj diff --git a/bridge.py b/bridge.py index 2330f084..1c3c0747 100644 --- a/bridge.py +++ b/bridge.py @@ -6,18 +6,21 @@ # ConcreteImplementor 1/2 class DrawingAPI1(object): + def draw_circle(self, x, y, radius): print('API1.circle at {}:{} radius {}'.format(x, y, radius)) # ConcreteImplementor 2/2 class DrawingAPI2(object): + def draw_circle(self, x, y, radius): print('API2.circle at {}:{} radius {}'.format(x, y, radius)) # Refined Abstraction class CircleShape(object): + def __init__(self, x, y, radius, drawing_api): self._x = x self._y = y diff --git a/builder.py b/builder.py index 5f633ca2..3b04412d 100644 --- a/builder.py +++ b/builder.py @@ -9,6 +9,7 @@ # Director class Director(object): + def __init__(self): self.builder = None @@ -23,6 +24,7 @@ def get_building(self): # Abstract Builder class Builder(object): + def __init__(self): self.building = None @@ -32,6 +34,7 @@ def new_building(self): # Concrete Builder class BuilderHouse(Builder): + def build_floor(self): self.building.floor = 'One' @@ -40,15 +43,17 @@ def build_size(self): class BuilderFlat(Builder): + def build_floor(self): self.building.floor = 'More than One' - + def build_size(self): self.building.size = 'Small' # Product class Building(object): + def __init__(self): self.floor = None self.size = None diff --git a/catalog.py b/catalog.py index 138e11e3..bc6debba 100644 --- a/catalog.py +++ b/catalog.py @@ -10,6 +10,7 @@ class Catalog(): + """ catalog of multiple static methods that are executed depending on an init parameter diff --git a/chain.py b/chain.py index 7fa8b2fd..3a1f45ac 100644 --- a/chain.py +++ b/chain.py @@ -5,11 +5,13 @@ class Handler: + def successor(self, successor): self.successor = successor class ConcreteHandler1(Handler): + def handle(self, request): if 0 < request <= 10: print('request {} handled in handler 1'.format(request)) @@ -18,6 +20,7 @@ def handle(self, request): class ConcreteHandler2(Handler): + def handle(self, request): if 10 < request <= 20: print('request {} handled in handler 2'.format(request)) @@ -26,6 +29,7 @@ def handle(self, request): class ConcreteHandler3(Handler): + def handle(self, request): if 20 < request <= 30: print('request {} handled in handler 3'.format(request)) @@ -34,6 +38,7 @@ def handle(self, request): class Client: + def __init__(self): h1 = ConcreteHandler1() h2 = ConcreteHandler2() diff --git a/command.py b/command.py index a2ccac57..9aab10f6 100644 --- a/command.py +++ b/command.py @@ -5,6 +5,7 @@ class MoveFileCommand(object): + def __init__(self, src, dest): self.src = src self.dest = dest diff --git a/composite.py b/composite.py index 08a35100..9b8cc56f 100644 --- a/composite.py +++ b/composite.py @@ -37,6 +37,7 @@ def denormalize(val): class SpecialDict(dict): + """ A dictionary type which allows direct attribute access to its keys """ @@ -71,6 +72,7 @@ def __setattr__(self, name, value): class CompositeDict(SpecialDict): + """ A class which works like a hierarchical dictionary. This class is based on the Composite design-pattern """ diff --git a/decorator.py b/decorator.py index 045cff9b..591e0ac1 100644 --- a/decorator.py +++ b/decorator.py @@ -5,6 +5,7 @@ class foo_decorator(object): + def __init__(self, decoratee): self._decoratee = decoratee @@ -17,6 +18,7 @@ def __getattr__(self, name): class undecorated_foo(object): + def f1(self): print("original f1") @@ -26,6 +28,7 @@ def f2(self): @foo_decorator class decorated_foo(object): + def f1(self): print("original f1") diff --git a/facade.py b/facade.py index 027b6be9..77197dc3 100644 --- a/facade.py +++ b/facade.py @@ -8,6 +8,7 @@ # Complex Parts class TC1: + def run(self): print("###### In Test 1 ######") time.sleep(SLEEP) @@ -21,6 +22,7 @@ def run(self): class TC2: + def run(self): print("###### In Test 2 ######") time.sleep(SLEEP) @@ -34,6 +36,7 @@ def run(self): class TC3: + def run(self): print("###### In Test 3 ######") time.sleep(SLEEP) @@ -48,6 +51,7 @@ def run(self): # Facade class TestRunner: + def __init__(self): self.tc1 = TC1() self.tc2 = TC2() diff --git a/factory_method.py b/factory_method.py index b730b643..65c2e8d2 100644 --- a/factory_method.py +++ b/factory_method.py @@ -5,7 +5,9 @@ class GreekGetter: + """A simple localizer a la gettext""" + def __init__(self): self.trans = dict(dog="σκύλος", cat="γάτα") @@ -18,7 +20,9 @@ def get(self, msgid): class EnglishGetter: + """Simply echoes the msg ids""" + def get(self, msgid): return str(msgid) diff --git a/flyweight.py b/flyweight.py index b2edbea9..efa48981 100644 --- a/flyweight.py +++ b/flyweight.py @@ -7,6 +7,7 @@ class Card(object): + """The object pool. Has builtin reference counting""" _CardPool = weakref.WeakValueDictionary() diff --git a/graph_search.py b/graph_search.py index 39549757..191b3406 100644 --- a/graph_search.py +++ b/graph_search.py @@ -3,6 +3,7 @@ class GraphSearch: + """Graph search emulation in python, from source http://www.python.org/doc/essays/graphs/""" @@ -62,7 +63,7 @@ def find_shortest_path(self, start, end, path=[]): shortest = newpath return shortest -#example of graph usage +# example of graph usage graph = {'A': ['B', 'C'], 'B': ['C', 'D'], 'C': ['D'], @@ -71,7 +72,7 @@ def find_shortest_path(self, start, end, path=[]): 'F': ['C'] } -#initialization of new graph search object +# initialization of new graph search object graph1 = GraphSearch(graph) diff --git a/iterator.py b/iterator.py index 08660a0c..3aa36b8d 100644 --- a/iterator.py +++ b/iterator.py @@ -32,6 +32,6 @@ def count_to(count): ### OUTPUT ### # Counting to two... -# one two +# one two # Counting to five... -# one two three four five +# one two three four five diff --git a/mediator.py b/mediator.py index ade9eaa8..82a2886f 100644 --- a/mediator.py +++ b/mediator.py @@ -8,6 +8,7 @@ class TC: + def __init__(self): self._tm = None self._bProblem = 0 @@ -40,6 +41,7 @@ def setProblem(self, value): class Reporter: + def __init__(self): self._tm = None @@ -56,13 +58,14 @@ def setTM(self, tm): class DB: + def __init__(self): self._tm = None def insert(self): print("Inserting the execution begin status in the Database") time.sleep(0.1) - #Following code is to simulate a communication from DB to TC + # Following code is to simulate a communication from DB to TC if random.randrange(1, 4) == 3: return -1 @@ -75,6 +78,7 @@ def setTM(self, tm): class TestManager: + def __init__(self): self._reporter = None self._db = None diff --git a/memento.py b/memento.py index 5a83d213..42034658 100644 --- a/memento.py +++ b/memento.py @@ -16,6 +16,7 @@ def Restore(): class Transaction: + """A transaction guard. This is really just syntactic suggar arount a memento closure. """ @@ -34,9 +35,11 @@ def Rollback(self): class transactional(object): + """Adds transactional semantics to methods. Methods decorated with @transactional will rollback to entry state upon exceptions. """ + def __init__(self, method): self.method = method @@ -52,6 +55,7 @@ def transaction(*args, **kwargs): class NumObj(object): + def __init__(self, value): self.value = value @@ -116,7 +120,7 @@ def DoStuff(self): # File "memento.py", line 47, in transaction # return self.method(obj, *args, **kwargs) # File "memento.py", line 67, in DoStuff -# self.Increment() # <- will fail and rollback +# self.Increment() # <- will fail and rollback # File "memento.py", line 62, in Increment # self.value += 1 # TypeError: Can't convert 'int' object to str implicitly diff --git a/mvc.py b/mvc.py index 736d4ab0..8087bdab 100644 --- a/mvc.py +++ b/mvc.py @@ -61,14 +61,14 @@ def get_product_information(self, product): # cheese # eggs # milk -# +# # PRODUCT INFORMATION: # Name: Cheese, Price: 2.00, Quantity: 10 -# +# # PRODUCT INFORMATION: # Name: Eggs, Price: 0.20, Quantity: 100 -# +# # PRODUCT INFORMATION: # Name: Milk, Price: 1.50, Quantity: 10 -# +# # That product "arepas" does not exist in the records diff --git a/null.py b/null.py index 92026d1d..39b8e556 100644 --- a/null.py +++ b/null.py @@ -5,6 +5,7 @@ class Null: + def __init__(self, *args, **kwargs): """Ignore parameters.""" return None diff --git a/observer.py b/observer.py index 76e77bfb..a24c5bc3 100644 --- a/observer.py +++ b/observer.py @@ -5,6 +5,7 @@ class Subject(object): + def __init__(self): self._observers = [] @@ -26,6 +27,7 @@ def notify(self, modifier=None): # Example usage class Data(Subject): + def __init__(self, name=''): Subject.__init__(self) self.name = name @@ -42,12 +44,14 @@ def data(self, value): class HexViewer: + def update(self, subject): print('HexViewer: Subject %s has data 0x%x' % (subject.name, subject.data)) class DecimalViewer: + def update(self, subject): print('DecimalViewer: Subject %s has data %d' % (subject.name, subject.data)) diff --git a/pool.py b/pool.py index c6e31950..ec1107ba 100644 --- a/pool.py +++ b/pool.py @@ -5,6 +5,7 @@ class QueueObject(): + def __init__(self, queue, auto_get=False): self._queue = queue self.object = self._queue.get() if auto_get else None diff --git a/prototype.py b/prototype.py index 69f459cd..e28e5ce6 100644 --- a/prototype.py +++ b/prototype.py @@ -5,6 +5,7 @@ class Prototype: + def __init__(self): self._objects = {} diff --git a/proxy.py b/proxy.py index 1400eb15..4c5b4cc0 100644 --- a/proxy.py +++ b/proxy.py @@ -5,6 +5,7 @@ class SalesManager: + def work(self): print("Sales Manager working...") @@ -13,6 +14,7 @@ def talk(self): class Proxy: + def __init__(self): self.busy = 'No' self.sales = None diff --git a/publish_subscribe.py b/publish_subscribe.py index 255b7dd5..aeb599af 100644 --- a/publish_subscribe.py +++ b/publish_subscribe.py @@ -8,6 +8,7 @@ class Provider: + def __init__(self): self.msg_queue = [] self.subscribers = {} @@ -34,6 +35,7 @@ def update(self): class Publisher: + def __init__(self, msg_center): self.provider = msg_center @@ -42,6 +44,7 @@ def publish(self, msg): class Subscriber: + def __init__(self, name, msg_center): self.name = name self.provider = msg_center diff --git a/state.py b/state.py index d800a917..5f4ef41e 100644 --- a/state.py +++ b/state.py @@ -4,6 +4,7 @@ class State(object): + """Base state. This is to share functionality""" def scan(self): @@ -15,6 +16,7 @@ def scan(self): class AmState(State): + def __init__(self, radio): self.radio = radio self.stations = ["1250", "1380", "1510"] @@ -27,6 +29,7 @@ def toggle_amfm(self): class FmState(State): + def __init__(self, radio): self.radio = radio self.stations = ["81.3", "89.1", "103.9"] @@ -39,7 +42,9 @@ def toggle_amfm(self): class Radio(object): + """A radio. It has a scan button, and an AM/FM toggle switch.""" + def __init__(self): """We have an AM state and an FM state""" self.amstate = AmState(self) diff --git a/strategy.py b/strategy.py index b3131b09..8c7a9b5a 100644 --- a/strategy.py +++ b/strategy.py @@ -12,6 +12,7 @@ class StrategyExample: + def __init__(self, func=None): self.name = 'Strategy Example 0' if func is not None: diff --git a/visitor.py b/visitor.py index bf50f699..ca45102e 100644 --- a/visitor.py +++ b/visitor.py @@ -18,10 +18,11 @@ class C(A, B): class Visitor(object): + def visit(self, node, *args, **kwargs): meth = None for cls in node.__class__.__mro__: - meth_name = 'visit_'+cls.__name__ + meth_name = 'visit_' + cls.__name__ meth = getattr(self, meth_name, None) if meth: break @@ -31,10 +32,10 @@ def visit(self, node, *args, **kwargs): return meth(node, *args, **kwargs) def generic_visit(self, node, *args, **kwargs): - print('generic_visit '+node.__class__.__name__) + print('generic_visit ' + node.__class__.__name__) def visit_B(self, node, *args, **kwargs): - print('visit_B '+node.__class__.__name__) + print('visit_B ' + node.__class__.__name__) a = A()