https://github.com/galacticdynamics/oncequinox
micro-library for making Equinox singletons
https://github.com/galacticdynamics/oncequinox
equinox jax singleton singleton-pattern
Last synced: 8 months ago
JSON representation
micro-library for making Equinox singletons
- Host: GitHub
- URL: https://github.com/galacticdynamics/oncequinox
- Owner: GalacticDynamics
- License: mit
- Created: 2025-10-26T22:13:00.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2025-10-27T00:17:24.000Z (8 months ago)
- Last Synced: 2025-10-27T01:11:32.593Z (8 months ago)
- Topics: equinox, jax, singleton, singleton-pattern
- Language: Python
- Homepage:
- Size: 135 KB
- Stars: 1
- Watchers: 0
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
oncequinox
Create singleton Equinox modules.
This is a micro-package, containing the single metaclass `SingletonModuleMeta`.
`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]
```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
```
[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