From c34299573bc8838661ff5516c2f00560a2d810ba Mon Sep 17 00:00:00 2001 From: Piotr Maslanka <piotr.maslanka@henrietta.com.pl> Date: Sat, 17 Feb 2018 00:17:13 +0100 Subject: [PATCH] better singleton! --- CHANGELOG.md | 4 +++ satella/__init__.py | 2 +- satella/coding/__init__.py | 3 +- satella/coding/singleton.py | 68 ------------------------------------- setup.py | 3 +- 5 files changed, 9 insertions(+), 71 deletions(-) delete mode 100644 satella/coding/singleton.py diff --git a/CHANGELOG.md b/CHANGELOG.md index cd53ff4f..72d89add 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## v2.0.22rc4 + +* fixes #14 + ## v2.0.22rc3 * fixes #15 diff --git a/satella/__init__.py b/satella/__init__.py index df3b3c23..2e0230c7 100644 --- a/satella/__init__.py +++ b/satella/__init__.py @@ -1,2 +1,2 @@ # coding=UTF-8 -__version__ = '2.0.22rc3' +__version__ = '2.0.22rc4' diff --git a/satella/coding/__init__.py b/satella/coding/__init__.py index 186075c7..e0bcd88a 100644 --- a/satella/coding/__init__.py +++ b/satella/coding/__init__.py @@ -13,7 +13,8 @@ from .typecheck import typed, Callable, Sequence, \ Number, coerce, Set, Dict, List, Tuple, checked_coerce, for_argument, \ precondition, PreconditionError from .structures import TimeBasedHeap, Heap, typednamedtuple, OmniHashableMixin -from .singleton import Singleton +from singleton_decorator import singleton as Singleton + __all__ = [ 'typednamedtuple', 'OmniHashableMixin' diff --git a/satella/coding/singleton.py b/satella/coding/singleton.py deleted file mode 100644 index 0c8f9d4f..00000000 --- a/satella/coding/singleton.py +++ /dev/null @@ -1,68 +0,0 @@ -# coding=UTF-8 -from __future__ import print_function, absolute_import, division - -import functools - -import six - -__all__ = [ - 'Singleton', -] - -if six.PY3: - - # Taken from https://wiki.python.org/moin/PythonDecoratorLibrary - def Singleton(cls): - """ - Make a singleton out of decorated class. - - Usage: - - @Singleton - class MyClass(object): - ... - """ - - cls.__new_old__ = cls.__new__ - - @functools.wraps(cls.__new__) - def singleton_new(cls, *args, **kw): - it = cls.__dict__.get('__it__') - if it is not None: - return it - - cls.__it__ = it = cls.__new_old__(cls, *args, **kw) - it.__init_old__(*args, **kw) - return it - - cls.__new__ = singleton_new - cls.__init_old__ = cls.__init__ - cls.__init__ = object.__init__ - - return cls -else: - class _SingletonWrapper: - """ - A singleton wrapper class. Its instances would be created - for each decorated class. - """ - - def __init__(self, cls): - self.__wrapped__ = cls - self._instance = None - - def __call__(self, *args, **kwargs): - """Returns a single instance of decorated class""" - if self._instance is None: - self._instance = self.__wrapped__(*args, **kwargs) - return self._instance - - - # taken from https://pypi.python.org/pypi/singleton-decorator/1.0.0 - def Singleton(cls): - """ - A singleton decorator. Returns a wrapper objects. A call on that object - returns a single instance object of decorated class. Use the __wrapped__ - attribute to access decorated class directly in unit tests. - """ - return _SingletonWrapper(cls) diff --git a/setup.py b/setup.py index 00b5b535..57c9f4e5 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,8 @@ setup(keywords=['ha', 'high availability', 'scalable', 'scalability', 'server'], install_requires=[ "six", "monotonic", - "typing" + "typing", + 'singleton-decorator==1.0.0' ], tests_require=[ "nose", "mock", "coverage" -- GitLab