python -m pip install any-singletonRegister 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))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))any-singletonwill OCCUPY the global variable_any_singleton, seeany_singleton.singletons.GLOBAL_KEY.- DO NOT use
@cached_returnas a domain name. It's a RESERVED word.
View this source code on GitHub.