Last active
July 11, 2021 13:00
-
-
Save johanvergeer/290fbd8a8c0fbd912f65164dcf8da87a to your computer and use it in GitHub Desktop.
Revisions
-
johanvergeer revised this gist
Jul 11, 2021 . 2 changed files with 12 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,9 @@ class PersonService: @property def person_repo(self) -> PersonRepo: return PersonRepoFactory().create() def set_name(self, person_id: int, new_name: str) -> None: p = self.person_repo.find(person_id) p.name = new_name self.person_repo.add_or_update(p) 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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ class PersonRepo(Protocol): @abstractmethod def add_or_update(self, person: Person) -> None: ... @abstractmethod @@ -16,7 +16,7 @@ class PersonRepoJson(PersonRepo): def __init__(self, file: Path) -> None: self.__file = file def add_or_update(self, person: Person) -> None: """add person to file""" def remove(self, person_id: int) -> None: @@ -27,7 +27,7 @@ def find(self, person_id: int) -> None: class PersonRepoSql(PersonRepo): def add_or_update(self, person: Person) -> None: """add person to database""" def remove(self, person_id: int) -> None: -
johanvergeer created this gist
Jul 11, 2021 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,48 @@ class PersonRepo(Protocol): @abstractmethod def add(self, person: Person) -> None: ... @abstractmethod def remove(self, person_id: int) -> None: ... @abstractmethod def find(self, person_id: int) -> None: ... class PersonRepoJson(PersonRepo): def __init__(self, file: Path) -> None: self.__file = file def add(self, person: Person) -> None: """add person to file""" def remove(self, person_id: int) -> None: """remove person from file""" def find(self, person_id: int) -> None: """find person in file""" class PersonRepoSql(PersonRepo): def add(self, person: Person) -> None: """add person to database""" def remove(self, person_id: int) -> None: """remove person from database""" def find(self, person_id: int) -> None: """find person in database""" class PersonRepoFactory: def create(self) -> PersonRepo: if os.getenv("PERSON_REPO_TYPE") == "json": return PersonRepoJson(Path(os.getenv("PERSON_REPO_PATH"))) elif os.getenv("PERSON_REPO_TYPE) == "sql": return PersonRepoSql() else: raise ValueError()