###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
Raw data
{
"_id": null,
"home_page": "https://codeberg.org/Sojournings/singleton-decorator.git",
"name": "singleton-decorator1",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.7",
"maintainer_email": null,
"keywords": "decorators, singleton, threadsafe, testable, mypy clean, copy safe, verified",
"author": "Doug Sojourner",
"author_email": "<doug@sojournings.org>",
"download_url": "https://files.pythonhosted.org/packages/8e/5f/1d5c1416019c77dc170bd33258c142d171fa975428748a4abc0d0e5dcc4c/singleton_decorator1-1.0.0.tar.gz",
"platform": null,
"description": "###Singleton Decorator Library\n\nA decorator based on wiki.python.org Python Decorators Library, namely\nhttps://wiki.python.org/moin/PythonDecoratorLibrary#Singleton, with significant\nimprovements.\n\nFeatures (assuming you decorate a class definition of MyClass) \n* Every call of MyClass() results in the same instance, which persists from the first instantiation until the end of execution \n* Even if you delete all your references to the instance, it still persists to be returned on the next call of MyClass() \n* That instance is only initiallized once (the first time) \n* is_instance(MyClass(), MyClass) is True (that is, we do not wrap the class) \n* Using copy() or deepcopy() simply gives another reference to the single instance \n* The overhead of locking in the threaded version is low enough that you may use it even before adding threading to your class. \n* 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) \n* If MyClass has locking (either Lock or RLock) in the initialization (new and init) the singleton locking does not deadlock. \n* The test suite is included, so you can tell if I verified correctly \n* The singleton decorator and test bench pass black and mypy \n* The test suite is automatically run before publishing \n\nThis has some fixes (correct calling of old init, correct replacement of init, removal of copy and deepcopy), plus a threadsafe version\n\nfrom singleton_decorator1 import singleton \n@singleton \nclass MyClass... \n\nor\n\nfrom singleton_decorator1 import threaded_singleton \n@threaded_singleton \nclass MyThreadsafeClass... \n\nNote that if MyThreadsafeClass is __not__ threadsafe, then the decorator\nwill not fix that -- it only ensures that the singleton functionality is\nthread safe.\n\n\nThe library can be built with poetry >= 2.0: \n[Recommended] \nif needed, install pyenv \npyenv install 3.10 \npyenv local 3.10\n\n[required] \n(pip show poetry | grep -q \"Version: 2\") || pip install --ignore installed poetry==2\n\npoetry env use $(pyenv which python) or \npoetry env use $(which python)\n\npoetry install\n\npoetry build\n\n\n",
"bugtrack_url": null,
"license": "GNU General Public License v3 (GPLv3)",
"summary": "Singleton decorator which is: thread-safe, copy safe, mypy compliant, not wrapped class",
"version": "1.0.0",
"project_urls": {
"Homepage": "https://codeberg.org/Sojournings/singleton-decorator.git",
"Repository": "https://codeberg.org/Sojournings/singleton-decorator.git"
},
"split_keywords": [
"decorators",
" singleton",
" threadsafe",
" testable",
" mypy clean",
" copy safe",
" verified"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "1f80b189fd0ab5d83753aac0793d4cc61c2a2fe610684a5dc932de2ae506652b",
"md5": "a5e7ac4446f8688628784e63b9c64206",
"sha256": "90d9ae89b546f73e14df9f1ac8f0b3de9807529d17815398db3cfd149ee779a3"
},
"downloads": -1,
"filename": "singleton_decorator1-1.0.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "a5e7ac4446f8688628784e63b9c64206",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.7",
"size": 17831,
"upload_time": "2025-08-06T22:45:57",
"upload_time_iso_8601": "2025-08-06T22:45:57.575551Z",
"url": "https://files.pythonhosted.org/packages/1f/80/b189fd0ab5d83753aac0793d4cc61c2a2fe610684a5dc932de2ae506652b/singleton_decorator1-1.0.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "8e5f1d5c1416019c77dc170bd33258c142d171fa975428748a4abc0d0e5dcc4c",
"md5": "a7791d6e4cd70fb4146accdb8988bf31",
"sha256": "a33be52b2fc567bb3100fd8115e5e747c1f486aa6e1a20e9327c33def86d3169"
},
"downloads": -1,
"filename": "singleton_decorator1-1.0.0.tar.gz",
"has_sig": false,
"md5_digest": "a7791d6e4cd70fb4146accdb8988bf31",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.7",
"size": 4219085,
"upload_time": "2025-08-06T22:56:40",
"upload_time_iso_8601": "2025-08-06T22:56:40.355077Z",
"url": "https://files.pythonhosted.org/packages/8e/5f/1d5c1416019c77dc170bd33258c142d171fa975428748a4abc0d0e5dcc4c/singleton_decorator1-1.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-08-06 22:56:40",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": true,
"codeberg_user": "Sojournings",
"codeberg_project": "singleton-decorator",
"lcname": "singleton-decorator1"
}