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
8 changes: 4 additions & 4 deletions behavioral/catalog.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
A class that uses different static function depending of a parameter passed in
init. Note the use of a single dictionary instead of multiple conditions
Expand All @@ -22,7 +21,7 @@ def __init__(self, param):
# to be executed but that will be also used to store possible param
# value
self._static_method_choices = {'param_value_1': self._static_method_1,
'param_value_2': self._static_method_2}
'param_value_2': self._static_method_2}

# simple test to validate param value
if param in self._static_method_choices.keys():
Expand Down Expand Up @@ -72,7 +71,6 @@ def _instance_method_2(self):
_instance_method_choices = {'param_value_1': _instance_method_1,
'param_value_2': _instance_method_2}


def main_method(self):
"""
will execute either _instance_method_1 or _instance_method_2
Expand Down Expand Up @@ -123,6 +121,7 @@ class CatalogStatic:
catalog of multiple static methods that are executed depending on an init
parameter
"""

def __init__(self, param):
# simple test to validate param value
if param in self._static_method_choices:
Expand All @@ -139,7 +138,7 @@ def _static_method_2():
print("executed method 2!")

_static_method_choices = {'param_value_1': _static_method_1,
'param_value_2': _static_method_2}
'param_value_2': _static_method_2}

def main_method(self):
"""
Expand All @@ -148,6 +147,7 @@ def main_method(self):
"""
self._static_method_choices[self.param].__get__(None, self.__class__)()


def main():
"""
>>> c = Catalog('param_value_1').main_method()
Expand Down
40 changes: 29 additions & 11 deletions behavioral/chain.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""http://www.dabeaz.com/coroutines/"""

import time
import os
import sys
from contextlib import contextmanager


class Handler:

def __init__(self, successor=None):
self._successor = successor

def handle(self, request):
res = self._handle(request)
if not res:
self._successor.handle(request)

def _handle(self, request):
raise NotImplementedError('Must provide implementation in subclass.')

Expand All @@ -25,30 +28,37 @@ def _handle(self, request):
if 0 < request <= 10:
print('request {} handled in handler 1'.format(request))
return True



class ConcreteHandler2(Handler):

def _handle(self, request):
if 10 < request <= 20:
print('request {} handled in handler 2'.format(request))
return True



class ConcreteHandler3(Handler):

def _handle(self, request):
if 20 < request <= 30:
print('request {} handled in handler 3'.format(request))
return True


class DefaultHandler(Handler):

def _handle(self, request):
print('end of chain, no handler for {}'.format(request))
return True


class Client:

def __init__(self):
self.handler = ConcreteHandler1(ConcreteHandler3(ConcreteHandler2(DefaultHandler())))
self.handler = ConcreteHandler1(
ConcreteHandler3(ConcreteHandler2(DefaultHandler())))

def delegate(self, requests):
for request in requests:
self.handler.handle(request)
Expand All @@ -61,6 +71,7 @@ def start(*args, **kwargs):
return cr
return start


@coroutine
def coroutine1(target):
while True:
Expand All @@ -70,6 +81,7 @@ def coroutine1(target):
else:
target.send(request)


@coroutine
def coroutine2(target):
while True:
Expand All @@ -79,6 +91,7 @@ def coroutine2(target):
else:
target.send(request)


@coroutine
def coroutine3(target):
while True:
Expand All @@ -88,20 +101,24 @@ def coroutine3(target):
else:
target.send(request)


@coroutine
def default_coroutine():
while True:
request = yield
print('end of chain, no coroutine for {}'.format(request))
while True:
request = yield
print('end of chain, no coroutine for {}'.format(request))


class ClientCoroutine:

def __init__(self):
self.target = coroutine1(coroutine3(coroutine2(default_coroutine())))

def delegate(self, requests):
for request in requests:
self.target.send(request)


def timeit(func):

def count(*args, **kwargs):
Expand All @@ -111,6 +128,7 @@ def count(*args, **kwargs):
return res
return count


@contextmanager
def suppress_stdout():
try:
Expand All @@ -126,7 +144,7 @@ def suppress_stdout():
requests = [2, 5, 14, 22, 18, 3, 35, 27, 20]

client1.delegate(requests)
print('-'*30)
print('-' * 30)
client2.delegate(requests)

requests *= 10000
Expand Down
2 changes: 2 additions & 0 deletions behavioral/chaining_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from __future__ import print_function


class Person(object):

def __init__(self, name, action):
Expand All @@ -13,6 +14,7 @@ def do_action(self):
print(self.name, self.action.name, end=' ')
return self.action


class Action(object):

def __init__(self, name):
Expand Down
1 change: 1 addition & 0 deletions behavioral/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
from os.path import lexists


class MoveFileCommand(object):

def __init__(self, src, dest):
Expand Down
8 changes: 4 additions & 4 deletions behavioral/iterator.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/
Implementation of the iterator pattern with a generator"""
"""
http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/
Implementation of the iterator pattern with a generator
"""

from __future__ import print_function

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

"""http://dpip.testingperspective.com/?p=28"""

import random
Expand Down
2 changes: 1 addition & 1 deletion behavioral/memento.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""http://code.activestate.com/recipes/413838-memento-closure/"""

from copy import copy, deepcopy
Expand Down Expand Up @@ -57,6 +56,7 @@ def transaction(*args, **kwargs):


class NumObj(object):

def __init__(self, value):
self.value = value

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

"""http://code.activestate.com/recipes/131499-observer-pattern/"""


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

"""
Reference: http://www.slideshare.net/ishraqabd/publish-subscribe-model-overview-13368808
Reference:
http://www.slideshare.net/ishraqabd/publish-subscribe-model-overview-13368808
Author: https://github.com/HanWenfang
"""

Expand Down
5 changes: 2 additions & 3 deletions behavioral/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,14 @@ class ClassRegistree(BaseRegisteredClass):

def __init__(self, *args, **kwargs):
pass


print("After subclassing: ")
for k in RegistryHolder.REGISTRY:
print(k)

### OUTPUT ###
# Before subclassing:
# Before subclassing:
# BaseRegisteredClass
# After subclassing:
# After subclassing:
# BaseRegisteredClass
# ClassRegistree
2 changes: 1 addition & 1 deletion behavioral/specification.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
@author: Gordeev Andrey <gordeev.and.and@gmail.com>
Expand Down Expand Up @@ -28,6 +27,7 @@ def is_satisfied_by(self, candidate):


class CompositeSpecification(Specification):

@abstractmethod
def is_satisfied_by(self, candidate):
pass
Expand Down
1 change: 1 addition & 0 deletions behavioral/state.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Implementation of the state pattern"""

# http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/
Expand Down
5 changes: 3 additions & 2 deletions behavioral/strategy.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env python
# http://stackoverflow.com/questions/963965/how-is-this-strategy-pattern
# -written-in-python-the-sample-in-wikipedia
# -*- coding: utf-8 -*-
"""
http://stackoverflow.com/questions/963965/how-is-this-strategy-pattern
-written-in-python-the-sample-in-wikipedia
In most of other languages Strategy pattern is implemented via creating some
base strategy interface/abstract class and subclassing it with a number of
concrete strategies (as we can see at
Expand Down
2 changes: 2 additions & 0 deletions behavioral/template.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/
An example of the Template pattern in Python"""
Expand Down
6 changes: 5 additions & 1 deletion behavioral/visitor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
"""http://peter-hoffmann.com/2010/extrinsic-visitor-pattern-python-inheritance.html"""
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
http://peter-hoffmann.com/2010/extrinsic-visitor-pattern-python-inheritance.html
"""


class Node(object):
Expand Down
1 change: 1 addition & 0 deletions fundamental/delegation_pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Delegator(object):
>>> delegator.do_anything()
"""

def __init__(self, delegate):
self.delegate = delegate

Expand Down