Skip to main content

Provides decorators and utilities for implementing the singleton pattern.

Reason this release was yanked:

singleton_instance() is incorrect.

Project description

Any-singleton

Provides decorators and utilities for implementing the singleton pattern.


Installation

python -m pip install any-singleton

Documentation

Usage

Create a singleton

Register a singleton with a value:

from any_singleton import singleton

tea = singleton('my_project.main.coffee', 'tea')

Instantiate an object and register as a singleton:

from any_singleton import singleton

my_range = singleton('my_project.main.coffee', range, 123)

For disambiguating, you can use singleton_value() to instead singleton() when the value is a type:

from any_singleton import singleton_value

class Tea:
    pass

tea = singleton_value('my_project.main.coffee', type(Tea))

Make a function can only be called once in global

Using @once to create a function that can only be called once in global.

import tomllib
from any_singleton import once, singleton

@once('my_project.initializations.init')
def init(config_path: str) -> None:
    with open(config_path, 'rb') as f:
        config = singleton('my_project.globals.config', tomllib.load(f))

init('config.toml')

Or just using @run_once to create a function as same as decorated with @once and calling it immediately.

import tomllib
from any_singleton import run_once, singleton

@run_once('my_project.initializations.init', 'config.toml')
def init(config_path: str) -> None:
    with open(config_path, 'rb') as f:
        config = singleton('my_project.globals.config', tomllib.load(f))

ATTENTION

  • any-singleton will OCCUPY the global variable _any_singleton, see any_singleton.singletons.GLOBAL_KEY.
  • DO NOT use @cached_return as a domain name. It's a RESERVED word.

View this source code on GitHub.

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

any_singleton-1.0.0.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

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

any_singleton-1.0.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file any_singleton-1.0.0.tar.gz.

File metadata

  • Download URL: any_singleton-1.0.0.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for any_singleton-1.0.0.tar.gz
Algorithm Hash digest
SHA256 45f6af035a2fb579e6b411b71f2b4a2331c809c900114b01fe1c0b2fcabe778a
MD5 fd203298775f2fdd25f7ca879ebb4bd6
BLAKE2b-256 63e6ae92b184a214d8fb1700be639a5e39bec76a094c00d19f244e25dc99aa0f

See more details on using hashes here.

Provenance

The following attestation bundles were made for any_singleton-1.0.0.tar.gz:

Publisher: publish-pypi.yml on Orange23333/any-singleton

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file any_singleton-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: any_singleton-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for any_singleton-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fb876b1a5a271e32418f8e2c2acf5562dc2a693a1eddf4becf0f4b008e12f2e8
MD5 89dbafc3d3be2786b3c8085192519e6e
BLAKE2b-256 f28fc6e99a745c5ca503e1dc085f983af8c15bcf3538aee5f8b11945e88fa2e9

See more details on using hashes here.

Provenance

The following attestation bundles were made for any_singleton-1.0.0-py3-none-any.whl:

Publisher: publish-pypi.yml on Orange23333/any-singleton

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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