Giter VIP home page Giter VIP logo

python-registerer's Introduction

Registerer

pypi ci codecov license

Sometimes you may want to use a string to identify a specific function or class. This is a common way of designing your code, but it can be tricky to do it without repeating yourself. Registerer is a tool that helps you to do this easily, and also makes sure that your code is explicit and type safe.

TLDR

Write this:

import registerer

command_handler_registry = registerer.Registerer()


@command_handler_registry.register()
def info(args):
    return "how can i help you?"


@command_handler_registry.register()
def play(args):
    return "let me play a song for you"


command = "info"
args = {}
assert command_handler_registry[command](args) == "how can i help you?"

Instead of this, which violates the Open-Closed Principle (OCP):

def info(args):
    return "how can i help you?"


def play(args):
    return "let me play a song for you"


def command_handler(command, args):
    if command == "info":
        return info(args)
    if command == "play":
        return play(args)


command = "play"
args = {}
assert command_handler(command, args) == "let me play a song for you"

Links

Installation

You can install the latest version of registerer from PyPI:

pip install registerer

Features

  • It's completely type-safe, thus you will get suggestions from your IDE.
  • Writing custom validations for registered items is provided without any inheritance.
  • generate choices for Django from registered items.
  • And so on...

python-registerer's People

Contributors

danialkeimasi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

sobhannavidi

python-registerer's Issues

[FEATURE REQUEST]: Auto import and register

Problem

Having this folder structure:

  • Base.py
  • classes
    • child1.py
    • child2.py

At some point, I have to import every single child in some file to register it, and also I have to decorate it. Sometimes it's hard to remember to do this and add the right properties to the children.

Solution suggestion

registery = registerer.Registerer(
    parent_class=BaseClass,
    autoregister=["./classes/*"],
)

This case is suitable for auto-registering. also If I want to unregister, maybe I could add an unregister property to the child class to avoid getting registered automatically.

# classes/child1.py
from base import BaseClass

class Child(BaseClass):
   slug = "child1"
# classes/child2.py
from base import BaseClass

class Child(BaseClass):
   slug = "child1"
   unregister = True

Register method type hint

Hey,

import registerer

class Human:
    ...

gender_registery = registerer.Registerer(parent_class=Human)

@gender_registery.register("man")
class Man(Human):
    ...

print(gender_registery._registry_dict)

register method is giving me the following type:
(method) register: (*args: Unknown, **kwargs: Unknown) -> (Type[Human] | ((item: Type[Human]) -> T@Registerer))

And says Expected no arguments to "Human" constructor (Because I passed a string to register, it says this)

BTW, it gets registered, but the type hint is not correct.

Python version: 3.9.7

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.