kkpyutil 1.40.1__tar.gz → 1.42.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.40.1
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
@@ -74,14 +74,58 @@ 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
+
89
+ class ClassicSingleton:
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 BorgSingleton:
109
+ """
110
+ - Borg pattern: all instances share the same state, but not the same identity
111
+ - override _shared_borg_state to avoid child polluting states of parent instances
112
+ - ref: https://www.geeksforgeeks.org/singleton-pattern-in-python-a-complete-guide/
113
+ """
114
+ _shared_borg_state = {}
115
+
116
+ def __new__(cls, *args, **kwargs):
117
+ obj = super(BorgSingleton, cls).__new__(cls, *args, **kwargs)
118
+ obj.__dict__ = cls._shared_borg_state
119
+ return obj
120
+
121
+
77
122
  class SingletonDecorator:
78
123
  """
79
- Decorator to build Singleton class, single-inheritance only.
80
- Usage:
124
+ - Decorator to build Singleton class, single-inheritance only.
125
+ - Usage:
81
126
  class MyClass: ...
82
127
  myobj = SingletonDecorator(MyClass, args, kwargs)
83
128
  """
84
-
85
129
  def __init__(self, klass, *args, **kwargs):
86
130
  self.klass = klass
87
131
  self.instance = None
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "kkpyutil"
3
- version = "1.40.1"
3
+ version = "1.42.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