catalogue: Super lightweight Function Registries

2.0.10 · active · verified Sun Mar 29

catalogue is a tiny, zero-dependencies library designed to simplify the creation of function (or object) registries within your Python code. These registries are particularly useful for making objects easily serializable and customizable, allowing you to reference functions by string identifiers instead of direct object references. Currently at version 2.0.10, it maintains an active release cadence with a focus on Python 3.6+ compatibility.

Warnings

Install

Imports

Quickstart

This example demonstrates how to create a new registry, register functions using the provided decorator, and then retrieve and use those functions by their string identifiers. This pattern enables extensible and serializable configurations for your library.

import catalogue

# YOUR PACKAGE - Define a registry
loaders = catalogue.create("my_package", "loaders")

# USER CODE - Register custom functions
@loaders.register("file_loader")
def load_from_file(path):
    print(f"Loading data from file: {path}")
    return f"Data from {path}"

@loaders.register("db_loader")
def load_from_db(query):
    print(f"Loading data from DB with query: {query}")
    return f"Data for {query}"

# YOUR PACKAGE - Access registered functions
def get_data(loader_id, source):
    loader = loaders.get(loader_id)
    if loader:
        return loader(source)
    else:
        raise ValueError(f"Unknown loader: {loader_id}")

print(get_data("file_loader", "/path/to/data.txt"))
print(get_data("db_loader", "SELECT * FROM users"))

# List all registered loaders
print("All registered loaders:", loaders.get_all())

view raw JSON →