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
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ script:
- PYTHONPATH=. nosetests -s -v --with-doctest --with-cov --cover-package . --logging-level=INFO -v .
# Actually run all the scripts, contributing to coverage
- PYTHONPATH=. ./run_all.sh
# for now failure in flaking is ignored
- flake8 *py || echo "PEP8 the code"
- flake8 *py

after_success:
- coveralls
Expand Down
13 changes: 5 additions & 8 deletions behavioral/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ def __init__(self, param):
# dictionary that will be used to determine which static method is
# 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}
self._static_method_choices = {'param_value_1': self._static_method_1, '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 @@ -68,8 +67,7 @@ def _instance_method_1(self):
def _instance_method_2(self):
print("Value {}".format(self.x2))

_instance_method_choices = {'param_value_1': _instance_method_1,
'param_value_2': _instance_method_2}
_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 @@ -104,8 +102,7 @@ def _class_method_1(cls):
def _class_method_2(cls):
print("Value {}".format(cls.x2))

_class_method_choices = {'param_value_1': _class_method_1,
'param_value_2': _class_method_2}
_class_method_choices = {'param_value_1': _class_method_1, 'param_value_2': _class_method_2}

def main_method(self):
"""will execute either _class_method_1 or _class_method_2
Expand Down Expand Up @@ -137,8 +134,7 @@ def _static_method_1():
def _static_method_2():
print("executed method 2!")

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

def main_method(self):
"""will execute either _static_method_1 or _static_method_2
Expand Down Expand Up @@ -168,6 +164,7 @@ def main():
test = CatalogStatic('param_value_1')
test.main_method()


if __name__ == "__main__":

main()
Expand Down
12 changes: 3 additions & 9 deletions behavioral/chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,41 +44,35 @@ def _handle(self, request):


class ConcreteHandler1(Handler):

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(object):

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

def delegate(self, requests):
for request in requests:
Expand All @@ -90,6 +84,7 @@ def start(*args, **kwargs):
cr = func(*args, **kwargs)
next(cr)
return cr

return start


Expand Down Expand Up @@ -131,7 +126,6 @@ def default_coroutine():


class ClientCoroutine:

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

Expand All @@ -141,12 +135,12 @@ def delegate(self, requests):


def timeit(func):

def count(*args, **kwargs):
start = time.time()
res = func(*args, **kwargs)
count._time = time.time() - start
return res

return count


Expand Down
3 changes: 1 addition & 2 deletions behavioral/chaining_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@


class Person(object):

def __init__(self, name, action):
self.name = name
self.action = action
Expand All @@ -16,7 +15,6 @@ def do_action(self):


class Action(object):

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

Expand All @@ -27,6 +25,7 @@ def amount(self, val):
def stop(self):
print('then stop')


if __name__ == '__main__':

move = Action('move')
Expand Down
8 changes: 4 additions & 4 deletions behavioral/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@


class MoveFileCommand(object):

def __init__(self, src, dest):
self.src = src
self.dest = dest
Expand All @@ -36,9 +35,9 @@ def main():
command_stack.append(MoveFileCommand('bar.txt', 'baz.txt'))

# verify that none of the target files exist
assert(not lexists("foo.txt"))
assert(not lexists("bar.txt"))
assert(not lexists("baz.txt"))
assert not lexists("foo.txt")
assert not lexists("bar.txt")
assert not lexists("baz.txt")
try:
with open("foo.txt", "w"): # Creating the file
pass
Expand All @@ -53,6 +52,7 @@ def main():
finally:
os.unlink("foo.txt")


if __name__ == "__main__":
main()

Expand Down
1 change: 1 addition & 0 deletions behavioral/iterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def count_to(count):
for number in numbers[:count]:
yield number


# Test the generator
count_to_two = lambda: count_to(2)
count_to_five = lambda: count_to(5)
Expand Down
4 changes: 0 additions & 4 deletions behavioral/mediator.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@


class TC:

def __init__(self):
self._tm = None
self._bProblem = 0
Expand Down Expand Up @@ -46,7 +45,6 @@ def setProblem(self, value):


class Reporter:

def __init__(self):
self._tm = None

Expand All @@ -63,7 +61,6 @@ def setTM(self, tm):


class DB:

def __init__(self):
self._tm = None

Expand All @@ -83,7 +80,6 @@ def setTM(self, tm):


class TestManager:

def __init__(self):
self._reporter = None
self._db = None
Expand Down
2 changes: 1 addition & 1 deletion behavioral/memento.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Transaction(object):

This is, in fact, just syntactic sugar around a memento closure.
"""

deep = False
states = []

Expand Down Expand Up @@ -65,7 +66,6 @@ def transaction(*args, **kwargs):


class NumObj(object):

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

Expand Down
10 changes: 2 additions & 8 deletions behavioral/observer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@


class Subject(object):

def __init__(self):
self._observers = []

Expand All @@ -34,7 +33,6 @@ def notify(self, modifier=None):

# Example usage
class Data(Subject):

def __init__(self, name=''):
Subject.__init__(self)
self.name = name
Expand All @@ -51,17 +49,13 @@ def data(self, value):


class HexViewer:

def update(self, subject):
print(u'HexViewer: Subject %s has data 0x%x' %
(subject.name, subject.data))
print(u'HexViewer: Subject %s has data 0x%x' % (subject.name, subject.data))


class DecimalViewer:

def update(self, subject):
print(u'DecimalViewer: Subject %s has data %d' %
(subject.name, subject.data))
print(u'DecimalViewer: Subject %s has data %d' % (subject.name, subject.data))


# Example usage...
Expand Down
3 changes: 0 additions & 3 deletions behavioral/publish_subscribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@


class Provider:

def __init__(self):
self.msg_queue = []
self.subscribers = {}
Expand All @@ -30,7 +29,6 @@ def update(self):


class Publisher:

def __init__(self, msg_center):
self.provider = msg_center

Expand All @@ -39,7 +37,6 @@ def publish(self, msg):


class Subscriber:

def __init__(self, name, msg_center):
self.name = name
self.provider = msg_center
Expand Down
2 changes: 1 addition & 1 deletion behavioral/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ class and the associated value, the class itself.
"""
pass


if __name__ == "__main__":
print("Before subclassing: ")
for k in RegistryHolder.REGISTRY:
print(k)

class ClassRegistree(BaseRegisteredClass):

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

Expand Down
14 changes: 3 additions & 11 deletions behavioral/specification.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@


class Specification(object):

def and_specification(self, candidate):
raise NotImplementedError()

Expand All @@ -28,7 +27,6 @@ def is_satisfied_by(self, candidate):


class CompositeSpecification(Specification):

@abstractmethod
def is_satisfied_by(self, candidate):
pass
Expand All @@ -52,8 +50,7 @@ def __init__(self, one, other):
self._other = other

def is_satisfied_by(self, candidate):
return bool(self._one.is_satisfied_by(candidate) and
self._other.is_satisfied_by(candidate))
return bool(self._one.is_satisfied_by(candidate) and self._other.is_satisfied_by(candidate))


class OrSpecification(CompositeSpecification):
Expand All @@ -65,8 +62,7 @@ def __init__(self, one, other):
self._other = other

def is_satisfied_by(self, candidate):
return bool(self._one.is_satisfied_by(candidate) or
self._other.is_satisfied_by(candidate))
return bool(self._one.is_satisfied_by(candidate) or self._other.is_satisfied_by(candidate))


class NotSpecification(CompositeSpecification):
Expand All @@ -80,19 +76,16 @@ def is_satisfied_by(self, candidate):


class User(object):

def __init__(self, super_user=False):
self.super_user = super_user


class UserSpecification(CompositeSpecification):

def is_satisfied_by(self, candidate):
return isinstance(candidate, User)


class SuperUserSpecification(CompositeSpecification):

def is_satisfied_by(self, candidate):
return getattr(candidate, 'super_user', False)

Expand All @@ -103,8 +96,7 @@ def is_satisfied_by(self, candidate):
ivan = User(super_user=True)
vasiliy = 'not User instance'

root_specification = UserSpecification().\
and_specification(SuperUserSpecification())
root_specification = UserSpecification().and_specification(SuperUserSpecification())

print(root_specification.is_satisfied_by(andrey))
print(root_specification.is_satisfied_by(ivan))
Expand Down
Loading