Thread-safe singleton and multiton pattern decorators.
Project description
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
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.) |
Development
pip install -e .
python -m pytest tests/ -v
Support
If you find this project useful:
License
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file philiprehberger_singleton-0.3.0.tar.gz.
File metadata
- Download URL: philiprehberger_singleton-0.3.0.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cdeb27e7ac5795e383684f89a3f56b7a80db20bac7555379c4b23a1006ab1c45
|
|
| MD5 |
77e3ea87e5a56a9edaa03035f5e1e244
|
|
| BLAKE2b-256 |
b1055c01832f84c96ee81e5d472c74fab2b0802e1a5af199118dd6baf99443fb
|
File details
Details for the file philiprehberger_singleton-0.3.0-py3-none-any.whl.
File metadata
- Download URL: philiprehberger_singleton-0.3.0-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1634b82c670ef4ceada21ba1a688ff96ca0d7fc1f8933d762e1352cac0983cbd
|
|
| MD5 |
508e8b29c496eec4b3bcc0723838f786
|
|
| BLAKE2b-256 |
17f7a4159f9535e18ef42ee3e398051a7fb16a68ed8671921618272c063288bd
|