Skip to main content

Thread-safe singleton and multiton pattern decorators.

Project description

philiprehberger-singleton

Tests PyPI version Last updated

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"

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.

Development

pip install -e .
python -m pytest tests/ -v

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

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

philiprehberger_singleton-0.1.2.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

philiprehberger_singleton-0.1.2-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_singleton-0.1.2.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_singleton-0.1.2.tar.gz
Algorithm Hash digest
SHA256 bc1f86b8b1fdfcbfb75d7cea290d36c70fab65b6c2b8d2951bc8963e38cd83dd
MD5 542f3ae342591b9b15113ed66fcb1c9c
BLAKE2b-256 fef6455846dd3c83b505ec1301371378e8b09b1fc55cb9ab00602478b43d0afc

See more details on using hashes here.

File details

Details for the file philiprehberger_singleton-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_singleton-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 97eaec7033a3f5915922f29b2b8a3576f541df410428c7a673854de3269d712c
MD5 793a4292ab4f4d77aca830599b21a0b8
BLAKE2b-256 81a60d18a0cef9213ab8b5195a7a200af6533b51b43799fe5b1de5142714e2e9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page