diff --git a/satella/coding/__init__.py b/satella/coding/__init__.py index e0bcd88a33f01f09e6c6db45be3d6aa553f27c3e..7c93216ec6a3f8be881d09f1acf6a125f18e5782 100644 --- a/satella/coding/__init__.py +++ b/satella/coding/__init__.py @@ -13,7 +13,7 @@ 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_decorator import singleton as Singleton +from .singleton import Singleton __all__ = [ diff --git a/satella/coding/singleton.py b/satella/coding/singleton.py new file mode 100644 index 0000000000000000000000000000000000000000..690a8c8928af6ba50e33520cae736aa90b90ceb7 --- /dev/null +++ b/satella/coding/singleton.py @@ -0,0 +1,28 @@ +""" +Taken from module pypi/singleton-decorator +""" + +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 + + +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 57c9f4e5674c99dfa1dac1cfe4aca5719c2c0bfe..c19c79fee8fc80f5d6093f2eef63fc21df612b58 100644 --- a/setup.py +++ b/setup.py @@ -9,7 +9,6 @@ setup(keywords=['ha', 'high availability', 'scalable', 'scalability', 'server'], "six", "monotonic", "typing", - 'singleton-decorator==1.0.0' ], tests_require=[ "nose", "mock", "coverage"