Skip to main content

Automatic registry design-pattern for mapping names to functionality.

Project description

https://raw.githubusercontent.com/BrianPugh/autoregistry/main/assets/logo_400w.png

Python compat PyPi GHA Status Coverage Documentation Status

Documentation: https://autoregistry.readthedocs.io

Source Code: https://github.com/BrianPugh/autoregistry

AutoRegistry

Invoking functions and class-constructors from a string is a common design pattern that AutoRegistry aims to solve. For example, a user might specify a backend of type "sqlite" in a yaml configuration file, for which our program needs to construct the SQLite subclass of our Database class. Classically, you would need to manually create a lookup, mapping the string "sqlite" to the SQLite constructor. With AutoRegistry, the lookup is automatically created for you.

AutoRegistry has a single powerful class Registry that can do the following:

  • Be inherited to automatically register subclasses by their name.

  • Be directly invoked my_registry = Registry() to create a decorator for registering callables like functions.

  • Traverse and automatically create registries for other python libraries.

AutoRegistry is also highly configurable, with features like name-schema-enforcement and name-conversion-rules. Checkout the docs for more information.

Watch AutoRegistry in action!

Installation

AutoRegistry requires Python >=3.8.

python -m pip install autoregistry

Examples

Class Inheritance

Registry adds a dictionary-like interface to class constructors for looking up subclasses.

from abc import abstractmethod
from dataclasses import dataclass
from autoregistry import Registry


@dataclass
class Pokemon(Registry):
    level: int
    hp: int

    @abstractmethod
    def attack(self, target):
        """Attack another Pokemon."""


class Charmander(Pokemon):
    def attack(self, target):
        return 1


class Pikachu(Pokemon):
    def attack(self, target):
        return 2


class SurfingPikachu(Pikachu):
    def attack(self, target):
        return 3


print(f"{len(Pokemon)} Pokemon types registered:")
print(f"    {list(Pokemon)}")
# By default, lookup is case-insensitive
charmander = Pokemon["cHaRmAnDer"](level=7, hp=31)
print(f"Created Pokemon: {charmander}")

This code block produces the following output:

3 Pokemon types registered:
    ['charmander', 'pikachu', 'surfingpikachu']
Created Pokemon: Charmander(level=7, hp=31)

Function Registry

Directly instantiating a Registry object allows you to register functions by decorating them.

from autoregistry import Registry

pokeballs = Registry()


@pokeballs
def masterball(target):
    return 1.0


@pokeballs
def pokeball(target):
    return 0.1


for ball in ["pokeball", "masterball"]:
    success_rate = pokeballs[ball](None)
    print(f"Ash used {ball} and had {success_rate=}")

This code block produces the following output:

Ash used pokeball and had success_rate=0.1
Ash used masterball and had success_rate=1.0

Module Registry

Create a registry for another python module.

import torch
from autoregistry import Registry

optims = Registry(torch.optim)

# "adamw" and ``lr`` could be coming from a configuration file.
optimizer = optims["adamw"](model.parameters(), lr=3e-3)

assert list(optims) == [
    "asgd",
    "adadelta",
    "adagrad",
    "adam",
    "adamw",
    "adamax",
    "lbfgs",
    "nadam",
    "optimizer",
    "radam",
    "rmsprop",
    "rprop",
    "sgd",
    "sparseadam",
    "lr_scheduler",
    "swa_utils",
]

Project Status

This library is feature-complete and accomplishes pretty much everything it was designed to do. So while releases will be more rare/sparse, do not interpret that as this being a “dead” project!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

autoregistry-1.3.1.tar.gz (19.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

autoregistry-1.3.1-py3-none-any.whl (19.0 kB view details)

Uploaded Python 3

File details

Details for the file autoregistry-1.3.1.tar.gz.

File metadata

  • Download URL: autoregistry-1.3.1.tar.gz
  • Upload date:
  • Size: 19.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for autoregistry-1.3.1.tar.gz
Algorithm Hash digest
SHA256 3110c5054fd6fb1f972c7adbfc9df8cfa49ab3ce6c90c0d465625db310a3cb6d
MD5 a9f290d5bf84c52b0f6c2573db750ffa
BLAKE2b-256 b2a73a3abcbe79f1e835436bb72fb3c64d1f9705887cfaafb637cbb9c328bfea

See more details on using hashes here.

Provenance

The following attestation bundles were made for autoregistry-1.3.1.tar.gz:

Publisher: deploy.yaml on BrianPugh/autoregistry

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file autoregistry-1.3.1-py3-none-any.whl.

File metadata

  • Download URL: autoregistry-1.3.1-py3-none-any.whl
  • Upload date:
  • Size: 19.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for autoregistry-1.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8ac1859a140be6a092c1d3be592b6f15c40e9ecdec17932fea4a25f1427fd681
MD5 a5190ce9424931266bf7830805d06e03
BLAKE2b-256 b2f4a3b2ac9c69be875560435fc5d7b9bab427cc239585511b9f55e16ddca82e

See more details on using hashes here.

Provenance

The following attestation bundles were made for autoregistry-1.3.1-py3-none-any.whl:

Publisher: deploy.yaml on BrianPugh/autoregistry

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page