Metadata-Version: 2.4
Name: oncequinox
Version: 0.1.0
Summary: Create singleton Equinox modules.
Project-URL: Bug Tracker, https://github.com/GalacticDynamics/oncequinox/issues
Project-URL: Changelog, https://github.com/GalacticDynamics/oncequinox/releases
Project-URL: Discussions, https://github.com/GalacticDynamics/oncequinox/discussions
Project-URL: Homepage, https://github.com/GalacticDynamics/oncequinox
Author-email: Nathaniel Starkman <nstarman@users.noreply.github.com>
License: Copyright 2024 Nathaniel Starkman
        
        Permission is hereby granted, free of charge, to any person obtaining a copy of
        this software and associated documentation files (the "Software"), to deal in
        the Software without restriction, including without limitation the rights to
        use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
        of the Software, and to permit persons to whom the Software is furnished to do
        so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Classifier: Development Status :: 1 - Planning
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering
Classifier: Typing :: Typed
Requires-Python: >=3.10
Requires-Dist: equinox>=0.12.0
Requires-Dist: jax>=0.4.38
Description-Content-Type: text/markdown

<h1 align='center'> oncequinox </h1>
<h2 align="center">Create singleton Equinox modules.</h2>

This is a micro-package, containing the single metaclass `SingletonModuleMeta`.
</br> `SingletonModuleMeta` can be used to create Equinox modules that are
singletons, meaning that only one instance of the module can exist at any given
time.

## Installation

[![PyPI platforms][pypi-platforms]][pypi-link]
[![PyPI version][pypi-version]][pypi-link]

<!-- [![Conda-Forge][conda-badge]][conda-link] -->

```bash
pip install oncequinox
```

## Documentation

[![Actions Status][actions-badge]][actions-link]

### Basic Usage

Create a singleton Equinox module:

```python
import equinox as eqx
from oncequinox import SingletonModuleMeta


class MySingletonModule(eqx.Module, metaclass=SingletonModuleMeta):
    value: str = "this is a singleton module"


# Create the singleton instance
singleton1 = MySingletonModule()

# Attempt to create another instance
singleton2 = MySingletonModule()

print(singleton1 is singleton2)  # True
```

### Singleton Per Class

Different classes maintain separate singleton instances:

```python
import equinox as eqx
from oncequinox import SingletonModuleMeta


class ConfigA(eqx.Module, metaclass=SingletonModuleMeta):
    name: str = "A"


class ConfigB(eqx.Module, metaclass=SingletonModuleMeta):
    name: str = "B"


config_a = ConfigA()
config_b = ConfigB()

print(config_a is config_b)  # False
print(config_a.name)  # A
print(config_b.name)  # B
```

### With Initialization Arguments

Arguments are only used for the first instantiation:

```python
import equinox as eqx
from oncequinox import SingletonModuleMeta


class HasValue(eqx.Module, metaclass=SingletonModuleMeta):
    value: int

    def __init__(self, value: int):
        self.value = value


v1 = HasValue(10)
print(v1.value)  # 10

# Subsequent calls ignore arguments and return existing instance
v2 = HasValue(20)
print(v2.value)  # 10
print(v1 is v2)  # True
```

<!-- SPHINX-START -->

<!-- prettier-ignore-start -->
[actions-badge]:            https://github.com/GalacticDynamics/oncequinox/workflows/CI/badge.svg
[actions-link]:             https://github.com/GalacticDynamics/oncequinox/actions
[pypi-link]:                https://pypi.org/project/oncequinox/
[pypi-platforms]:           https://img.shields.io/pypi/pyversions/oncequinox
[pypi-version]:             https://img.shields.io/pypi/v/oncequinox

<!-- prettier-ignore-end -->
