Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions patterns/behavioral/catalog.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
A class that uses different static function depending of a parameter passed in
Expand All @@ -9,7 +8,7 @@
__author__ = "Ibrahim Diop <ibrahim@sikilabs.com>"


class Catalog(object):
class Catalog:
"""catalog of multiple static methods that are executed depending on an init

parameter
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion patterns/behavioral/chain_of_responsibility.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
*What is this pattern about?
Expand Down
7 changes: 2 additions & 5 deletions patterns/behavioral/chaining_method.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -14,7 +11,7 @@ def do_action(self):
return self.action


class Action(object):
class Action:
def __init__(self, name):
self.name = name

Expand Down
6 changes: 2 additions & 4 deletions patterns/behavioral/command.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
*TL;DR
Expand All @@ -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
Expand All @@ -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)


Expand Down
3 changes: 0 additions & 3 deletions patterns/behavioral/iterator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/
Expand All @@ -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"""
Expand Down
5 changes: 2 additions & 3 deletions patterns/behavioral/mediator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
https://www.djangospin.com/design-patterns-python/mediator/
Expand All @@ -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):
Expand Down
7 changes: 3 additions & 4 deletions patterns/behavioral/memento.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
http://code.activestate.com/recipes/413838-memento-closure/
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -65,7 +64,7 @@ def transaction(*args, **kwargs):
return transaction


class NumObj(object):
class NumObj:
def __init__(self, value):
self.value = value

Expand Down
11 changes: 3 additions & 8 deletions patterns/behavioral/observer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
http://code.activestate.com/recipes/131499-observer-pattern/
Expand All @@ -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 = []

Expand All @@ -35,7 +32,6 @@ def notify(self, modifier=None):
observer.update(self)


# Example usage
class Data(Subject):
def __init__(self, name=''):
Subject.__init__(self)
Expand All @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion patterns/behavioral/publish_subscribe.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Reference:
http://www.slideshare.net/ishraqabd/publish-subscribe-model-overview-13368808
Expand Down
1 change: 0 additions & 1 deletion patterns/behavioral/registry.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-


class RegistryHolder(type):
Expand Down
5 changes: 2 additions & 3 deletions patterns/behavioral/specification.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
@author: Gordeev Andrey <gordeev.and.and@gmail.com>
Expand All @@ -11,7 +10,7 @@
from abc import abstractmethod


class Specification(object):
class Specification:
def and_specification(self, candidate):
raise NotImplementedError()

Expand Down Expand Up @@ -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

Expand Down
13 changes: 5 additions & 8 deletions patterns/behavioral/state.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Implementation of the state pattern
Expand All @@ -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"""

Expand All @@ -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):
Expand All @@ -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


Expand All @@ -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."""

Expand Down
1 change: 0 additions & 1 deletion patterns/behavioral/strategy.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
*What is this pattern about?
Expand Down
1 change: 0 additions & 1 deletion patterns/behavioral/template.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
An example of the Template pattern in Python
Expand Down
5 changes: 2 additions & 3 deletions patterns/behavioral/visitor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
http://peter-hoffmann.com/2010/extrinsic-visitor-pattern-python-inheritance.html
Expand All @@ -19,7 +18,7 @@
"""


class Node(object):
class Node:
pass


Expand All @@ -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__:
Expand Down
7 changes: 3 additions & 4 deletions patterns/creational/abstract_factory.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
*What is this pattern about?
Expand Down Expand Up @@ -36,7 +35,7 @@
import random


class PetShop(object):
class PetShop:

"""A pet shop"""

Expand All @@ -53,15 +52,15 @@ def show_pet(self):
print("It says {}".format(pet.speak()))


class Dog(object):
class Dog:
def speak(self):
return "woof"

def __str__(self):
return "Dog"


class Cat(object):
class Cat:
def speak(self):
return "meow"

Expand Down
3 changes: 1 addition & 2 deletions patterns/creational/borg.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
*What is this pattern about?
Expand Down Expand Up @@ -41,7 +40,7 @@
"""


class Borg(object):
class Borg:
__shared_state = {}

def __init__(self):
Expand Down
Loading