###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 [working on this]
* The singleton decorator and test bench pass black and mypy
* The test suite is automatically run before publishing
This has some fixes, plus a threadsafe version
from singleton_decorator1 import singleton
@singleton
class MyClass...
or
from singleton_decorator1 import threaded_singleton
@threaded_singleton
nnclass MyThreadsafeClass...
Note that if MyThreadsafeClass is __not__ threadsafe, then the decorator
will not fix that -- it only ensures that the singleton functionality is
threnad 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.10",
"maintainer_email": null,
"keywords": "decorators, singleton",
"author": "Editor 82 of wiki.python.org",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/ba/21/ff88e234b4119a8d97d9c0b6ff75ff3e47067c9e1a14c2aa5af255ed52d1/singleton_decorator1-0.5.1.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 [working on this]\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, 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 \nnnclass MyThreadsafeClass... \n\nNote that if MyThreadsafeClass is __not__ threadsafe, then the decorator\nwill not fix that -- it only ensures that the singleton functionality is\nthrenad 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": "Packaged selections from Python Decorator Library on wiki.python.org",
"version": "0.5.1",
"project_urls": {
"Homepage": "https://codeberg.org/Sojournings/singleton-decorator.git",
"Repository": "https://codeberg.org/Sojournings/singleton-decorator.git"
},
"split_keywords": [
"decorators",
" singleton"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "1016d44cf5771fca223d14c24fdde86797287a7a5a8c4b0c020fcdf89d14026a",
"md5": "dddbc1904f42a9e4bd9d3d561c6391cb",
"sha256": "6cb494a6defcad353ffd9f1af1c855d1b045feb69c91d5f8f28bfcd72dcc9781"
},
"downloads": -1,
"filename": "singleton_decorator1-0.5.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "dddbc1904f42a9e4bd9d3d561c6391cb",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.10",
"size": 17573,
"upload_time": "2025-07-24T23:49:11",
"upload_time_iso_8601": "2025-07-24T23:49:11.092964Z",
"url": "https://files.pythonhosted.org/packages/10/16/d44cf5771fca223d14c24fdde86797287a7a5a8c4b0c020fcdf89d14026a/singleton_decorator1-0.5.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "ba21ff88e234b4119a8d97d9c0b6ff75ff3e47067c9e1a14c2aa5af255ed52d1",
"md5": "fff73515f13cce2fd4dd3dcd4f324c38",
"sha256": "a1584785b8432b4c85d85c91b940d88d859ae88b00311cd4bb0679eeeac79e14"
},
"downloads": -1,
"filename": "singleton_decorator1-0.5.1.tar.gz",
"has_sig": false,
"md5_digest": "fff73515f13cce2fd4dd3dcd4f324c38",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.10",
"size": 17690,
"upload_time": "2025-07-24T23:49:12",
"upload_time_iso_8601": "2025-07-24T23:49:12.516797Z",
"url": "https://files.pythonhosted.org/packages/ba/21/ff88e234b4119a8d97d9c0b6ff75ff3e47067c9e1a14c2aa5af255ed52d1/singleton_decorator1-0.5.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-07-24 23:49:12",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": true,
"codeberg_user": "Sojournings",
"codeberg_project": "singleton-decorator",
"lcname": "singleton_decorator1"
}