diff --git a/patterns/behavioral/catalog.py b/patterns/behavioral/catalog.py index 1a3246205..1f96d0b00 100644 --- a/patterns/behavioral/catalog.py +++ b/patterns/behavioral/catalog.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- """ A class that uses different static function depending of a parameter passed in @@ -9,7 +8,7 @@ __author__ = "Ibrahim Diop " -class Catalog(object): +class Catalog: """catalog of multiple static methods that are executed depending on an init parameter @@ -45,7 +44,7 @@ def main_method(self): # Alternative implementation for different levels of methods -class CatalogInstance(object): +class CatalogInstance: """catalog of multiple methods that are executed depending on an init @@ -77,7 +76,7 @@ def main_method(self): self._instance_method_choices[self.param].__get__(self)() -class CatalogClass(object): +class CatalogClass: """catalog of multiple class methods that are executed depending on an init @@ -112,7 +111,7 @@ def main_method(self): self._class_method_choices[self.param].__get__(None, self.__class__)() -class CatalogStatic(object): +class CatalogStatic: """catalog of multiple static methods that are executed depending on an init diff --git a/patterns/behavioral/chain_of_responsibility.py b/patterns/behavioral/chain_of_responsibility.py index 2b86d1938..9c000e1ed 100644 --- a/patterns/behavioral/chain_of_responsibility.py +++ b/patterns/behavioral/chain_of_responsibility.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- """ *What is this pattern about? diff --git a/patterns/behavioral/chaining_method.py b/patterns/behavioral/chaining_method.py index 01ef035be..e4779ec71 100644 --- a/patterns/behavioral/chaining_method.py +++ b/patterns/behavioral/chaining_method.py @@ -1,10 +1,7 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- -from __future__ import print_function - -class Person(object): +class Person: def __init__(self, name, action): self.name = name self.action = action @@ -14,7 +11,7 @@ def do_action(self): return self.action -class Action(object): +class Action: def __init__(self, name): self.name = name diff --git a/patterns/behavioral/command.py b/patterns/behavioral/command.py index 4a39420aa..5e6a4dcd6 100644 --- a/patterns/behavioral/command.py +++ b/patterns/behavioral/command.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- """ *TL;DR @@ -10,11 +9,10 @@ https://docs.djangoproject.com/en/2.1/ref/request-response/#httprequest-objects """ -from __future__ import print_function import os -class MoveFileCommand(object): +class MoveFileCommand: def __init__(self, src, dest): self.src = src self.dest = dest @@ -26,7 +24,7 @@ def undo(self): self.rename(self.dest, self.src) def rename(self, src, dest): - print(u"renaming %s to %s" % (src, dest)) + print("renaming {} to {}".format(src, dest)) os.rename(src, dest) diff --git a/patterns/behavioral/iterator.py b/patterns/behavioral/iterator.py index 625b77e31..129da047a 100644 --- a/patterns/behavioral/iterator.py +++ b/patterns/behavioral/iterator.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- """ http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/ @@ -9,8 +8,6 @@ Traverses a container and accesses the container's elements. """ -from __future__ import print_function - def count_to(count): """Counts by word numbers, up to a maximum of five""" diff --git a/patterns/behavioral/mediator.py b/patterns/behavioral/mediator.py index 073596c28..ac6e5cf9b 100644 --- a/patterns/behavioral/mediator.py +++ b/patterns/behavioral/mediator.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- """ https://www.djangospin.com/design-patterns-python/mediator/ @@ -12,14 +11,14 @@ """ -class ChatRoom(object): +class ChatRoom: """Mediator class""" def display_message(self, user, message): print("[{} says]: {}".format(user, message)) -class User(object): +class User: """A class whose instances want to interact with each other""" def __init__(self, name): diff --git a/patterns/behavioral/memento.py b/patterns/behavioral/memento.py index f7acd254a..ddb02b2d0 100644 --- a/patterns/behavioral/memento.py +++ b/patterns/behavioral/memento.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- """ http://code.activestate.com/recipes/413838-memento-closure/ @@ -22,7 +21,7 @@ def restore(): return restore -class Transaction(object): +class Transaction: """A transaction guard. This is, in fact, just syntactic sugar around a memento closure. @@ -44,7 +43,7 @@ def rollback(self): a_state() -class Transactional(object): +class Transactional: """Adds transactional semantics to methods. Methods decorated with @Transactional will rollback to entry-state upon exceptions. @@ -65,7 +64,7 @@ def transaction(*args, **kwargs): return transaction -class NumObj(object): +class NumObj: def __init__(self, value): self.value = value diff --git a/patterns/behavioral/observer.py b/patterns/behavioral/observer.py index 2590de149..ce75a9b3f 100644 --- a/patterns/behavioral/observer.py +++ b/patterns/behavioral/observer.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- """ http://code.activestate.com/recipes/131499-observer-pattern/ @@ -12,10 +11,8 @@ Flask Signals: http://flask.pocoo.org/docs/1.0/signals/ """ -from __future__ import print_function - -class Subject(object): +class Subject: def __init__(self): self._observers = [] @@ -35,7 +32,6 @@ def notify(self, modifier=None): observer.update(self) -# Example usage class Data(Subject): def __init__(self, name=''): Subject.__init__(self) @@ -54,15 +50,14 @@ def data(self, value): class HexViewer: def update(self, subject): - print(u'HexViewer: Subject %s has data 0x%x' % (subject.name, subject.data)) + print('HexViewer: Subject {} has data 0x{:x}'.format(subject.name, subject.data)) class DecimalViewer: def update(self, subject): - print(u'DecimalViewer: Subject %s has data %d' % (subject.name, subject.data)) + print('DecimalViewer: Subject %s has data %d' % (subject.name, subject.data)) -# Example usage... def main(): """ >>> data1 = Data('Data 1') diff --git a/patterns/behavioral/publish_subscribe.py b/patterns/behavioral/publish_subscribe.py index 131181e08..73309c438 100644 --- a/patterns/behavioral/publish_subscribe.py +++ b/patterns/behavioral/publish_subscribe.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- + """ Reference: http://www.slideshare.net/ishraqabd/publish-subscribe-model-overview-13368808 diff --git a/patterns/behavioral/registry.py b/patterns/behavioral/registry.py index 82c4eaa2b..7843b0ce7 100644 --- a/patterns/behavioral/registry.py +++ b/patterns/behavioral/registry.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- class RegistryHolder(type): diff --git a/patterns/behavioral/specification.py b/patterns/behavioral/specification.py index 4959241e7..1c6c76474 100644 --- a/patterns/behavioral/specification.py +++ b/patterns/behavioral/specification.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- """ @author: Gordeev Andrey @@ -11,7 +10,7 @@ from abc import abstractmethod -class Specification(object): +class Specification: def and_specification(self, candidate): raise NotImplementedError() @@ -75,7 +74,7 @@ def is_satisfied_by(self, candidate): return bool(not self._wrapped.is_satisfied_by(candidate)) -class User(object): +class User: def __init__(self, super_user=False): self.super_user = super_user diff --git a/patterns/behavioral/state.py b/patterns/behavioral/state.py index 0bf2d012e..f4fa77389 100644 --- a/patterns/behavioral/state.py +++ b/patterns/behavioral/state.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- """ Implementation of the state pattern @@ -11,10 +10,8 @@ Implements state transitions by invoking methods from the pattern's superclass. """ -from __future__ import print_function - -class State(object): +class State: """Base state. This is to share functionality""" @@ -23,7 +20,7 @@ def scan(self): self.pos += 1 if self.pos == len(self.stations): self.pos = 0 - print(u"Scanning... Station is %s %s" % (self.stations[self.pos], self.name)) + print("Scanning... Station is {} {}".format(self.stations[self.pos], self.name)) class AmState(State): @@ -34,7 +31,7 @@ def __init__(self, radio): self.name = "AM" def toggle_amfm(self): - print(u"Switching to FM") + print("Switching to FM") self.radio.state = self.radio.fmstate @@ -46,11 +43,11 @@ def __init__(self, radio): self.name = "FM" def toggle_amfm(self): - print(u"Switching to AM") + print("Switching to AM") self.radio.state = self.radio.amstate -class Radio(object): +class Radio: """A radio. It has a scan button, and an AM/FM toggle switch.""" diff --git a/patterns/behavioral/strategy.py b/patterns/behavioral/strategy.py index 2dd245fc8..bc6fbf7b9 100644 --- a/patterns/behavioral/strategy.py +++ b/patterns/behavioral/strategy.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- """ *What is this pattern about? diff --git a/patterns/behavioral/template.py b/patterns/behavioral/template.py index c6919b315..d98c614e3 100644 --- a/patterns/behavioral/template.py +++ b/patterns/behavioral/template.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- """ An example of the Template pattern in Python diff --git a/patterns/behavioral/visitor.py b/patterns/behavioral/visitor.py index 9aac35d3c..45f120bd7 100644 --- a/patterns/behavioral/visitor.py +++ b/patterns/behavioral/visitor.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- """ http://peter-hoffmann.com/2010/extrinsic-visitor-pattern-python-inheritance.html @@ -19,7 +18,7 @@ """ -class Node(object): +class Node: pass @@ -35,7 +34,7 @@ class C(A, B): pass -class Visitor(object): +class Visitor: def visit(self, node, *args, **kwargs): meth = None for cls in node.__class__.__mro__: diff --git a/patterns/creational/abstract_factory.py b/patterns/creational/abstract_factory.py index 1690ca75d..ddb40f2a4 100644 --- a/patterns/creational/abstract_factory.py +++ b/patterns/creational/abstract_factory.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- """ *What is this pattern about? @@ -36,7 +35,7 @@ import random -class PetShop(object): +class PetShop: """A pet shop""" @@ -53,7 +52,7 @@ def show_pet(self): print("It says {}".format(pet.speak())) -class Dog(object): +class Dog: def speak(self): return "woof" @@ -61,7 +60,7 @@ def __str__(self): return "Dog" -class Cat(object): +class Cat: def speak(self): return "meow" diff --git a/patterns/creational/borg.py b/patterns/creational/borg.py index e329d130d..7f85e25c1 100644 --- a/patterns/creational/borg.py +++ b/patterns/creational/borg.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- """ *What is this pattern about? @@ -41,7 +40,7 @@ """ -class Borg(object): +class Borg: __shared_state = {} def __init__(self): diff --git a/patterns/creational/builder.py b/patterns/creational/builder.py index 73e0f7f85..6a450165f 100644 --- a/patterns/creational/builder.py +++ b/patterns/creational/builder.py @@ -36,7 +36,7 @@ class for a building, where the initializer (__init__ method) specifies the # Abstract Building -class Building(object): +class Building: def __init__(self): self.build_floor() self.build_size() @@ -74,7 +74,7 @@ def build_size(self): # a concrete class does not have a useful constructor) -class ComplexBuilding(object): +class ComplexBuilding: def __repr__(self): return 'Floor: {0.floor} | Size: {0.size}'.format(self) diff --git a/patterns/creational/factory.py b/patterns/creational/factory.py index b2a36f05e..fea8c36a0 100644 --- a/patterns/creational/factory.py +++ b/patterns/creational/factory.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- """*What is this pattern about? A Factory is an object for creating other objects. @@ -28,11 +27,8 @@ Creates objects without having to specify the exact class. """ -from __future__ import unicode_literals -from __future__ import print_function - -class GreekLocalizer(object): +class GreekLocalizer: """A simple localizer a la gettext""" def __init__(self): @@ -43,7 +39,7 @@ def localize(self, msg): return self.translations.get(msg, msg) -class EnglishLocalizer(object): +class EnglishLocalizer: """Simply echoes the message""" def localize(self, msg): diff --git a/patterns/creational/lazy_evaluation.py b/patterns/creational/lazy_evaluation.py index 06af01000..10cebe231 100644 --- a/patterns/creational/lazy_evaluation.py +++ b/patterns/creational/lazy_evaluation.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- """ Lazily-evaluated property pattern in Python. @@ -22,11 +21,10 @@ Delays the eval of an expr until its value is needed and avoids repeated evals. """ -from __future__ import print_function import functools -class lazy_property(object): +class lazy_property: def __init__(self, function): self.function = function functools.update_wrapper(self, function) @@ -51,7 +49,7 @@ def _lazy_property(self): return _lazy_property -class Person(object): +class Person: def __init__(self, name, occupation): self.name = name self.occupation = occupation diff --git a/patterns/creational/pool.py b/patterns/creational/pool.py index d29fa7ee0..526d325e7 100644 --- a/patterns/creational/pool.py +++ b/patterns/creational/pool.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- """ *What is this pattern about? @@ -32,7 +31,7 @@ """ -class ObjectPool(object): +class ObjectPool: def __init__(self, queue, auto_get=False): self._queue = queue self.item = self._queue.get() if auto_get else None diff --git a/patterns/creational/prototype.py b/patterns/creational/prototype.py index fe2ff16db..00c810821 100644 --- a/patterns/creational/prototype.py +++ b/patterns/creational/prototype.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- """ *What is this pattern about? @@ -25,7 +24,7 @@ """ -class Prototype(object): +class Prototype: value = 'default' @@ -37,7 +36,7 @@ def clone(self, **attrs): return obj -class PrototypeDispatcher(object): +class PrototypeDispatcher: def __init__(self): self._objects = {} diff --git a/patterns/dependency_injection.py b/patterns/dependency_injection.py index 06dee725d..c75eabb88 100644 --- a/patterns/dependency_injection.py +++ b/patterns/dependency_injection.py @@ -29,7 +29,7 @@ def get_current_time_as_html_fragment(self): import datetime -class ConstructorInjection(object): +class ConstructorInjection: def __init__(self, time_provider): self.time_provider = time_provider @@ -40,7 +40,7 @@ def get_current_time_as_html_fragment(self): return current_time_as_html_fragment -class ParameterInjection(object): +class ParameterInjection: def __init__(self): pass @@ -51,7 +51,7 @@ def get_current_time_as_html_fragment(self, time_provider): return current_time_as_html_fragment -class SetterInjection(object): +class SetterInjection: """Setter Injection""" def __init__(self): diff --git a/patterns/fundamental/delegation_pattern.py b/patterns/fundamental/delegation_pattern.py index 81ec18326..e58443197 100644 --- a/patterns/fundamental/delegation_pattern.py +++ b/patterns/fundamental/delegation_pattern.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- """ Reference: https://en.wikipedia.org/wiki/Delegation_pattern @@ -10,7 +9,7 @@ """ -class Delegator(object): +class Delegator: """ >>> delegator = Delegator(Delegate()) >>> delegator.p1 @@ -41,7 +40,7 @@ def wrapper(*args, **kwargs): return wrapper -class Delegate(object): +class Delegate: def __init__(self): self.p1 = 123 diff --git a/patterns/other/blackboard.py b/patterns/other/blackboard.py index b9f8d9d0e..61eb38c77 100644 --- a/patterns/other/blackboard.py +++ b/patterns/other/blackboard.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- """ @author: Eugene Duboviy | github.com/duboviy @@ -16,7 +15,7 @@ import random -class Blackboard(object): +class Blackboard: def __init__(self): self.experts = [] self.common_state = { @@ -30,7 +29,7 @@ def add_expert(self, expert): self.experts.append(expert) -class Controller(object): +class Controller: def __init__(self, blackboard): self.blackboard = blackboard diff --git a/patterns/other/graph_search.py b/patterns/other/graph_search.py index 35ad4f10e..f340f11a7 100644 --- a/patterns/other/graph_search.py +++ b/patterns/other/graph_search.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- "" diff --git a/patterns/other/hsm/hsm.py b/patterns/other/hsm/hsm.py index 3665b3242..26bb2da88 100644 --- a/patterns/other/hsm/hsm.py +++ b/patterns/other/hsm/hsm.py @@ -21,7 +21,7 @@ class UnsupportedTransition(BaseException): pass -class HierachicalStateMachine(object): +class HierachicalStateMachine: def __init__(self): self._active_state = Active(self) # Unit.Inservice.Active() self._standby_state = Standby(self) # Unit.Inservice.Standby() @@ -85,7 +85,7 @@ def on_message(self, message_type): # message ignored raise UnsupportedMessageType -class Unit(object): +class Unit: def __init__(self, HierachicalStateMachine): self.hsm = HierachicalStateMachine @@ -125,8 +125,8 @@ def __init__(self, HierachicalStateMachine): self._hsm = HierachicalStateMachine def on_fault_trigger(self): - super(Active, self).perform_switchover() - super(Active, self).on_fault_trigger() + super().perform_switchover() + super().on_fault_trigger() def on_switchover(self): self._hsm.on_switchover() # message ignored @@ -138,7 +138,7 @@ def __init__(self, HierachicalStateMachine): self._hsm = HierachicalStateMachine def on_switchover(self): - super(Standby, self).on_switchover() # message ignored + super().on_switchover() # message ignored self._hsm._next_state('active') @@ -157,17 +157,17 @@ def __init__(self, HierachicalStateMachine): self._hsm = HierachicalStateMachine def on_diagnostics_failed(self): - super(Suspect, self).send_diagnostics_failure_report() - super(Suspect, self).next_state('failed') + super().send_diagnostics_failure_report() + super().next_state('failed') def on_diagnostics_passed(self): - super(Suspect, self).send_diagnostics_pass_report() - super(Suspect, self).clear_alarm() # loss of redundancy alarm - super(Suspect, self).next_state('standby') + super().send_diagnostics_pass_report() + super().clear_alarm() # loss of redundancy alarm + super().next_state('standby') def on_operator_inservice(self): - super(Suspect, self).abort_diagnostics() - super(Suspect, self).on_operator_inservice() # message ignored + super().abort_diagnostics() + super().on_operator_inservice() # message ignored class Failed(OutOfService): diff --git a/patterns/structural/3-tier.py b/patterns/structural/3-tier.py index 4e45844cf..b922f642f 100644 --- a/patterns/structural/3-tier.py +++ b/patterns/structural/3-tier.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- """ *TL;DR @@ -7,7 +6,7 @@ """ -class Data(object): +class Data: """ Data Store Class """ products = { @@ -21,7 +20,7 @@ def __get__(self, obj, klas): return {'products': self.products} -class BusinessLogic(object): +class BusinessLogic: """ Business logic holding data store instances """ data = Data() @@ -33,7 +32,7 @@ def product_information(self, product): return self.data['products'].get(product, None) -class Ui(object): +class Ui: """ UI interaction class """ def __init__(self): diff --git a/patterns/structural/adapter.py b/patterns/structural/adapter.py index e7a6e6087..e3167a20e 100644 --- a/patterns/structural/adapter.py +++ b/patterns/structural/adapter.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- """ *What is this pattern about? @@ -32,7 +31,7 @@ """ -class Dog(object): +class Dog: def __init__(self): self.name = "Dog" @@ -40,7 +39,7 @@ def bark(self): return "woof!" -class Cat(object): +class Cat: def __init__(self): self.name = "Cat" @@ -48,7 +47,7 @@ def meow(self): return "meow!" -class Human(object): +class Human: def __init__(self): self.name = "Human" @@ -56,7 +55,7 @@ def speak(self): return "'hello'" -class Car(object): +class Car: def __init__(self): self.name = "Car" @@ -64,7 +63,7 @@ def make_noise(self, octane_level): return "vroom{0}".format("!" * octane_level) -class Adapter(object): +class Adapter: """ Adapts an object by replacing methods. Usage: diff --git a/patterns/structural/bridge.py b/patterns/structural/bridge.py index 3d9a4d93c..28b70fc28 100644 --- a/patterns/structural/bridge.py +++ b/patterns/structural/bridge.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- """ *References: @@ -11,19 +10,19 @@ # ConcreteImplementor 1/2 -class DrawingAPI1(object): +class DrawingAPI1: def draw_circle(self, x, y, radius): print('API1.circle at {}:{} radius {}'.format(x, y, radius)) # ConcreteImplementor 2/2 -class DrawingAPI2(object): +class DrawingAPI2: def draw_circle(self, x, y, radius): print('API2.circle at {}:{} radius {}'.format(x, y, radius)) # Refined Abstraction -class CircleShape(object): +class CircleShape: def __init__(self, x, y, radius, drawing_api): self._x = x self._y = y diff --git a/patterns/structural/composite.py b/patterns/structural/composite.py index 12f2dcaf0..aaa0a432b 100644 --- a/patterns/structural/composite.py +++ b/patterns/structural/composite.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- """ *What is this pattern about? diff --git a/patterns/structural/decorator.py b/patterns/structural/decorator.py index c06e987b7..e6e094d57 100644 --- a/patterns/structural/decorator.py +++ b/patterns/structural/decorator.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- """ *What is this pattern about? @@ -27,10 +26,8 @@ Adds behaviour to object without affecting its class. """ -from __future__ import print_function - -class TextTag(object): +class TextTag: """Represents a base text tag""" def __init__(self, text): diff --git a/patterns/structural/facade.py b/patterns/structural/facade.py index 64285a047..0dee36c1b 100644 --- a/patterns/structural/facade.py +++ b/patterns/structural/facade.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- """ Example from https://en.wikipedia.org/wiki/Facade_pattern#Python @@ -31,11 +30,9 @@ Provides a simpler unified interface to a complex system. """ -from __future__ import print_function - # Complex computer parts -class CPU(object): +class CPU: """ Simple CPU representation. """ @@ -49,7 +46,7 @@ def execute(self): print("Executing.") -class Memory(object): +class Memory: """ Simple memory representation. """ @@ -57,7 +54,7 @@ def load(self, position, data): print("Loading from {0} data: '{1}'.".format(position, data)) -class SolidStateDrive(object): +class SolidStateDrive: """ Simple solid state drive representation. """ @@ -65,7 +62,7 @@ def read(self, lba, size): return "Some data from sector {0} with size {1}".format(lba, size) -class ComputerFacade(object): +class ComputerFacade: """ Represents a facade for various computer parts. """ diff --git a/patterns/structural/flyweight.py b/patterns/structural/flyweight.py index 4ca95f566..585483793 100644 --- a/patterns/structural/flyweight.py +++ b/patterns/structural/flyweight.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- """ *What is this pattern about? @@ -31,7 +30,7 @@ import weakref -class Card(object): +class Card: """The Flyweight""" # Could be a simple dict. @@ -56,7 +55,7 @@ def __new__(cls, value, suit): # self.value, self.suit = value, suit def __repr__(self): - return "" % (self.value, self.suit) + return "".format(self.value, self.suit) def main(): diff --git a/patterns/structural/flyweight_with_metaclass.py b/patterns/structural/flyweight_with_metaclass.py index 33536b3d8..bba21360c 100644 --- a/patterns/structural/flyweight_with_metaclass.py +++ b/patterns/structural/flyweight_with_metaclass.py @@ -13,7 +13,7 @@ def __new__(mcs, name, parents, dct): :return: new class """ dct['pool'] = weakref.WeakValueDictionary() - return super(FlyweightMeta, mcs).__new__(mcs, name, parents, dct) + return super().__new__(mcs, name, parents, dct) @staticmethod def _serialize_params(cls, *args, **kwargs): @@ -32,7 +32,7 @@ def __call__(cls, *args, **kwargs): instance = pool.get(key) if instance is None: - instance = super(FlyweightMeta, cls).__call__(*args, **kwargs) + instance = super().__call__(*args, **kwargs) pool[key] = instance return instance diff --git a/patterns/structural/front_controller.py b/patterns/structural/front_controller.py index e533e53d0..bf857651f 100644 --- a/patterns/structural/front_controller.py +++ b/patterns/structural/front_controller.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- """ @author: Gordeev Andrey @@ -9,17 +8,17 @@ """ -class MobileView(object): +class MobileView: def show_index_page(self): print('Displaying mobile index page') -class TabletView(object): +class TabletView: def show_index_page(self): print('Displaying tablet index page') -class Dispatcher(object): +class Dispatcher: def __init__(self): self.mobile_view = MobileView() self.tablet_view = TabletView() @@ -33,7 +32,7 @@ def dispatch(self, request): print('cant dispatch the request') -class RequestController(object): +class RequestController: """ front controller """ def __init__(self): @@ -46,7 +45,7 @@ def dispatch_request(self, request): print('request must be a Request object') -class Request(object): +class Request: """ request """ mobile_type = 'mobile' diff --git a/patterns/structural/mvc.py b/patterns/structural/mvc.py index a95ef668e..4e1a4c0ef 100644 --- a/patterns/structural/mvc.py +++ b/patterns/structural/mvc.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- """ *TL;DR @@ -7,7 +6,7 @@ """ -class Model(object): +class Model: def __iter__(self): raise NotImplementedError @@ -45,10 +44,10 @@ def get(self, product): try: return self.products[product] except KeyError as e: - raise KeyError((str(e) + " not in the model's item list.")) + raise KeyError(str(e) + " not in the model's item list.") -class View(object): +class View: def show_item_list(self, item_type, item_list): raise NotImplementedError @@ -81,10 +80,10 @@ def show_item_information(self, item_type, item_name, item_info): print(printout) def item_not_found(self, item_type, item_name): - print('That %s "%s" does not exist in the records' % (item_type, item_name)) + print('That {} "{}" does not exist in the records'.format(item_type, item_name)) -class Controller(object): +class Controller: def __init__(self, model, view): self.model = model self.view = view diff --git a/patterns/structural/proxy.py b/patterns/structural/proxy.py index b3b0091d8..f2d3c8b23 100644 --- a/patterns/structural/proxy.py +++ b/patterns/structural/proxy.py @@ -1,12 +1,10 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- """ *TL;DR Provides an interface to resource that is expensive to duplicate. """ -from __future__ import print_function import time