From a40009447810050ee60b6800c41a262da6e18946 Mon Sep 17 00:00:00 2001 From: cdorsman Date: Wed, 23 Apr 2025 02:10:21 +0200 Subject: [PATCH 1/2] Added routing --- patterns/structural/mvc.py | 41 +++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/patterns/structural/mvc.py b/patterns/structural/mvc.py index 3f7dc315..b81e10be 100644 --- a/patterns/structural/mvc.py +++ b/patterns/structural/mvc.py @@ -4,6 +4,8 @@ """ from abc import ABC, abstractmethod +from inspect import signature +from sys import argv class Model(ABC): @@ -113,6 +115,23 @@ def show_item_information(self, item_name): self.view.show_item_information(item_type, item_name, item_info) +class Router: + def __init__(self): + self.routes = {} + + def register(self, path, controller, model, view): + model = model() + view = view() + self.routes[path] = controller(model, view) + + def resolve(self, path): + if self.routes.get(path): + controller = self.routes[path] + return controller + else: + return None + + def main(): """ >>> model = ProductModel() @@ -147,6 +166,26 @@ def main(): if __name__ == "__main__": - import doctest + router = Router() + router.register("products", Controller, ProductModel, ConsoleView) + controller = router.resolve(argv[1]) + + command = str(argv[2]) if len(argv) > 2 else None + args = ' '.join(map(str, argv[3:])) if len(argv) > 3 else None + + if hasattr(controller, command): + command = getattr(controller, command) + sig = signature(command) + + if len(sig.parameters) > 0: + if args: + command(args) + else: + print("Command requires arguments.") + else: + command() + else: + print(f"Command {command} not found in the controller.") + import doctest doctest.testmod() From 8f0a91c3d021599d78159b1a59f452c15119bdaf Mon Sep 17 00:00:00 2001 From: cdorsman Date: Wed, 23 Apr 2025 16:28:33 +0200 Subject: [PATCH 2/2] Cleaned up lint.sh --- .codespellignore | 14 ++++++++++++++ lint.sh | 20 +++++++++++--------- tox.ini | 4 ++-- 3 files changed, 27 insertions(+), 11 deletions(-) create mode 100644 .codespellignore diff --git a/.codespellignore b/.codespellignore new file mode 100644 index 00000000..d272a2e1 --- /dev/null +++ b/.codespellignore @@ -0,0 +1,14 @@ +__pycache__ +*.pyc +.idea +*.egg-info/ +.tox/ +env/ +venv/ +.env +.venv +.vscode/ +.python-version +.coverage +build/ +dist/ \ No newline at end of file diff --git a/lint.sh b/lint.sh index a3ce9719..5c418249 100755 --- a/lint.sh +++ b/lint.sh @@ -2,13 +2,15 @@ pip install --upgrade pip pip install black codespell flake8 isort mypy pytest pyupgrade tox -black --check . -codespell --quiet-level=2 # --ignore-words-list="" --skip="" -flake8 . --count --show-source --statistics -isort --profile black . -tox pip install -e . -mypy --ignore-missing-imports . || true -pytest . -pytest --doctest-modules . || true -shopt -s globstar && pyupgrade --py37-plus **/*.py \ No newline at end of file + +source_dir="./patterns" + +codespell --quiet-level=2 ./patterns # --ignore-words-list="" --skip="" +flake8 "${source_dir}" --count --show-source --statistics +isort --profile black "${source_dir}" +tox +mypy --ignore-missing-imports "${source_dir}" || true +pytest "${source_dir}" +pytest --doctest-modules "${source_dir}" || true +shopt -s globstar && pyupgrade --py37-plus ${source_dir}/*.py diff --git a/tox.ini b/tox.ini index 1eca32ab..3ce6e132 100644 --- a/tox.ini +++ b/tox.ini @@ -9,10 +9,10 @@ setenv = deps = -r requirements-dev.txt commands = - flake8 . --exclude="./.*, venv" + flake8 --exclude="venv/,.tox/" patterns/ ; `randomly-seed` option from `pytest-randomly` helps with deterministic outputs for examples like `other/blackboard.py` pytest --randomly-seed=1234 --doctest-modules patterns/ - pytest -s -vv --cov={envsitepackagesdir}/patterns --log-level=INFO tests/ + pytest -s -vv --cov=patterns/ --log-level=INFO tests/ [testenv:cov-report]