Description
<a href="https://explosion.ai"><img src="https://explosion.ai/assets/img/logo.svg" width="125" height="125" align="right" /></a> # catalogue: Super lightweight function registries for your library `catalogue` is a tiny, zero-dependencies library that makes it easy to **add function (or object) registries** to your code. Function registries are helpful when you have objects that need to be both easily serializable and fully customizable. Instead of passing a function into your object, you pass in an identifier name, which the object can use to lookup the function from the registry. This makes the object easy to serialize, because the name is a simple string. If you instead saved the function, you'd have to use Pickle for serialization, which has many drawbacks. [](https://github.com/explosion/catalogue/actions/workflows/tests.yml) [](https://github.com/explosion/catalogue/releases) [](https://pypi.org/project/catalogue/) [](https://anaconda.org/conda-forge/catalogue) [](https://github.com/ambv/black) ## ⏳ Installation ```bash pip install catalogue ``` ```bash conda install -c conda-forge catalogue ``` > ⚠️ **Important note:** `catalogue` v2.0+ is only compatible with Python 3.6+. > For Python 2.7+ compatibility, use `catalogue` v1.x. ## 👩💻 Usage Let's imagine you're developing a Python package that needs to load data somewhere. You've already implemented some loader functions for the most common data types, but you want to allow the user to easily add their own. Using `catalogue.create` you can create a new registry under the namespace `your_package` → `loaders`. ```python # YOUR PACKAGE import catalogue loaders = catalogue.create("your_package", "loaders") ``` This gives you a `loaders.register` decorator that your users can import and decorate their custom loader functions with. ```python # USER CODE from your_package import loaders @loaders.register("custom_loader") def custom_loader(data): # Load something here... return data ``` The decorated function will be registered automatically and in your package, you'll be able to access all loaders by calling `loaders.get_all`. ```python # YOUR PACKAGE def load_data(data, loader_id): print("All loaders:", loaders.get_all()) # {"custom_loader": <custom_loader>} loader = loaders.get(loader_id) return loader(data) ``` The user can now refer to their custom loader using only its string name (`"custom_loader"`) and your application will know what to do and will use their custom function. ```python # USER CODE from your_package import load_data load_data(data, loader_id="custom_loader") ``` ## ❓ FAQ #### But can't the user just pass in the `custom_loader` function directly? Sure, that's the more classic callback approach. Instead of a string ID, `load_data` could also take a function, in which case you wouldn't need a package like this. `catalogue` helps you when you need to produce a serializable record of which functions were passed in. For instance, you might want to write a log message, or save a config to load back your object later. With `catalogue`, your functions can be parameterized by strings, so logging and serialization remains easy – while still giving you full extensibility. #### How do I make sure all of the registration decorators have run? Decorators normally run when modules are imported. Relying on this side-effect can sometimes lead to confusion, especially if there's no other reason the module would be imported. One solution is to use [entry points](https://packaging.python.org/specifications/entry-points/). For instance, in [spaCy](https://spacy.io) we're starting to use function registries to make the pipeline components much more customizable. Let's say one user, Jo, develops a better tagging model using new machine learning research. End-users of Jo's package should be able to write `spacy.load("jo_tagging_model")`. They shouldn't need to remember to write `import jos_tagged_model` first, just to run the function registries as a side-effect. With entry points, the registration happens at install time – so you don't need to rely on the import side-effects. ## 🎛 API ### <kbd>function</kbd> `catalogue.create` Create a new registry for a given namespace. Returns a setter function that can be used as a decorator or called with a name and `func` keyword argument. If `entry_points=True` is set, the registry will check for [Python entry points](https
Release History
| Version | Changes | Urgency | Date |
|---|---|---|---|
| 2.0.10 | Imported from PyPI (2.0.10) | Low | 4/21/2026 |
| v2.0.10 | - Package and test suite updates for Python 3.12 (#66). | Low | 9/25/2023 |
| v2.0.9 | - Update for compatibility with future Python 3.12 (#62). | Low | 7/24/2023 |
| v1.0.2 | * Require Python 3.6+. | Low | 10/18/2022 |
| v1.0.1 | > **Note:** v1.0.1 was deleted from PyPI because it was incompatible with python 2.7, but the package setup did not specify this correctly and yanked packages are not supported by the latest `pip` for python 2.7. The v1.0.2 release is identical to v1.0.1 except for the corrected `python_requires>=3.6`. * Add vendored `importlib_metadata` v3.2.0 for use with Python 3.6-3.7 to avoid breaking changes in newer releases and to avoid conflicts with `importlib_metadata` requirements in other package | Low | 10/7/2022 |
| v2.0.8 | * Improve future compatibility with `importlib.metadata` for python 3.10+. * Update tests for compatibility with python 3.10+. | Low | 7/26/2022 |
| v2.0.7 | * Modify test suite for python 3.10 until `importlib.metadata` differences can be addressed. | Low | 3/22/2022 |
| v2.0.6 | * Prevent `Registry.find` from failing on Cython modules. | Low | 8/21/2021 |
| v2.0.5 | * Backport `packages_distributions` from `importlib_metadata`. | Low | 8/13/2021 |
| v2.0.4 | * Fix conflicts between `catalogue` and `importlib-metadata` | Low | 4/30/2021 |
| v2.0.3 | * Add vendored `importlib-metadata` v3.2.0 to avoid conflicts with `importlib-metadata` requirements in other packages | Low | 4/30/2021 |
| v2.0.2 | * Restrict `importlib-metadata` to `<3.3.0` due to reports of performance regressions and issues with the test suite | Low | 4/14/2021 |
| v2.0.1 | Release v2.0.1 | Low | 8/27/2020 |
| v2.0.0 | Release v2.0.0 | Low | 1/10/2020 |
| v1.0.0 | Release v1.0.0 | Low | 1/9/2020 |
| v0.2.1 | Release v0.2.1 | Low | 1/8/2020 |
| v0.2.0 | Release v0.2.0 | Low | 12/26/2019 |
| v0.0.8 | Release v0.0.8 | Low | 11/4/2019 |
| v0.0.7 | Release v0.0.7 | Low | 11/4/2019 |
| v0.0.6 | Release v0.0.6 | Low | 11/4/2019 |
| v0.0.5 | Release v0.0.5 | Low | 11/4/2019 |
| v0.0.4 | Release v0.0.4 | Low | 11/2/2019 |
| v0.0.3 | Release v0.0.3 | Low | 11/1/2019 |
| v0.0.2 | Release v0.0.2 | Low | 11/1/2019 |
