Metadata-Version: 2.4
Name: any-singleton
Version: 1.1.0
Summary: Provides decorators and utilities for implementing the singleton pattern.
Keywords: singleton
Author: Orange 233
Author-email: Orange 233 <orange_233@foxmail.com>
License-Expression: MIT
License-File: LICENSE.txt
Classifier: Programming Language :: Python :: 3
Classifier: Operating System :: OS Independent
Classifier: License :: OSI Approved :: MIT License
Requires-Python: >=3.0.0
Project-URL: Homepage, https://github.com/Orange23333/any-singleton
Project-URL: Repository, https://github.com/Orange23333/any-singleton
Project-URL: Documentation, https://github.com/Orange23333/any-singleton/blob/main/README.md#Documentation
Project-URL: Issues, https://github.com/Orange23333/any-singleton/issues
Project-URL: Changelog, https://github.com/Orange23333/any-singleton/blob/main/CHANGELOG.md
Description-Content-Type: text/markdown

<div align="center">
    <h1>Any-singleton</h1>
    <p>Provides decorators and utilities for implementing the singleton pattern.</p>
    <a href="https://github.com/Orange23333/any-singleton/">
        <img alt="GitHub Repo stars" src="https://img.shields.io/github/stars/Orange23333/any-singleton" />
    </a>
    <a href="https://github.com/Orange23333/any-singleton/blob/main/LICENSE.txt">
        <img alt="PyPI - License" src="https://img.shields.io/pypi/l/any-singleton" />
    </a>
    <a href="https://pypi.org/project/any-singleton/">
        <img alt="PyPI - Version" src="https://img.shields.io/pypi/v/any-singleton" />
    </a>
    <a href="https://pypi.org/project/any-singleton/">
        <img alt="PyPI - Python Version" src="https://img.shields.io/pypi/pyversions/any-singleton" />
    </a>
</div>

---

# Installation

```shell
python -m pip install any-singleton
```

# Documentation

## Usage

### Create a singleton

Register a singleton with a value:
```python
from any_singleton import singleton

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

Instantiate an object and register as a singleton:
```python
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`:
```python
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.

```python
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.

```python
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](https://github.com/Orange23333/any-singleton).
