renamed package to pypattyrn
This commit is contained in:
0
pypattyrn/behavioral/__init__.py
Normal file
0
pypattyrn/behavioral/__init__.py
Normal file
75
pypattyrn/behavioral/chain.py
Normal file
75
pypattyrn/behavioral/chain.py
Normal file
@@ -0,0 +1,75 @@
|
||||
from abc import ABCMeta, abstractmethod
|
||||
|
||||
|
||||
class ChainLink(object, metaclass=ABCMeta):
|
||||
"""
|
||||
Abstract ChainLink object as part of the Chain of Responsibility pattern.
|
||||
|
||||
- External Usage documentation: U{https://github.com/tylerlaberge/PyPatterns/wiki/Behavioral-Pattern-Usage}
|
||||
- External Chain of Responsibility Pattern documentation: U{https://en.wikipedia.org/wiki/Chain-of-responsibility_pattern}
|
||||
"""
|
||||
def __init__(self):
|
||||
"""
|
||||
Initialize a new ChainLink instance.
|
||||
"""
|
||||
self.successor = None
|
||||
|
||||
def set_successor(self, successor):
|
||||
"""
|
||||
Set a chain link to call if this chain link fails.
|
||||
|
||||
@param successor: The chain link to call if this chain link fails.
|
||||
@type successor: ChainLink
|
||||
"""
|
||||
self.successor = successor
|
||||
|
||||
def successor_handle(self, request):
|
||||
"""
|
||||
Have this chain links successor handle a request.
|
||||
|
||||
@param request: The request to handle.
|
||||
"""
|
||||
return self.successor.handle(request)
|
||||
|
||||
@abstractmethod
|
||||
def handle(self, request):
|
||||
"""
|
||||
Handle a request.
|
||||
|
||||
@param request: The request to handle.
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class Chain(object, metaclass=ABCMeta):
|
||||
"""
|
||||
Abstract Chain class as part of the Chain of Responsibility pattern.
|
||||
|
||||
- External Usage documentation: U{https://github.com/tylerlaberge/PyPatterns/wiki/Behavioral-Pattern-Usage}
|
||||
- External Chain of Responsibility Pattern documentation: U{https://en.wikipedia.org/wiki/Chain-of-responsibility_pattern}
|
||||
"""
|
||||
def __init__(self, chainlink):
|
||||
"""
|
||||
Initialize a new Chain instance.
|
||||
|
||||
@param chainlink: The starting chain link.
|
||||
"""
|
||||
self.chainlink = chainlink
|
||||
|
||||
def handle(self, request):
|
||||
"""
|
||||
Handle a request.
|
||||
|
||||
@param request: The request to handle.
|
||||
"""
|
||||
try:
|
||||
return self.chainlink.handle(request)
|
||||
except AttributeError:
|
||||
return self.fail()
|
||||
|
||||
@abstractmethod
|
||||
def fail(self):
|
||||
"""
|
||||
The method to call when the chain could not handle a request.
|
||||
"""
|
||||
pass
|
||||
90
pypattyrn/behavioral/command.py
Normal file
90
pypattyrn/behavioral/command.py
Normal file
@@ -0,0 +1,90 @@
|
||||
from abc import ABCMeta, abstractmethod
|
||||
|
||||
|
||||
class Receiver(object, metaclass=ABCMeta):
|
||||
"""
|
||||
Abstract receiver class as part of the Command pattern.
|
||||
|
||||
- External Usage documentation: U{https://github.com/tylerlaberge/PyPatterns/wiki/Behavioral-Pattern-Usage}
|
||||
- External Command Pattern documentation: U{https://en.wikipedia.org/wiki/Command_pattern}
|
||||
"""
|
||||
def action(self, name, *args, **kwargs):
|
||||
"""
|
||||
Delegates which method to be called for a desired action.
|
||||
|
||||
@param name: The name of the action to execute.
|
||||
@type name: str
|
||||
@param args: Any arguments for the action.
|
||||
@param kwargs: Any keyword arguments for the action.
|
||||
"""
|
||||
try:
|
||||
return getattr(self, name)(*args, **kwargs)
|
||||
except AttributeError:
|
||||
raise AttributeError('Invalid Action.')
|
||||
|
||||
|
||||
class Command(object, metaclass=ABCMeta):
|
||||
"""
|
||||
Abstract Command class as part of the Command pattern.
|
||||
|
||||
- External Usage documentation: U{https://github.com/tylerlaberge/PyPatterns/wiki/Behavioral-Pattern-Usage}
|
||||
- External Command Pattern documentation: U{https://en.wikipedia.org/wiki/Command_pattern}
|
||||
"""
|
||||
def __init__(self, receiver):
|
||||
"""
|
||||
Initialize a new command instance.
|
||||
|
||||
@param receiver: The receiver for this command to use.
|
||||
@type receiver: Receiver
|
||||
"""
|
||||
self._receiver = receiver
|
||||
|
||||
@abstractmethod
|
||||
def execute(self):
|
||||
"""
|
||||
Abstract method for executing an action.
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def unexecute(self):
|
||||
"""
|
||||
Abstract method for unexecuting an action.
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class Invoker(object, metaclass=ABCMeta):
|
||||
"""
|
||||
Abstract Invoker class as part of the Command pattern.
|
||||
|
||||
- External Usage documentation: U{https://github.com/tylerlaberge/PyPatterns/wiki/Behavioral-Pattern-Usage}
|
||||
- External Command Pattern documentation: U{https://en.wikipedia.org/wiki/Command_pattern}
|
||||
"""
|
||||
def __init__(self, valid_commands):
|
||||
"""
|
||||
Initialize a new Invoker instance.
|
||||
|
||||
@param valid_commands: A list of command classes this invoker can handle.
|
||||
"""
|
||||
self._history = []
|
||||
self._valid_commands = valid_commands
|
||||
|
||||
def execute(self, command):
|
||||
"""
|
||||
Execute a command.
|
||||
|
||||
@param command: A command for the invoker to execute.
|
||||
@type command: Command
|
||||
"""
|
||||
if command.__class__ not in self._valid_commands:
|
||||
raise AttributeError('Invalid Command')
|
||||
else:
|
||||
self._history.append(command)
|
||||
return command.execute()
|
||||
|
||||
def undo(self):
|
||||
"""
|
||||
Undo the last command.
|
||||
"""
|
||||
return self._history.pop().unexecute()
|
||||
39
pypattyrn/behavioral/iterator.py
Normal file
39
pypattyrn/behavioral/iterator.py
Normal file
@@ -0,0 +1,39 @@
|
||||
from abc import ABCMeta, abstractmethod
|
||||
|
||||
|
||||
class Iterator(object):
|
||||
"""
|
||||
An Iterator class for the Iterator design pattern.
|
||||
|
||||
- External Usage documentation: U{https://github.com/tylerlaberge/PyPatterns/wiki/Behavioral-Pattern-Usage}
|
||||
- External Iterator Pattern documentation: U{https://en.wikipedia.org/wiki/Iterator_pattern}
|
||||
"""
|
||||
def __init__(self, iterable):
|
||||
"""
|
||||
Initialize a new Iterator instance.
|
||||
|
||||
@param iterable: An Iterable object to iterate over.
|
||||
@type iterable: Iterable
|
||||
"""
|
||||
self.iterable = iterable
|
||||
|
||||
def __iter__(self):
|
||||
return self
|
||||
|
||||
def __next__(self):
|
||||
return self.iterable.__next__()
|
||||
|
||||
|
||||
class Iterable(object, metaclass=ABCMeta):
|
||||
"""
|
||||
An abstract class representing an Iterable object as part of the Iterator design pattern.
|
||||
|
||||
- External Usage documentation: U{https://github.com/tylerlaberge/PyPatterns/wiki/Behavioral-Pattern-Usage}
|
||||
- External Iterator Pattern documentation: U{https://en.wikipedia.org/wiki/Iterator_pattern}
|
||||
"""
|
||||
@abstractmethod
|
||||
def __next__(self):
|
||||
"""
|
||||
All Iterable's must implement a __next__ method which eventually raises StopIteration.
|
||||
"""
|
||||
pass
|
||||
50
pypattyrn/behavioral/mediator.py
Normal file
50
pypattyrn/behavioral/mediator.py
Normal file
@@ -0,0 +1,50 @@
|
||||
from collections import defaultdict
|
||||
|
||||
|
||||
class Mediator(object):
|
||||
"""
|
||||
Mediator class as part of the Mediator design pattern.
|
||||
|
||||
- External Usage documentation: U{https://github.com/tylerlaberge/PyPatterns/wiki/Behavioral-Pattern-Usage}
|
||||
- External Mediator Pattern documentation: U{https://en.wikipedia.org/wiki/Mediator_pattern}
|
||||
"""
|
||||
def __init__(self):
|
||||
"""
|
||||
Initialize a new Mediator instance.
|
||||
"""
|
||||
self.signals = defaultdict(list)
|
||||
|
||||
def signal(self, signal_name, *args, **kwargs):
|
||||
"""
|
||||
Send a signal out to all connected handlers.
|
||||
|
||||
@param signal_name: The name of the signal.
|
||||
@type signal_name: Str
|
||||
@param args: Positional arguments to send with the signal.
|
||||
@param kwargs: Keyword arguments to send with the signal.
|
||||
"""
|
||||
for handler in self.signals[signal_name]:
|
||||
handler(*args, **kwargs)
|
||||
|
||||
def connect(self, signal_name, receiver):
|
||||
"""
|
||||
Connect a receiver to a signal.
|
||||
|
||||
@param signal_name: The name of the signal to connect the receiver to.
|
||||
@type signal_name: str
|
||||
@param receiver: A handler to call when the signal is sent out.
|
||||
"""
|
||||
self.signals[signal_name].append(receiver)
|
||||
|
||||
def disconnect(self, signal_name, receiver):
|
||||
"""
|
||||
Disconnect a receiver from a signal.
|
||||
|
||||
@param signal_name: The name of the signal to disconnect the receiver from.
|
||||
@type signal_name: str
|
||||
@param receiver: The receiver to disconnect from the signal.
|
||||
"""
|
||||
try:
|
||||
self.signals[signal_name].remove(receiver)
|
||||
except ValueError:
|
||||
pass
|
||||
47
pypattyrn/behavioral/memento.py
Normal file
47
pypattyrn/behavioral/memento.py
Normal file
@@ -0,0 +1,47 @@
|
||||
from copy import deepcopy
|
||||
|
||||
|
||||
class Memento(object):
|
||||
"""
|
||||
Memento class as part of the Memento design pattern.
|
||||
|
||||
- External Usage documentation: U{https://github.com/tylerlaberge/PyPatterns/wiki/Behavioral-Pattern-Usage}
|
||||
- External Memento Pattern documentation: U{https://en.wikipedia.org/wiki/Memento_pattern}
|
||||
"""
|
||||
def __init__(self, state):
|
||||
"""
|
||||
Initialize a new Memento instance.
|
||||
|
||||
@param state: The state to save in this Memento.
|
||||
@type state: dict
|
||||
"""
|
||||
self.__state = state
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
return self.__state
|
||||
|
||||
|
||||
class Originator(object):
|
||||
"""
|
||||
Originator base class as part of the Memento design pattern.
|
||||
|
||||
- External Usage documentation: U{https://github.com/tylerlaberge/PyPatterns/wiki/Behavioral-Pattern-Usage}
|
||||
- External Mediator Pattern documentation: U{https://en.wikipedia.org/wiki/Memento_pattern}
|
||||
"""
|
||||
def commit(self):
|
||||
"""
|
||||
Commit this objects state to a memento.
|
||||
|
||||
@return: A memento instance with this objects state.
|
||||
"""
|
||||
return Memento(deepcopy(self.__dict__))
|
||||
|
||||
def rollback(self, memento):
|
||||
"""
|
||||
Rollback this objects state to a previous state.
|
||||
|
||||
@param memento: The memento object holding the state to rollback to.
|
||||
@type memento: Memento
|
||||
"""
|
||||
self.__dict__ = memento.state
|
||||
75
pypattyrn/behavioral/null.py
Normal file
75
pypattyrn/behavioral/null.py
Normal file
@@ -0,0 +1,75 @@
|
||||
class Null(object):
|
||||
"""
|
||||
A Null object class as part of the Null object design pattern.
|
||||
|
||||
- External Usage documentation: U{https://github.com/tylerlaberge/PyPatterns/wiki/Behavioral-Pattern-Usage}
|
||||
- External Null Object Pattern documentation: U{https://en.wikipedia.org/wiki/Null_Object_pattern}
|
||||
"""
|
||||
def __init__(self, *args, **kwargs):
|
||||
"""
|
||||
Do nothing.
|
||||
"""
|
||||
pass
|
||||
|
||||
def __call__(self, *args, **kwargs):
|
||||
"""
|
||||
Do nothing.
|
||||
|
||||
@return: This object instance.
|
||||
@rtype: Null
|
||||
"""
|
||||
return self
|
||||
|
||||
def __getattr__(self, name):
|
||||
"""
|
||||
Do nothing.
|
||||
|
||||
@return: This object instance.
|
||||
@rtype: Null
|
||||
"""
|
||||
return self
|
||||
|
||||
def __setattr__(self, name, value):
|
||||
"""
|
||||
Do nothing.
|
||||
|
||||
@return: This object instance.
|
||||
@rtype: Null
|
||||
"""
|
||||
return self
|
||||
|
||||
def __delattr__(self, name):
|
||||
"""
|
||||
Do nothing.
|
||||
|
||||
@return: This object instance.
|
||||
@rtype: Null
|
||||
"""
|
||||
return self
|
||||
|
||||
def __repr__(self):
|
||||
"""
|
||||
Null object string representation is the empty string.
|
||||
|
||||
@return: An empty string.
|
||||
@rtype: String
|
||||
"""
|
||||
return ''
|
||||
|
||||
def __str__(self):
|
||||
"""
|
||||
Null object string representation is the empty string.
|
||||
|
||||
@return: An empty string.
|
||||
@rtype: String
|
||||
"""
|
||||
return ''
|
||||
|
||||
def __bool__(self):
|
||||
"""
|
||||
Null object evaluates to False.
|
||||
|
||||
@return: False.
|
||||
@rtype: Boolean
|
||||
"""
|
||||
return False
|
||||
60
pypattyrn/behavioral/observer.py
Normal file
60
pypattyrn/behavioral/observer.py
Normal file
@@ -0,0 +1,60 @@
|
||||
from abc import ABCMeta, abstractmethod
|
||||
|
||||
|
||||
class Observer(object, metaclass=ABCMeta):
|
||||
"""
|
||||
Abstract Observer class as part of the Observer design pattern.
|
||||
|
||||
- External Usage documentation: U{https://github.com/tylerlaberge/PyPatterns/wiki/Behavioral-Pattern-Usage}
|
||||
- External Observer Pattern documentation: U{https://en.wikipedia.org/wiki/Observer_pattern}
|
||||
"""
|
||||
@abstractmethod
|
||||
def update(self, **state):
|
||||
"""
|
||||
Abstract method that is called when an Observable's state changes.
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class Observable(object):
|
||||
"""
|
||||
Base Observable class as part of the Observer design pattern.
|
||||
|
||||
- External Usage documentation: U{https://github.com/tylerlaberge/PyPatterns/wiki/Behavioral-Pattern-Usage}
|
||||
- External Observer Pattern documentation: U{https://en.wikipedia.org/wiki/Observer_pattern}
|
||||
"""
|
||||
def __init__(self):
|
||||
"""
|
||||
Initialize a new Observable instance.
|
||||
"""
|
||||
self._observers = set()
|
||||
|
||||
def attach(self, observer):
|
||||
"""
|
||||
Attach an observer to this Observable.
|
||||
|
||||
@param observer: The Observer to attach.
|
||||
@type observer: Observer
|
||||
"""
|
||||
self._observers.add(observer)
|
||||
|
||||
def detach(self, observer):
|
||||
"""
|
||||
Detach an observer from this Observable.
|
||||
|
||||
@param observer: The Observer to detach.
|
||||
@type observer: Observer
|
||||
"""
|
||||
try:
|
||||
self._observers.remove(observer)
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
def notify(self):
|
||||
"""
|
||||
Notify all attached Observers of the state of this Observable.
|
||||
"""
|
||||
for observer in self._observers:
|
||||
state = {k: v for k, v in self.__dict__.items() if not k.startswith('__') and not k.startswith('_')}
|
||||
observer.update(**state)
|
||||
|
||||
58
pypattyrn/behavioral/visitor.py
Normal file
58
pypattyrn/behavioral/visitor.py
Normal file
@@ -0,0 +1,58 @@
|
||||
from abc import ABCMeta, abstractmethod
|
||||
|
||||
|
||||
class Visitor(metaclass=ABCMeta):
|
||||
"""
|
||||
Abstract Visitor class as part of the Visitor Design Pattern.
|
||||
|
||||
- External Usage documentation: U{https://github.com/tylerlaberge/PyPatterns/wiki/Behavioral-Pattern-Usage}
|
||||
- External Visitor Design Pattern documentation: U{https://en.wikipedia.org/wiki/Visitor_pattern}
|
||||
"""
|
||||
def visit(self, node, *args, **kwargs):
|
||||
"""
|
||||
Visit the visitor with some object.
|
||||
|
||||
@param node: An object to call a visitor method with.
|
||||
@param args: Arguments to go with the visitor method call.
|
||||
@param kwargs: Keyword arguments to go with the visitor method call.
|
||||
@return: The return value of the method that was called for visiting object.
|
||||
"""
|
||||
method = None
|
||||
for cls in node.__class__.__mro__:
|
||||
method_name = 'visit_'+cls.__name__.lower()
|
||||
method = getattr(self, method_name, None)
|
||||
if method:
|
||||
break
|
||||
|
||||
if not method:
|
||||
method = self.generic_visit
|
||||
return method(node, *args, **kwargs)
|
||||
|
||||
@abstractmethod
|
||||
def generic_visit(self, node, *args, **kwargs):
|
||||
"""
|
||||
The method to call if no methods were found for a visiting object.
|
||||
|
||||
@param node: An object to call a visitor method with.
|
||||
@param args: Arguments to go with the visitor method call.
|
||||
@param kwargs: Keyword arguments to go with the visitor method call.
|
||||
"""
|
||||
|
||||
|
||||
class Visitee(object):
|
||||
"""
|
||||
A base class for objects that wish to be able to be visited by a Visitor class.
|
||||
|
||||
- External Usage documentation: U{https://github.com/tylerlaberge/PyPatterns/wiki/Behavioral-Pattern-Usage}
|
||||
- External Visitor Design Pattern documentation: U{https://en.wikipedia.org/wiki/Visitor_pattern}
|
||||
"""
|
||||
def accept(self, visitor, *args, **kwargs):
|
||||
"""
|
||||
Have a visitor visit this class instance.
|
||||
|
||||
@param visitor: The visitor to visit.
|
||||
@type visitor: Visitor
|
||||
@param args: Any args to send with the visit.
|
||||
@param kwargs: Any kwargs to send with the visit.
|
||||
"""
|
||||
return visitor.visit(self, *args, **kwargs)
|
||||
Reference in New Issue
Block a user