Skip to content

Orange23333/any-singleton

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Provides decorators and utilities for implementing the singleton pattern.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages