philiprehberger-singleton
Thread-safe singleton and multiton pattern decorators.
Installation
pip install philiprehberger-singleton
Usage
Singleton
The @singleton decorator ensures only one instance of a class exists:
from philiprehberger_singleton import singleton
@singleton
class Database:
def __init__(self, url: str) -> None:
self.url = url
db1 = Database("postgres://localhost/mydb")
db2 = Database("postgres://localhost/other")
assert db1 is db2 # same instance
assert db1.url == "postgres://localhost/mydb"
Reset
Use reset() to discard the cached instance (useful in tests):
Database.reset()
db3 = Database("postgres://localhost/new")
assert db3.url == "postgres://localhost/new"
Read-only access via instance()
instance() returns the cached singleton without constructing one. Useful when callers should not be able to create the singleton, only access it.
Database("postgres://localhost/mydb")
db = Database.instance() # → existing instance
Database.reset()
Database.instance() # raises RuntimeError
Non-raising existence check via is_instantiated()
When you only need to know whether the singleton was constructed (and don't want
to use try/except around instance()), call is_instantiated():
if Database.is_instantiated():
Database.instance().close()
Multiton
The @multiton(key=...) decorator maintains one instance per unique key value:
from philiprehberger_singleton import multiton
@multiton(key="name")
class Connection:
def __init__(self, name: str, timeout: int = 30) -> None:
self.name = name
self.timeout = timeout
cache = Connection("cache", timeout=10)
db = Connection("db", timeout=60)
cache2 = Connection("cache")
assert cache is cache2 # same key -> same instance
assert cache is not db # different key -> different instance
Reset
Use reset() to discard all cached instances:
Connection.reset()
API
| Function / Class | Description |
|---|---|
@singleton |
Thread-safe singleton decorator. Returns the same instance on every call. |
@multiton(key) |
Thread-safe multiton decorator factory. One instance per unique value of the named parameter. |
cls.reset() |
Discards cached instance(s), added by both decorators. |
cls.instance() |
Returns the cached singleton without constructing one. Raises RuntimeError if not yet constructed. (Singleton only.) |
cls.is_instantiated() |
Returns True if the singleton has been constructed, False otherwise. Non-raising. (Singleton only.) |
Development
pip install -e .
python -m pytest tests/ -v
Support
If you find this project useful:
License
Metadata
Release files for philiprehberger-singleton 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| philiprehberger_singleton-0.4.0.tar.gz | 181.8 kB | Without provenance | Details for philiprehberger_singleton-0.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| philiprehberger_singleton-0.4.0-py3-none-any.whl | Python 3 | none | any | Details for philiprehberger_singleton-0.4.0-py3-none-any.whl |
Total release size: 187.2 kB
Release files / philiprehberger_singleton-0.4.0.tar.gz
| Download URL | philiprehberger_singleton-0.4.0.tar.gz |
|---|---|
| Size | 181.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b5e35e072824385791252d66239a50f98dd6ef477948812ce2e5fe2478db7377
|
|
BLAKE2b-256 checksum How to use checksums |
e83ea558c648111309d87f991129404949c2eeb2a9a827025dab24a43d3eddd8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.13
|
Release files / philiprehberger_singleton-0.4.0-py3-none-any.whl
| Download URL | philiprehberger_singleton-0.4.0-py3-none-any.whl |
|---|---|
| Size | 5.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c644b439cffe865f92416ee048787001794fafa11033122a21e254ba8baa6fcc
|
|
BLAKE2b-256 checksum How to use checksums |
5f9b3ad22412fd1926600135ee55a0b80e9143f1d4dc3c5a9e193843ef57ea6e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.13
|
Security for philiprehberger-singleton 0.4.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.