explosion / explosion/catalogue

suggestion: dry registrations for us lazy registrars

Open
#6 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
183
Forks
21
PR merge metrics
No merged PRs in 30d

Description

Thanks for building this.

Would it be easy to allow the `__name__` of the callable being passed in as the default, but allowing a `name` kwarg to overwrite it? I started going down a [decorator rabbit-hole on stack overflow](https://stackoverflow.com/questions/653368/how-to-create-a-python-decorator-that-can-be-used-either-with-or-without-paramet) to try and pitch in a solution, but got lost in decorator hell.

Using ```loaders = catalogue.create("mypackage", "loaders")``` as our shared example, here's what things look like at the moment:
```
#passing the name in explicitly
@loaders.register(name='custom_func')
def custom_func(data):
pass
```
vs.
```
#letting the func name itself
@loaders.register
def custom_func(data):
pass
```
vs.
```
#a cool shorthand version
@loaders
def custom_func(data):
pass
```

This was my attempt, but it doesn't accept passing in the name kwarg.

```
class Registry:
callables = {}

def __call__(self, func, name=None):
if not name: name = func.__name__
self.callables[name] = func

def __contains__(self, func):
return func in self.callables

def __repr__(self):
return f"{self.callables}"

loaders = Registry()

#this works fine
@loaders
def custom_func(data):
pass

#this not so much
@loaders(name='blah')
def custom_func(data):
pass
```

What do you think?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.