{"id":16220210,"url":"https://github.com/brianpugh/autoregistry","last_synced_at":"2025-04-06T13:11:48.609Z","repository":{"id":37024527,"uuid":"492538494","full_name":"BrianPugh/autoregistry","owner":"BrianPugh","description":"Automatic registry design-pattern library for mapping string names to code functionality.","archived":false,"fork":false,"pushed_at":"2025-03-17T02:32:18.000Z","size":273,"stargazers_count":44,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-30T12:07:51.388Z","etag":null,"topics":["abc","abstract","automatic","config","configuration","design","interface","introspection","mapping","metaclass","pattern","reflection","registry"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/BrianPugh.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["BrianPugh"],"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"lfx_crowdfunding":null,"custom":null}},"created_at":"2022-05-15T16:15:33.000Z","updated_at":"2025-03-17T02:32:20.000Z","dependencies_parsed_at":"2023-02-11T23:01:38.425Z","dependency_job_id":"1f29752c-4d0e-4308-8f5d-3171e546927a","html_url":"https://github.com/BrianPugh/autoregistry","commit_stats":null,"previous_names":[],"tags_count":27,"template":false,"template_full_name":"BrianPugh/python-template","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BrianPugh%2Fautoregistry","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BrianPugh%2Fautoregistry/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BrianPugh%2Fautoregistry/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BrianPugh%2Fautoregistry/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/BrianPugh","download_url":"https://codeload.github.com/BrianPugh/autoregistry/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247485290,"owners_count":20946398,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["abc","abstract","automatic","config","configuration","design","interface","introspection","mapping","metaclass","pattern","reflection","registry"],"created_at":"2024-10-10T11:58:09.213Z","updated_at":"2025-04-06T13:11:48.586Z","avatar_url":"https://github.com/BrianPugh.png","language":"Python","funding_links":["https://github.com/sponsors/BrianPugh"],"categories":[],"sub_categories":[],"readme":".. image:: https://raw.githubusercontent.com/BrianPugh/autoregistry/main/assets/logo_400w.png\n\n|Python compat| |PyPi| |GHA tests| |Codecov report| |readthedocs|\n\n.. inclusion-marker-do-not-remove\n\nAutoRegistry\n============\n\nInvoking functions and class-constructors from a string is a common design pattern\nthat AutoRegistry aims to solve.\nFor example, a user might specify a backend of type ``\"sqlite\"`` in a yaml configuration\nfile, for which our program needs to construct the ``SQLite`` subclass of our ``Database`` class.\nClassically, you would need to manually create a lookup, mapping the string ``\"sqlite\"`` to\nthe ``SQLite`` constructor.\nWith AutoRegistry, the lookup is automatically created for you.\n\n\nAutoRegistry has a single  powerful class ``Registry`` that can do the following:\n\n* Be inherited to automatically register subclasses by their name.\n\n* Be directly invoked ``my_registry = Registry()`` to create a decorator\n  for registering callables like functions.\n\n* Traverse and automatically create registries for other python libraries.\n\n.. inclusion-marker-remove\n\nAutoRegistry is also highly configurable, with features like name-schema-enforcement and name-conversion-rules.\n`Checkout the docs for more information \u003chttps://autoregistry.readthedocs.io/en/latest/?badge=latest/\u003e`_.\n\n`Watch AutoRegistry in action! \u003chttps://youtu.be/4No_NE7bUOM\u003e`_\n\nInstallation\n============\nAutoRegistry requires Python ``\u003e=3.8``.\n\n.. code-block:: bash\n\n   python -m pip install autoregistry\n\n\nExamples\n========\n\nClass Inheritance\n^^^^^^^^^^^^^^^^^\n\n``Registry`` adds a dictionary-like interface to class constructors\nfor looking up subclasses.\n\n.. code-block:: python\n\n   from abc import abstractmethod\n   from dataclasses import dataclass\n   from autoregistry import Registry\n\n\n   @dataclass\n   class Pokemon(Registry):\n       level: int\n       hp: int\n\n       @abstractmethod\n       def attack(self, target):\n           \"\"\"Attack another Pokemon.\"\"\"\n\n\n   class Charmander(Pokemon):\n       def attack(self, target):\n           return 1\n\n\n   class Pikachu(Pokemon):\n       def attack(self, target):\n           return 2\n\n\n   class SurfingPikachu(Pikachu):\n       def attack(self, target):\n           return 3\n\n\n   print(f\"{len(Pokemon)} Pokemon types registered:\")\n   print(f\"    {list(Pokemon)}\")\n   # By default, lookup is case-insensitive\n   charmander = Pokemon[\"cHaRmAnDer\"](level=7, hp=31)\n   print(f\"Created Pokemon: {charmander}\")\n\nThis code block produces the following output:\n\n.. code-block::\n\n   3 Pokemon types registered:\n       ['charmander', 'pikachu', 'surfingpikachu']\n   Created Pokemon: Charmander(level=7, hp=31)\n\n\nFunction Registry\n^^^^^^^^^^^^^^^^^\n\nDirectly instantiating a ``Registry`` object allows you to\nregister functions by decorating them.\n\n.. code-block:: python\n\n   from autoregistry import Registry\n\n   pokeballs = Registry()\n\n\n   @pokeballs\n   def masterball(target):\n       return 1.0\n\n\n   @pokeballs\n   def pokeball(target):\n       return 0.1\n\n\n   for ball in [\"pokeball\", \"masterball\"]:\n       success_rate = pokeballs[ball](None)\n       print(f\"Ash used {ball} and had {success_rate=}\")\n\nThis code block produces the following output:\n\n.. code-block:: text\n\n   Ash used pokeball and had success_rate=0.1\n   Ash used masterball and had success_rate=1.0\n\n\nModule Registry\n^^^^^^^^^^^^^^^\n\nCreate a registry for another python module.\n\n.. code-block:: python\n\n   import torch\n   from autoregistry import Registry\n\n   optims = Registry(torch.optim)\n\n   # \"adamw\" and ``lr`` could be coming from a configuration file.\n   optimizer = optims[\"adamw\"](model.parameters(), lr=3e-3)\n\n   assert list(optims) == [\n       \"asgd\",\n       \"adadelta\",\n       \"adagrad\",\n       \"adam\",\n       \"adamw\",\n       \"adamax\",\n       \"lbfgs\",\n       \"nadam\",\n       \"optimizer\",\n       \"radam\",\n       \"rmsprop\",\n       \"rprop\",\n       \"sgd\",\n       \"sparseadam\",\n       \"lr_scheduler\",\n       \"swa_utils\",\n   ]\n\n\n.. |GHA tests| image:: https://github.com/BrianPugh/autoregistry/workflows/tests/badge.svg\n   :target: https://github.com/BrianPugh/autoregistry/actions?query=workflow%3Atests\n   :alt: GHA Status\n.. |Codecov report| image:: https://codecov.io/github/BrianPugh/autoregistry/coverage.svg?branch=main\n   :target: https://codecov.io/github/BrianPugh/autoregistry?branch=main\n   :alt: Coverage\n.. |readthedocs| image:: https://readthedocs.org/projects/autoregistry/badge/?version=latest\n        :target: https://autoregistry.readthedocs.io/en/latest/?badge=latest\n        :alt: Documentation Status\n.. |Python compat| image:: https://img.shields.io/badge/\u003e=python-3.8-blue.svg\n.. |PyPi| image:: https://img.shields.io/pypi/v/autoregistry.svg\n        :target: https://pypi.python.org/pypi/autoregistry\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrianpugh%2Fautoregistry","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbrianpugh%2Fautoregistry","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrianpugh%2Fautoregistry/lists"}