kkpyutil 1.41.0__py3-none-any.whl → 1.42.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: kkpyutil
3
- Version: 1.41.0
3
+ Version: 1.42.0
4
4
  Summary: Building blocks for sysadmin and DevOps
5
5
  Home-page: https://github.com/kakyoism/kkpyutil/
6
6
  License: MIT
@@ -0,0 +1,7 @@
1
+ kkpyutil.py,sha256=W9idh6_OvxtfOMfLWKxj6iDIFHNilsBX03Zcm-A99ps,113164
2
+ kkpyutil_helper/windows/kkttssave.ps1,sha256=xa3-WzqNh2rGYlOx_I4ewOuCE94gkTO5cEwYH0M67_0,446
3
+ kkpyutil_helper/windows/kkttsspeak.ps1,sha256=7WUUHMmjTQroUWA2Mvdt4JtSt475nZUHQx-qP-7rS6o,305
4
+ kkpyutil-1.42.0.dist-info/LICENSE,sha256=uISevGnCxB5QOU0ftbofN75_yUtd6E2h_MWE1zqagC8,1065
5
+ kkpyutil-1.42.0.dist-info/METADATA,sha256=0ZKxCttVdGWjb4NrbZSPNFp0kjNfpIS9SxE-CUyTKp0,1136
6
+ kkpyutil-1.42.0.dist-info/WHEEL,sha256=kLuE8m1WYU0Ig0_YEGrXyTtiJvKPpLpDEiChiNyei5Y,88
7
+ kkpyutil-1.42.0.dist-info/RECORD,,
kkpyutil.py CHANGED
@@ -74,11 +74,35 @@ if PLATFORM == 'Windows':
74
74
 
75
75
  # region classes
76
76
 
77
+ # class ClassicSingleton:
78
+ # _instances = {}
79
+ #
80
+ # def __new__(cls, *args, **kwargs):
81
+ # if cls not in cls._instances:
82
+ # print(f"Creating new instance for {cls}")
83
+ # cls._instances[cls] = super(ClassicSingleton, cls).__new__(cls, *args, **kwargs)
84
+ # else:
85
+ # print(f"Reusing instance for {cls}")
86
+ # return cls._instances[cls]
87
+
88
+
77
89
  class ClassicSingleton:
78
- def __new__(cls):
79
- if not hasattr(cls, 'instance'):
80
- cls.instance = super(ClassicSingleton, cls).__new__(cls)
81
- return cls.instance
90
+ _instances = {}
91
+
92
+ @classmethod
93
+ def instance(cls, *args, **kwargs):
94
+ if cls not in cls._instances:
95
+ # Create instance using `object.__new__` directly to avoid triggering overridden `__new__`
96
+ cls._instances[cls] = object.__new__(cls)
97
+ cls._instances[cls].__init__(*args, **kwargs)
98
+ return cls._instances[cls]
99
+
100
+ def __new__(cls, *args, **kwargs):
101
+ if cls in cls._instances:
102
+ # Allow the instance to exist if already created
103
+ return cls._instances[cls]
104
+ # Otherwise, raise error if someone tries to use `cls()` directly
105
+ raise RuntimeError("Use `cls.instance()` to access the singleton instance.")
82
106
 
83
107
 
84
108
  class BorgSingleton:
@@ -95,6 +119,23 @@ class BorgSingleton:
95
119
  return obj
96
120
 
97
121
 
122
+ class SingletonDecorator:
123
+ """
124
+ - Decorator to build Singleton class, single-inheritance only.
125
+ - Usage:
126
+ class MyClass: ...
127
+ myobj = SingletonDecorator(MyClass, args, kwargs)
128
+ """
129
+ def __init__(self, klass, *args, **kwargs):
130
+ self.klass = klass
131
+ self.instance = None
132
+
133
+ def __call__(self, *args, **kwargs):
134
+ if self.instance is None:
135
+ self.instance = self.klass(*args, **kwargs)
136
+ return self.instance
137
+
138
+
98
139
  class LowPassLogFilter(object):
99
140
  """
100
141
  Logging filter: Show log messages below input level.
@@ -1,7 +0,0 @@
1
- kkpyutil.py,sha256=h-6ouzFxcD0npugNk4vNH0moQeNJ-cYdkquKY_7V3IM,111759
2
- kkpyutil_helper/windows/kkttssave.ps1,sha256=xa3-WzqNh2rGYlOx_I4ewOuCE94gkTO5cEwYH0M67_0,446
3
- kkpyutil_helper/windows/kkttsspeak.ps1,sha256=7WUUHMmjTQroUWA2Mvdt4JtSt475nZUHQx-qP-7rS6o,305
4
- kkpyutil-1.41.0.dist-info/LICENSE,sha256=uISevGnCxB5QOU0ftbofN75_yUtd6E2h_MWE1zqagC8,1065
5
- kkpyutil-1.41.0.dist-info/METADATA,sha256=7nPG14wwwICSVCT6QKvg3Jqec8EaMV0cSSMLM7ox8i8,1136
6
- kkpyutil-1.41.0.dist-info/WHEEL,sha256=kLuE8m1WYU0Ig0_YEGrXyTtiJvKPpLpDEiChiNyei5Y,88
7
- kkpyutil-1.41.0.dist-info/RECORD,,