Skip to main content

Singleton decorator which is: thread-safe, copy safe, mypy compliant, not wrapped class

Project description

###Singleton Decorator Library

A decorator based on wiki.python.org Python Decorators Library, namely https://wiki.python.org/moin/PythonDecoratorLibrary#Singleton, with significant improvements.

Features (assuming you decorate a class definition of MyClass)

  • Every call of MyClass() results in the same instance, which persists from the first instantiation until the end of execution
  • Even if you delete all your references to the instance, it still persists to be returned on the next call of MyClass()
  • That instance is only initiallized once (the first time)
  • is_instance(MyClass(), MyClass) is True (that is, we do not wrap the class)
  • Using copy() or deepcopy() simply gives another reference to the single instance
  • The overhead of locking in the threaded version is low enough that you may use it even before adding threading to your class.
  • If MyClass is threadsafe, then annotating with @threaded_singleton will give a threadsafe singleton (only the safety of creating the first instance requires locking, as all other singleton actions are no-ops or simply return a reference to the single instance)
  • If MyClass has locking (either Lock or RLock) in the initialization (new and init) the singleton locking does not deadlock.
  • The test suite is included, so you can tell if I verified correctly
  • The singleton decorator and test bench pass black and mypy
  • The test suite is automatically run before publishing

This has some fixes (correct calling of old init, correct replacement of init, removal of copy and deepcopy), plus a threadsafe version

from singleton_decorator1 import singleton
@singleton
class MyClass...

or

from singleton_decorator1 import threaded_singleton
@threaded_singleton
class MyThreadsafeClass...

Note that if MyThreadsafeClass is not threadsafe, then the decorator will not fix that -- it only ensures that the singleton functionality is thread safe.

The library can be built with poetry >= 2.0:
[Recommended]
if needed, install pyenv
pyenv install 3.10
pyenv local 3.10

[required]
(pip show poetry | grep -q "Version: 2") || pip install --ignore installed poetry==2

poetry env use $(pyenv which python) or
poetry env use $(which python)

poetry install

poetry build

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

singleton_decorator1-0.5.6.tar.gz (19.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

singleton_decorator1-0.5.6-py3-none-any.whl (17.9 kB view details)

Uploaded Python 3

File details

Details for the file singleton_decorator1-0.5.6.tar.gz.

File metadata

  • Download URL: singleton_decorator1-0.5.6.tar.gz
  • Upload date:
  • Size: 19.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.10.17 Linux/6.8.0-71-generic

File hashes

Hashes for singleton_decorator1-0.5.6.tar.gz
Algorithm Hash digest
SHA256 047e5f167e10f45f9cf0537d37665bb31b6be68f5eb0b82c2285f9e42d3f298d
MD5 a5935e10e5f244092323da345be5e82d
BLAKE2b-256 12b246308b1447f2d0d11f719accef48cc441ad3d2d86ce5a716b796eaef5c1b

See more details on using hashes here.

File details

Details for the file singleton_decorator1-0.5.6-py3-none-any.whl.

File metadata

  • Download URL: singleton_decorator1-0.5.6-py3-none-any.whl
  • Upload date:
  • Size: 17.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.10.17 Linux/6.8.0-71-generic

File hashes

Hashes for singleton_decorator1-0.5.6-py3-none-any.whl
Algorithm Hash digest
SHA256 3a678d11a53c8803ab7b159a22e56d92dc65a53ff11da0362a1b5ba869d51938
MD5 19cd9b6f87684b7ef97697ee7fb6d4d6
BLAKE2b-256 f8bb998bf196fc7e545c08c454a47f1b2ed11fcc8e83c0eba200a8f9c5c741e4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page