From f58e469404d576351f16cad5d7370e41db2a401b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=AE=E5=9C=B0=E5=AE=8F=E6=A8=B9?= Date: Sun, 6 Jun 2021 11:36:34 +0900 Subject: [PATCH 1/4] Add test case --- tests/clazz/base/test_autoload_module.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/clazz/base/test_autoload_module.py b/tests/clazz/base/test_autoload_module.py index 941a2a1..f78827b 100644 --- a/tests/clazz/base/test_autoload_module.py +++ b/tests/clazz/base/test_autoload_module.py @@ -45,7 +45,9 @@ def test_global_setting(self): with self.subTest(setting=setting): ModuleLoader.set_setting(*setting) test_loader = ModuleLoader() + test_loader2 = ModuleLoader() self.assertTupleEqual((test_loader.base_path, test_loader.strict), expected) + self.assertTupleEqual((test_loader2.base_path, test_loader2.strict), expected) def test_initialize(self): test_cases = ( From 4ee96d27b6a68e04a78e72e98025c44cd7d4142d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=AE=E5=9C=B0=E5=AE=8F=E6=A8=B9?= Date: Thu, 10 Jun 2021 08:45:01 +0900 Subject: [PATCH 2/4] Add singleton parameter --- autoload/module_loader.py | 20 ++++++++++++++++++-- tests/clazz/base/test_autoload_module.py | 24 ++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/autoload/module_loader.py b/autoload/module_loader.py index b694f37..eaa179c 100644 --- a/autoload/module_loader.py +++ b/autoload/module_loader.py @@ -53,18 +53,32 @@ def _access_private(): class ModuleLoaderSetting: base_path: Optional[str] = None strict: bool = False + singleton: bool = False class ModuleLoader: _setting: ClassVar[ModuleLoaderSetting] = ModuleLoaderSetting() + _instance: Optional["ModuleLoader"] = None @classmethod def get_setting(cls) -> ModuleLoaderSetting: return cls._setting @classmethod - def set_setting(cls, base_path: Optional[str] = None, strict: bool = False) -> None: - cls._setting = ModuleLoaderSetting(base_path, strict) + def set_setting( + cls, + base_path: Optional[str] = None, + strict: bool = False, + singleton: bool = False, + ) -> None: + cls._setting = ModuleLoaderSetting(base_path, strict, singleton) + + def __new__(cls, *args, **kwargs): + if cls._setting.singleton is False: + return super(ModuleLoader, cls).__new__(cls) + if cls._instance is None: + cls._instance = super(ModuleLoader, cls).__new__(cls) + return cls._instance def __init__(self, base_path: Optional[str] = None, strict: Optional[bool] = None): """initialize @@ -75,6 +89,8 @@ def __init__(self, base_path: Optional[str] = None, strict: Optional[bool] = Non per a Python module on a basis of its name. """ setting = ModuleLoader._setting + if setting.singleton is True and hasattr(self, "_ModuleLoader__base_path"): + return global_base_path, global_strict = setting.base_path, setting.strict self.__base_path: str = ( _access_private().init_base_url(base_path) diff --git a/tests/clazz/base/test_autoload_module.py b/tests/clazz/base/test_autoload_module.py index f78827b..81e6a1f 100644 --- a/tests/clazz/base/test_autoload_module.py +++ b/tests/clazz/base/test_autoload_module.py @@ -49,6 +49,30 @@ def test_global_setting(self): self.assertTupleEqual((test_loader.base_path, test_loader.strict), expected) self.assertTupleEqual((test_loader2.base_path, test_loader2.strict), expected) + def test_singleton(self): + ModuleLoader.set_setting(singleton=True) + test_cases = ( + (ModuleLoader(), ModuleLoader()), + (ModuleLoader('/test', strict=True), ModuleLoader()), + ) + for instance, expected in test_cases: + with self.subTest(instance=instance): + self.assertIs(instance, expected) + self.assertEqual(instance.base_path, expected.base_path) + self.assertEqual(instance.strict, expected.strict) + + def test_not_singleton(self): + test_cases = ( + (ModuleLoader(), ModuleLoader(), False), + (ModuleLoader('/test', strict=True), ModuleLoader(), True), + ) + for instance, expected, optional in test_cases: + with self.subTest(instance=instance): + self.assertIsNot(instance, expected) + if optional: + self.assertNotEqual(instance.base_path, expected.base_path) + self.assertNotEqual(instance.strict, expected.strict) + def test_initialize(self): test_cases = ( (ModuleLoader('').base_path, '/'), From b2856db3fad6d2de76ae07288ac523fdfa0f3e5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=AE=E5=9C=B0=E5=AE=8F=E6=A8=B9?= Date: Thu, 10 Jun 2021 08:46:20 +0900 Subject: [PATCH 3/4] Version Up --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 495fe9c..b266282 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "autoload-module" -version = "1.6.0" +version = "1.7.0" description = "Python Autoload Module" authors = ["Hiroki Miyaji "] license = "MIT" From 5ce93a8bafe905262427b0222a47f06aed174a6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=AE=E5=9C=B0=E5=AE=8F=E6=A8=B9?= Date: Thu, 10 Jun 2021 08:57:26 +0900 Subject: [PATCH 4/4] Add doc about singleton parameter --- README.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0e296f4..9ef02c5 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ loader = ModuleLoader('/user/local/src/custom') About strict parameter, please see [here](#NOTE) . -You can also create global setting. +You can also create global setting and initialize singleton object. ```python from autoload import ModuleLoader import os @@ -86,6 +86,19 @@ print(loader_a.base_path) # -> /Users/user1/abc print(loader_b.base_path) # -> /Users/user1/abc + +# singleton setting +ModuleLoader.set_setting(singleton=True) + +loader_c = ModuleLoader() +loader_d = ModuleLoader() +loader_e = ModuleLoader('/test') + +assert loader_c is loader_d # OK +assert loader_c is loader_e # OK + +# The base_path is '/Users/user1/abc' +assert loader_c.base_path is loader_e.base_path # OK ``` ### Methods