Last active
October 6, 2024 22:11
-
-
Save mortymacs/8fabac3de1c70e5080ba95cb6d7587d2 to your computer and use it in GitHub Desktop.
sample python design pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In [5]: from abc import ABC, abstractmethod | |
...: class Base(ABC): | |
...: @abstractmethod | |
...: def get_by_id(self, id): | |
...: pass | |
...: class C1(Base): | |
...: def get_by_id(self, id): | |
...: return f"hello client1: {id}" | |
...: class C2(Base): | |
...: def get_by_id(self, id): | |
...: return f"hello client2: {id}" | |
...: registers = [C1(), C2()] | |
...: for r in registers: | |
...: print(r.get_by_id(10)) | |
...: | |
hello client1: 10 | |
hello client2: 10 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In [5]: from abc import ABC, abstractmethod | |
...: class Base(ABC): | |
...: @abstractmethod | |
...: def get_by_id(self, id): | |
...: pass | |
...: @abstractmethod | |
...: def create(self, value): | |
...: pass | |
...: class C1(Base): | |
...: def get_by_id(self, id): | |
...: return f"hello client1: {id}" | |
...: def create(self, value): | |
...: return f"created row client1: value {id}" | |
...: class C2(Base): | |
...: def get_by_id(self, id): | |
...: return f"hello client2: {id}" | |
...: def create(self, value): | |
...: return "" | |
...: registers = [ | |
...: # client, built-in | |
...: (C1(), ["update", "delete", "create"]), | |
...: (C2(), []), | |
...: ] | |
...: for r, builtin in registers: | |
...: print(r.get_by_id(10), builtin) | |
...: if "create" in builtin: | |
...: print(r.create("hello")) | |
...: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment