BrianPugh / autoregistry

Automatic registry design-pattern library for mapping names to functionality.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

image

Python compat PyPi GHA Status Coverage Documentation Status

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.

Examples

Class Inheritance

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

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.

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.

About

Automatic registry design-pattern library for mapping names to functionality.

License:Apache License 2.0


Languages

Language:Python 100.0%