A clean, transparent and easy to use python singleton wrapper.
Project description
Singleton
About the project
A clean, transparent and easy to use python singleton wrapper.
Installation
pip install better-singleton
Usage
Singleton definition:
from better_singleton import singleton
@singleton
class Counter:
def __init__(self, initial_counter):
self.counter = initial_counter
def inc(self):
self.counter += 1
Usage:
counter1 = Counter(1)
counter1.inc()
counter2 = Counter(1)
print(counter2.counter)
# >>> 2
Ensures that at most only one instance of the class exists at any point in time.
__init__
and __new__
of the original class get called only once: the first time
an instance is created. This instance will from then on be the singleton instance.
Every following call to the constructor of the wrapped class returns the singleton instance.
The wrapped class cannot have a class variable called _singleton
as that stores
the singleton instance. This will throw an error before execution begins.
Not thread safe during first initialization.
Roadmap
Contributing
Publish
- Bump version in pyproject.toml
poetry update
poetry build
poetry publish
License
See License.md.
Contributors
Acknowledgments
Team Incentive Mechanism App Cluster of "Advanced Python Programming Spring 2022" Nour Bouzid, Yanjie Li, Stephan Schmiedmayer and Leonhard Zirus under supervision of Valentin Zieglmeier
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
Built Distribution
File details
Details for the file better_singleton-1.0.1.tar.gz
.
File metadata
- Download URL: better_singleton-1.0.1.tar.gz
- Upload date:
- Size: 2.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.11.4 Darwin/22.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3a684dec45b5ca99701b992a75e79e454be854586bbf109665fb038e09515945 |
|
MD5 | 9110a3de4ae6892d9a6006832546c0e2 |
|
BLAKE2b-256 | 60990c1c917b497bb358a2ffe0c37eb6134ffd64857ba41544bc15b5cab0e73e |
File details
Details for the file better_singleton-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: better_singleton-1.0.1-py3-none-any.whl
- Upload date:
- Size: 3.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.11.4 Darwin/22.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b7395c8ab07d7f3a78df3d6ff6eb7a4f910b515c863da66ff44547c93ad6c9b5 |
|
MD5 | 8858693f9c5ea8bdbff68c491aa8cc16 |
|
BLAKE2b-256 | 66a37d9a787aecb8f7222acdfc3e99018fe5dc65faefa892b424c83f562d8449 |