Skip to main content

Provides decorators and utilities for implementing the singleton pattern.

Project description

Any-singleton

Provides decorators and utilities for implementing the singleton pattern.

GitHub Repo stars PyPI - License PyPI - Version PyPI - Python Version

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 and any_singleton.nonreferenced_types as a domain name. They're RESERVED words.

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.1.0.tar.gz (4.6 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.1.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: any_singleton-1.1.0.tar.gz
  • Upload date:
  • Size: 4.6 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.1.0.tar.gz
Algorithm Hash digest
SHA256 fd4b8352a2ec49e9b193257112e44f842c42a546dc2cb800b094b7e34df95528
MD5 d7a9a11fa0476705cda6b829fd74a682
BLAKE2b-256 e0b106590377e8d614c4cfcb8039aa8433d7b1fc8d69f4f4cb3a9fb46f4db4e0

See more details on using hashes here.

Provenance

The following attestation bundles were made for any_singleton-1.1.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.1.0-py3-none-any.whl.

File metadata

  • Download URL: any_singleton-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.9 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 47ce19c60c9c2ad3c1da4b55d7714fea99fbae1ddd7a2b95c6e499ff7971ae37
MD5 de6200619ed329d335fce15c408cb88e
BLAKE2b-256 aeef9b73e4d92554d2cb9a0f10ee4364f0506f5e7d297663012a39c10458e7cc

See more details on using hashes here.

Provenance

The following attestation bundles were made for any_singleton-1.1.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