kkpyutil 1.41.0__tar.gz → 1.43.0__tar.gz

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.43.0
4
4
  Summary: Building blocks for sysadmin and DevOps
5
5
  Home-page: https://github.com/kakyoism/kkpyutil/
6
6
  License: MIT
@@ -74,11 +74,47 @@ 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.")
106
+
107
+
108
+ class MetaSingleton(type):
109
+ """
110
+ - usage: class MyClass(metaclass=MetaSingleton)
111
+ """
112
+ _instances = {}
113
+
114
+ def __call__(cls, *args, **kwargs):
115
+ if cls not in cls._instances:
116
+ cls._instances[cls] = super(MetaSingleton, cls).__call__(*args, **kwargs)
117
+ return cls._instances[cls]
82
118
 
83
119
 
84
120
  class BorgSingleton:
@@ -95,6 +131,23 @@ class BorgSingleton:
95
131
  return obj
96
132
 
97
133
 
134
+ class SingletonDecorator:
135
+ """
136
+ - Decorator to build Singleton class, single-inheritance only.
137
+ - Usage:
138
+ class MyClass: ...
139
+ myobj = SingletonDecorator(MyClass, args, kwargs)
140
+ """
141
+ def __init__(self, klass, *args, **kwargs):
142
+ self.klass = klass
143
+ self.instance = None
144
+
145
+ def __call__(self, *args, **kwargs):
146
+ if self.instance is None:
147
+ self.instance = self.klass(*args, **kwargs)
148
+ return self.instance
149
+
150
+
98
151
  class LowPassLogFilter(object):
99
152
  """
100
153
  Logging filter: Show log messages below input level.
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "kkpyutil"
3
- version = "1.41.0"
3
+ version = "1.43.0"
4
4
  description = "Building blocks for sysadmin and DevOps"
5
5
  authors = ["Beinan Li <li.beinan@gmail.com>"]
6
6
  maintainers = ["Beinan Li <li.beinan@gmail.com>"]
File without changes
File without changes