From 1176d609e8d04542bc886f75378a4fd380dba5b4 Mon Sep 17 00:00:00 2001 From: rednafi Date: Sun, 5 Jul 2020 22:05:03 +0600 Subject: [PATCH 1/3] Added typehint to catalog pattern --- patterns/behavioral/catalog.py | 76 ++++++++++++++++++++-------------- 1 file changed, 46 insertions(+), 30 deletions(-) diff --git a/patterns/behavioral/catalog.py b/patterns/behavioral/catalog.py index 0570f7d3e..27243dad3 100644 --- a/patterns/behavioral/catalog.py +++ b/patterns/behavioral/catalog.py @@ -6,34 +6,40 @@ __author__ = "Ibrahim Diop " +from typing import Callable, Dict + + class Catalog: """catalog of multiple static methods that are executed depending on an init parameter """ - def __init__(self, param): + def __init__(self, param: str) -> None: # 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: Dict[Callable] = { + "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(): self.param = param else: - raise ValueError("Invalid Value for Param: {0}".format(param)) + raise ValueError(f"Invalid Value for Param: {param}") @staticmethod - def _static_method_1(): + def _static_method_1() -> None: print("executed method 1!") @staticmethod - def _static_method_2(): + def _static_method_2() -> None: print("executed method 2!") - def main_method(self): + def main_method(self) -> None: """will execute either _static_method_1 or _static_method_2 depending on self.param value @@ -49,24 +55,27 @@ class CatalogInstance: parameter """ - def __init__(self, param): - self.x1 = 'x1' - self.x2 = 'x2' + def __init__(self, param: str) -> None: + self.x1 = "x1" + self.x2 = "x2" # simple test to validate param value if param in self._instance_method_choices: self.param = param else: - raise ValueError("Invalid Value for Param: {0}".format(param)) + raise ValueError(f"Invalid Value for Param: {param}") - def _instance_method_1(self): - print("Value {}".format(self.x1)) + def _instance_method_1(self) -> None: + print(f"Value {self.x1}") - def _instance_method_2(self): - print("Value {}".format(self.x2)) + def _instance_method_2(self) -> None: + print(f"Value {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): + def main_method(self) -> None: """will execute either _instance_method_1 or _instance_method_2 depending on self.param value @@ -81,25 +90,28 @@ class CatalogClass: parameter """ - x1 = 'x1' - x2 = 'x2' + x1 = "x1" + x2 = "x2" - def __init__(self, param): + def __init__(self, param: str) -> None: # simple test to validate param value if param in self._class_method_choices: self.param = param else: - raise ValueError("Invalid Value for Param: {0}".format(param)) + raise ValueError(f"Invalid Value for Param: {param}") @classmethod - def _class_method_1(cls): - print("Value {}".format(cls.x1)) + def _class_method_1(cls) -> None: + print(f"Value {cls.x1}") @classmethod - def _class_method_2(cls): - print("Value {}".format(cls.x2)) + def _class_method_2(cls)->None: + print(f"Value {cls.x2}") - _class_method_choices = {'param_value_1': _class_method_1, 'param_value_2': _class_method_2} + _class_method_choices: Dict[Callable] = { + "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 @@ -116,22 +128,25 @@ class CatalogStatic: parameter """ - def __init__(self, param): + def __init__(self, param: str) -> None: # simple test to validate param value if param in self._static_method_choices: self.param = param else: - raise ValueError("Invalid Value for Param: {0}".format(param)) + raise ValueError(f"Invalid Value for Param: {param}") @staticmethod - def _static_method_1(): + def _static_method_1() -> None: print("executed method 1!") @staticmethod - def _static_method_2(): + def _static_method_2() -> None: print("executed method 2!") - _static_method_choices = {'param_value_1': _static_method_1, 'param_value_2': _static_method_2} + _static_method_choices: Dict[Callable] = { + "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 @@ -163,4 +178,5 @@ def main(): if __name__ == "__main__": import doctest + doctest.testmod() From 4ba0c3fe28bb445cdfc2744b9f3b0c963ea85996 Mon Sep 17 00:00:00 2001 From: rednafi Date: Sun, 5 Jul 2020 23:53:11 +0600 Subject: [PATCH 2/3] Added typehint to catalog pattern --- patterns/behavioral/catalog.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/patterns/behavioral/catalog.py b/patterns/behavioral/catalog.py index 27243dad3..d1b87a42d 100644 --- a/patterns/behavioral/catalog.py +++ b/patterns/behavioral/catalog.py @@ -3,10 +3,10 @@ init. Note the use of a single dictionary instead of multiple conditions """ -__author__ = "Ibrahim Diop " - +from __future__ import annotations +from typing import Callable, Dict, Any -from typing import Callable, Dict +__author__ = "Ibrahim Diop " class Catalog: @@ -20,7 +20,7 @@ def __init__(self, param: str) -> None: # 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: Dict[Callable] = { + self._static_method_choices = { "param_value_1": self._static_method_1, "param_value_2": self._static_method_2, } @@ -105,10 +105,10 @@ def _class_method_1(cls) -> None: print(f"Value {cls.x1}") @classmethod - def _class_method_2(cls)->None: + def _class_method_2(cls) -> None: print(f"Value {cls.x2}") - _class_method_choices: Dict[Callable] = { + _class_method_choices = { "param_value_1": _class_method_1, "param_value_2": _class_method_2, } @@ -143,16 +143,17 @@ def _static_method_1() -> None: def _static_method_2() -> None: print("executed method 2!") - _static_method_choices: Dict[Callable] = { + _static_method_choices = { "param_value_1": _static_method_1, "param_value_2": _static_method_2, } - def main_method(self): + def main_method(self) -> None: """will execute either _static_method_1 or _static_method_2 depending on self.param value """ + self._static_method_choices[self.param].__get__(None, self.__class__)() From af85c4b95f0c83a649d6bd8f49925b922f3f632a Mon Sep 17 00:00:00 2001 From: rednafi Date: Sun, 5 Jul 2020 23:56:31 +0600 Subject: [PATCH 3/3] Added typehint to catalog pattern --- patterns/behavioral/catalog.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/patterns/behavioral/catalog.py b/patterns/behavioral/catalog.py index d1b87a42d..f979ac321 100644 --- a/patterns/behavioral/catalog.py +++ b/patterns/behavioral/catalog.py @@ -3,9 +3,6 @@ init. Note the use of a single dictionary instead of multiple conditions """ -from __future__ import annotations -from typing import Callable, Dict, Any - __author__ = "Ibrahim Diop "