From 9aa2a5bb782defb848ac3fb5a53ab2c435bbde81 Mon Sep 17 00:00:00 2001 From: Grygorii Iermolenko Date: Fri, 25 Jan 2019 16:07:20 +0200 Subject: [PATCH 1/2] Fix flake8 check and related warnings --- .travis.yml | 2 +- append_output.sh | 2 +- patterns/behavioral/command.py | 3 +- patterns/behavioral/iterator.py | 8 +- patterns/behavioral/mediator.py.bkp | 165 +++++++++++++++++++++ patterns/behavioral/memento.py | 7 +- patterns/behavioral/specification.py | 2 +- patterns/behavioral/state.py | 2 +- patterns/behavioral/template.py | 2 +- patterns/fundamental/delegation_pattern.py | 2 +- patterns/structural/flyweight.py | 4 +- setup.cfg | 5 + setup.py | 3 +- 13 files changed, 187 insertions(+), 20 deletions(-) create mode 100644 patterns/behavioral/mediator.py.bkp diff --git a/.travis.yml b/.travis.yml index ed782b1ea..bff54dd22 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,7 +20,7 @@ script: - pytest -s -vv --cov=. --log-level=INFO tests/ # Actually run all the scripts, contributing to coverage - PYTHONPATH=. ./run_all.sh - - flake8 *py + - flake8 patterns/ after_success: - codecov diff --git a/append_output.sh b/append_output.sh index 3e5ea610f..8e576d0f4 100755 --- a/append_output.sh +++ b/append_output.sh @@ -16,4 +16,4 @@ echo "$src" > $1 echo -e "\n" >> $1 echo "$output_marker" >> $1 echo "$output" >> $1 -echo '"""' >> $1 +echo '""" # noqa' >> $1 diff --git a/patterns/behavioral/command.py b/patterns/behavioral/command.py index 17765e52a..74fcfc9c6 100644 --- a/patterns/behavioral/command.py +++ b/patterns/behavioral/command.py @@ -6,7 +6,8 @@ Encapsulates all information needed to perform an action or trigger an event. *Examples in Python ecosystem: -Django HttpRequest (without `execute` method): https://docs.djangoproject.com/en/2.1/ref/request-response/#httprequest-objects +Django HttpRequest (without `execute` method): + https://docs.djangoproject.com/en/2.1/ref/request-response/#httprequest-objects """ from __future__ import print_function diff --git a/patterns/behavioral/iterator.py b/patterns/behavioral/iterator.py index 49f8e5697..167979ec7 100644 --- a/patterns/behavioral/iterator.py +++ b/patterns/behavioral/iterator.py @@ -29,13 +29,9 @@ def main(): for number in count_to_two(): print(number, end=' ') - print() - - print('Counting to five...') + print('\nCounting to five...') for number in count_to_five(): print(number, end=' ') - - print() if __name__ == "__main__": @@ -47,4 +43,4 @@ def main(): one two Counting to five... one two three four five -""" +""" # noqa diff --git a/patterns/behavioral/mediator.py.bkp b/patterns/behavioral/mediator.py.bkp new file mode 100644 index 000000000..1ec86cc6e --- /dev/null +++ b/patterns/behavioral/mediator.py.bkp @@ -0,0 +1,165 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +http://web.archive.org/web/20120309135549/http://dpip.testingperspective.com/?p=28 + +*TL;DR80 +Encapsulates how a set of objects interact. +""" + +import random +import time + + +class TC: + """TestCategory + responsible for running the tests with the help of setup(), execute() and tearDown() methods + """ + + def __init__(self): + self._tm = None + self._bProblem = 0 + + def setup(self): + print("Setting up the Test") + time.sleep(0.1) + self._tm.prepareReporting() + + def execute(self): + if not self._bProblem: + print("Executing the test") + time.sleep(0.1) + else: + print("Problem in setup. Test not executed.") + + def tearDown(self): + if not self._bProblem: + print("Tearing down") + time.sleep(0.1) + self._tm.publishReport() + else: + print("Test not executed. No tear down required.") + + def setTM(self, tm): + self._tm = tm + + def setProblem(self, value): + self._bProblem = value + + +class Reporter: + """ + - calls its prepare() method while TestCategory starts getting executed + - calls its report() method when TestCategory finishes its execution. + """ + def __init__(self): + self._tm = None + + def prepare(self): + print("Reporter Class is preparing to report the results") + time.sleep(0.1) + + def report(self): + print("Reporting the results of Test") + time.sleep(0.1) + + def setTM(self, tm): + self._tm = tm + + +class DB: + """ + stores the execution status of the test category by first + calling the insert() method while the test category is in setup(), + and then calls the update() method once the test category has finished execution. + In this way, at any given point of time, the test execution status is available for framework user to query from the database + """ + def __init__(self): + self._tm = None + + def insert(self): + print("Inserting the execution begin status in the Database") + time.sleep(0.1) + # Following code is to simulate a communication from DB to TC + if random.randrange(1, 4) == 3: + return -1 + + def update(self): + print("Updating the test results in Database") + time.sleep(0.1) + + def setTM(self, tm): + self._tm = tm + + +class TestManager: + """ + Mediator between Class TC, Class Reporter and Class DB, the Colleagues in the system + coordinates for + test category execution (Class TC) + and fetching the reports (Class Reporter) + and getting the test execution status in database (Class DB) with the help of prepareReporting() and publishReport() methods + """ + def __init__(self): + self._reporter = None + self._db = None + self._tc = None + + def prepareReporting(self): + rvalue = self._db.insert() + if rvalue == -1: + self._tc.setProblem(1) + self._reporter.prepare() + + def setReporter(self, reporter): + self._reporter = reporter + + def setDB(self, db): + self._db = db + + def publishReport(self): + self._db.update() + self._reporter.report() + + def setTC(self, tc): + self._tc = tc + + +if __name__ == '__main__': + reporter = Reporter() + db = DB() + tm = TestManager() + tm.setReporter(reporter) + tm.setDB(db) + reporter.setTM(tm) + db.setTM(tm) + # For simplification we are looping on the same test. + # Practically, it could be about various unique test classes and their + # objects + for i in range(3): + tc = TC() + tc.setTM(tm) + tm.setTC(tc) + tc.setup() + tc.execute() + tc.tearDown() + +### OUTPUT ### +# Setting up the Test +# Inserting the execution begin status in the Database +# Executing the test +# Tearing down +# Updating the test results in Database +# Reporting the results of Test +# Setting up the Test +# Inserting the execution begin status in the Database +# Reporter Class is preparing to report the results +# Problem in setup. Test not executed. +# Test not executed. No tear down required. +# Setting up the Test +# Inserting the execution begin status in the Database +# Executing the test +# Tearing down +# Updating the test results in Database +# Reporting the results of Test diff --git a/patterns/behavioral/memento.py b/patterns/behavioral/memento.py index 059dce421..7feade2bc 100644 --- a/patterns/behavioral/memento.py +++ b/patterns/behavioral/memento.py @@ -98,7 +98,7 @@ def main(): print(num_obj) num_obj.value += 'x' # will fail print(num_obj) - except Exception as e: + except Exception: a_transaction.rollback() print('-- rolled back') print(num_obj) @@ -106,7 +106,7 @@ def main(): print('-- now doing stuff ...') try: num_obj.do_stuff() - except Exception as e: + except Exception: print('-> doing stuff failed!') import sys import traceback @@ -114,9 +114,10 @@ def main(): traceback.print_exc(file=sys.stdout) print(num_obj) + if __name__ == '__main__': main() - + OUTPUT = """ diff --git a/patterns/behavioral/specification.py b/patterns/behavioral/specification.py index 6198af1f4..5c77370e2 100644 --- a/patterns/behavioral/specification.py +++ b/patterns/behavioral/specification.py @@ -102,7 +102,7 @@ def main(): print(root_specification.is_satisfied_by(ivan)) print(root_specification.is_satisfied_by(vasiliy)) - + if __name__ == '__main__': main() diff --git a/patterns/behavioral/state.py b/patterns/behavioral/state.py index 0d567093d..46508c3a9 100644 --- a/patterns/behavioral/state.py +++ b/patterns/behavioral/state.py @@ -76,7 +76,7 @@ def main(): for action in actions: action() - + if __name__ == '__main__': main() diff --git a/patterns/behavioral/template.py b/patterns/behavioral/template.py index 61c581ce7..7ae977963 100644 --- a/patterns/behavioral/template.py +++ b/patterns/behavioral/template.py @@ -5,7 +5,7 @@ An example of the Template pattern in Python *TL;DR80 -Defines the skeleton of a base algorithm, deferring definition of exact +Defines the skeleton of a base algorithm, deferring definition of exact steps to subclasses. *Examples in Python ecosystem: diff --git a/patterns/fundamental/delegation_pattern.py b/patterns/fundamental/delegation_pattern.py index 3e26be9d4..ad41ac1ea 100644 --- a/patterns/fundamental/delegation_pattern.py +++ b/patterns/fundamental/delegation_pattern.py @@ -32,7 +32,7 @@ def __init__(self, delegate): def __getattr__(self, name): attr = getattr(self.delegate, name) - + if not callable(attr): return attr diff --git a/patterns/structural/flyweight.py b/patterns/structural/flyweight.py index b2a0bde08..c8fba6a23 100644 --- a/patterns/structural/flyweight.py +++ b/patterns/structural/flyweight.py @@ -119,8 +119,8 @@ def __init__(self, *args, **kwargs): cm2 = Card2('10', 'h', a=1) cm3 = Card2('10', 'h', a=2) - assert (cm1 == cm2) and ( cm1 != cm3) - assert (cm1 is cm2) and ( cm1 is not cm3) + assert (cm1 == cm2) and (cm1 != cm3) + assert (cm1 is cm2) and (cm1 is not cm3) assert len(instances_pool) == 2 del cm1 diff --git a/setup.cfg b/setup.cfg index 51c75321c..536a52d79 100644 --- a/setup.cfg +++ b/setup.cfg @@ -2,3 +2,8 @@ max-line-length = 120 ignore = E266 E731 exclude = .venv* + +[tool:pytest] +filterwarnings = + ; ignore TestRunner class from facade example + ignore:.*test class 'TestRunner'.*:Warning diff --git a/setup.py b/setup.py index 3b0136779..07c495dc5 100644 --- a/setup.py +++ b/setup.py @@ -7,8 +7,7 @@ "Programming Language :: Python :: 2", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3", - "Programming Language :: Python :: 3.4", - "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", + "Programming Language :: Python :: 3.7", ], ) From 07310562485681b663b19d2e981ed8e42e9ae2e1 Mon Sep 17 00:00:00 2001 From: Grygorii Iermolenko Date: Fri, 25 Jan 2019 16:15:28 +0200 Subject: [PATCH 2/2] remove file added by mistake --- patterns/behavioral/mediator.py.bkp | 165 ---------------------------- 1 file changed, 165 deletions(-) delete mode 100644 patterns/behavioral/mediator.py.bkp diff --git a/patterns/behavioral/mediator.py.bkp b/patterns/behavioral/mediator.py.bkp deleted file mode 100644 index 1ec86cc6e..000000000 --- a/patterns/behavioral/mediator.py.bkp +++ /dev/null @@ -1,165 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- - -""" -http://web.archive.org/web/20120309135549/http://dpip.testingperspective.com/?p=28 - -*TL;DR80 -Encapsulates how a set of objects interact. -""" - -import random -import time - - -class TC: - """TestCategory - responsible for running the tests with the help of setup(), execute() and tearDown() methods - """ - - def __init__(self): - self._tm = None - self._bProblem = 0 - - def setup(self): - print("Setting up the Test") - time.sleep(0.1) - self._tm.prepareReporting() - - def execute(self): - if not self._bProblem: - print("Executing the test") - time.sleep(0.1) - else: - print("Problem in setup. Test not executed.") - - def tearDown(self): - if not self._bProblem: - print("Tearing down") - time.sleep(0.1) - self._tm.publishReport() - else: - print("Test not executed. No tear down required.") - - def setTM(self, tm): - self._tm = tm - - def setProblem(self, value): - self._bProblem = value - - -class Reporter: - """ - - calls its prepare() method while TestCategory starts getting executed - - calls its report() method when TestCategory finishes its execution. - """ - def __init__(self): - self._tm = None - - def prepare(self): - print("Reporter Class is preparing to report the results") - time.sleep(0.1) - - def report(self): - print("Reporting the results of Test") - time.sleep(0.1) - - def setTM(self, tm): - self._tm = tm - - -class DB: - """ - stores the execution status of the test category by first - calling the insert() method while the test category is in setup(), - and then calls the update() method once the test category has finished execution. - In this way, at any given point of time, the test execution status is available for framework user to query from the database - """ - def __init__(self): - self._tm = None - - def insert(self): - print("Inserting the execution begin status in the Database") - time.sleep(0.1) - # Following code is to simulate a communication from DB to TC - if random.randrange(1, 4) == 3: - return -1 - - def update(self): - print("Updating the test results in Database") - time.sleep(0.1) - - def setTM(self, tm): - self._tm = tm - - -class TestManager: - """ - Mediator between Class TC, Class Reporter and Class DB, the Colleagues in the system - coordinates for - test category execution (Class TC) - and fetching the reports (Class Reporter) - and getting the test execution status in database (Class DB) with the help of prepareReporting() and publishReport() methods - """ - def __init__(self): - self._reporter = None - self._db = None - self._tc = None - - def prepareReporting(self): - rvalue = self._db.insert() - if rvalue == -1: - self._tc.setProblem(1) - self._reporter.prepare() - - def setReporter(self, reporter): - self._reporter = reporter - - def setDB(self, db): - self._db = db - - def publishReport(self): - self._db.update() - self._reporter.report() - - def setTC(self, tc): - self._tc = tc - - -if __name__ == '__main__': - reporter = Reporter() - db = DB() - tm = TestManager() - tm.setReporter(reporter) - tm.setDB(db) - reporter.setTM(tm) - db.setTM(tm) - # For simplification we are looping on the same test. - # Practically, it could be about various unique test classes and their - # objects - for i in range(3): - tc = TC() - tc.setTM(tm) - tm.setTC(tc) - tc.setup() - tc.execute() - tc.tearDown() - -### OUTPUT ### -# Setting up the Test -# Inserting the execution begin status in the Database -# Executing the test -# Tearing down -# Updating the test results in Database -# Reporting the results of Test -# Setting up the Test -# Inserting the execution begin status in the Database -# Reporter Class is preparing to report the results -# Problem in setup. Test not executed. -# Test not executed. No tear down required. -# Setting up the Test -# Inserting the execution begin status in the Database -# Executing the test -# Tearing down -# Updating the test results in Database -# Reporting the results of Test