https://github.com/brianpugh/autoregistry

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

https://github.com/brianpugh/autoregistry

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (11.5%) to scientific vocabulary

Keywords

abc abstract automatic config configuration design interface introspection mapping metaclass pattern reflection registry

Keywords from Contributors

labels actions
Last synced: 5 months ago · JSON representation

Repository

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

Basic Info
  • Host: GitHub
  • Owner: BrianPugh
  • License: apache-2.0
  • Language: Python
  • Default Branch: main
  • Homepage:
  • Size: 272 KB
Statistics
  • Stars: 49
  • Watchers: 2
  • Forks: 2
  • Open Issues: 0
  • Releases: 27
Topics
abc abstract automatic config configuration design interface introspection mapping metaclass pattern reflection registry
Created almost 4 years ago · Last pushed 9 months ago
Metadata Files
Readme Funding License

README.rst

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

|Python compat| |PyPi| |GHA tests| |Codecov report| |readthedocs|

.. inclusion-marker-do-not-remove

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.

.. inclusion-marker-remove

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``.

.. code-block:: bash

   python -m pip install autoregistry


Examples
========

Class Inheritance
^^^^^^^^^^^^^^^^^

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

.. code-block:: python

   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:

.. code-block::

   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.

.. code-block:: python

   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:

.. code-block:: text

   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.

.. code-block:: python

   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!


.. |GHA tests| image:: https://github.com/BrianPugh/autoregistry/workflows/tests/badge.svg
   :target: https://github.com/BrianPugh/autoregistry/actions?query=workflow%3Atests
   :alt: GHA Status
.. |Codecov report| image:: https://codecov.io/github/BrianPugh/autoregistry/coverage.svg?branch=main
   :target: https://codecov.io/github/BrianPugh/autoregistry?branch=main
   :alt: Coverage
.. |readthedocs| image:: https://readthedocs.org/projects/autoregistry/badge/?version=latest
        :target: https://autoregistry.readthedocs.io/en/latest/?badge=latest
        :alt: Documentation Status
.. |Python compat| image:: https://img.shields.io/badge/>=python-3.8-blue.svg
.. |PyPi| image:: https://img.shields.io/pypi/v/autoregistry.svg
        :target: https://pypi.python.org/pypi/autoregistry

Owner

  • Name: Brian Pugh
  • Login: BrianPugh
  • Kind: user
  • Location: Washington D.C.

Deep Learning Scientist and blockchain enthusiast

GitHub Events

Total
  • Watch event: 6
  • Delete event: 4
  • Issue comment event: 9
  • Push event: 6
  • Pull request event: 7
  • Create event: 5
Last Year
  • Watch event: 6
  • Delete event: 4
  • Issue comment event: 9
  • Push event: 6
  • Pull request event: 7
  • Create event: 5

Committers

Last synced: 5 months ago

All Time
  • Total Commits: 203
  • Total Committers: 2
  • Avg Commits per committer: 101.5
  • Development Distribution Score (DDS): 0.064
Past Year
  • Commits: 8
  • Committers: 2
  • Avg Commits per committer: 4.0
  • Development Distribution Score (DDS): 0.375
Top Committers
Name Email Commits
Brian Pugh b****7@g****m 190
dependabot[bot] 4****]@u****m 13

Issues and Pull Requests

Last synced: 6 months ago

All Time
  • Total issues: 0
  • Total pull requests: 63
  • Average time to close issues: N/A
  • Average time to close pull requests: 4 days
  • Total issue authors: 0
  • Total pull request authors: 2
  • Average comments per issue: 0
  • Average comments per pull request: 0.9
  • Merged pull requests: 50
  • Bot issues: 0
  • Bot pull requests: 23
Past Year
  • Issues: 0
  • Pull requests: 10
  • Average time to close issues: N/A
  • Average time to close pull requests: 11 days
  • Issue authors: 0
  • Pull request authors: 2
  • Average comments per issue: 0
  • Average comments per pull request: 1.3
  • Merged pull requests: 4
  • Bot issues: 0
  • Bot pull requests: 8
Top Authors
Issue Authors
  • dependabot[bot] (1)
Pull Request Authors
  • BrianPugh (43)
  • dependabot[bot] (29)
Top Labels
Issue Labels
dependencies (1)
Pull Request Labels
dependencies (28) python (2)

Packages

  • Total packages: 1
  • Total downloads:
    • pypi 7,965 last-month
  • Total dependent packages: 5
  • Total dependent repositories: 1
  • Total versions: 28
  • Total maintainers: 1
pypi.org: autoregistry

Automatic registry design-pattern for mapping names to functionality.

  • Versions: 28
  • Dependent Packages: 5
  • Dependent Repositories: 1
  • Downloads: 7,965 Last month
Rankings
Dependent packages count: 1.9%
Downloads: 5.5%
Stargazers count: 12.0%
Average: 12.7%
Dependent repos count: 21.6%
Forks count: 22.6%
Maintainers (1)
Last synced: 6 months ago

Dependencies

pyproject.toml pypi
  • python ^3.8
.github/workflows/deploy.yaml actions
  • JRubics/poetry-publish v1.13 composite
  • actions/checkout v3 composite
.github/workflows/tests.yaml actions
  • actions/cache v2 composite
  • actions/checkout v3 composite
  • actions/setup-python v3 composite
  • codecov/codecov-action v2 composite
  • pre-commit/action v2.0.3 composite
  • snok/install-poetry v1 composite