kkpyutil 1.40.1__tar.gz → 1.41.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.41.0
4
4
  Summary: Building blocks for sysadmin and DevOps
5
5
  Home-page: https://github.com/kakyoism/kkpyutil/
6
6
  License: MIT
@@ -74,22 +74,25 @@ if PLATFORM == 'Windows':
74
74
 
75
75
  # region classes
76
76
 
77
- class SingletonDecorator:
77
+ class ClassicSingleton:
78
+ def __new__(cls):
79
+ if not hasattr(cls, 'instance'):
80
+ cls.instance = super(ClassicSingleton, cls).__new__(cls)
81
+ return cls.instance
82
+
83
+
84
+ class BorgSingleton:
78
85
  """
79
- Decorator to build Singleton class, single-inheritance only.
80
- Usage:
81
- class MyClass: ...
82
- myobj = SingletonDecorator(MyClass, args, kwargs)
86
+ - Borg pattern: all instances share the same state, but not the same identity
87
+ - override _shared_borg_state to avoid child polluting states of parent instances
88
+ - ref: https://www.geeksforgeeks.org/singleton-pattern-in-python-a-complete-guide/
83
89
  """
90
+ _shared_borg_state = {}
84
91
 
85
- def __init__(self, klass, *args, **kwargs):
86
- self.klass = klass
87
- self.instance = None
88
-
89
- def __call__(self, *args, **kwargs):
90
- if self.instance is None:
91
- self.instance = self.klass(*args, **kwargs)
92
- return self.instance
92
+ def __new__(cls, *args, **kwargs):
93
+ obj = super(BorgSingleton, cls).__new__(cls, *args, **kwargs)
94
+ obj.__dict__ = cls._shared_borg_state
95
+ return obj
93
96
 
94
97
 
95
98
  class LowPassLogFilter(object):
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "kkpyutil"
3
- version = "1.40.1"
3
+ version = "1.41.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