Skip to content

Commit

Permalink
Registry.from_dict() method, closes #17
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Apr 14, 2023
1 parent c6b8245 commit b73eead
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ The HTTP requests to `www.example.com` and `simonwillison.net` will be performed

The library notices that `both()` takes two arguments which are the names of other registered `async def` functions, and will construct an execution plan that executes those two functions in parallel, then passes their results to the `both()` method.

### Registry.from_dict()

Passing a list of functions to the `Registry` constructor will register each function under their introspected function name, using `fn.__name__`.

You can set explicit names instead using a dictionary:

```python
registry = Registry.from_dict({
"example": example,
"simonwillison": simonwillison,
"both": both
})
```
Those string names will be used to match parameters, so each function will need to accept parameters named after the keys used in that dictionary.

### Registering additional functions

Functions that are registered can be regular functions or `async def` functions.
Expand Down
7 changes: 7 additions & 0 deletions asyncinject/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ def __init__(self, *fns, parallel=True, timer=None):
for fn in fns:
self.register(fn)

@classmethod
def from_dict(cls, d, parallel=True, timer=None):
instance = cls(parallel=parallel, timer=timer)
for key, fn in d.items():
instance.register(fn, name=key)
return instance

def register(self, fn, *, name=None):
self._registry[name or fn.__name__] = fn
# Clear caches:
Expand Down
1 change: 0 additions & 1 deletion asyncinject/vendored_graphlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ def done(self, *nodes):
n2i = self._node2info

for node in nodes:

# Check if we know about this node (it was added previously using add()
nodeinfo = n2i.get(node)
if nodeinfo is None:
Expand Down
20 changes: 20 additions & 0 deletions tests/test_asyncinject.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,23 @@ def three(one, two):
assert result == 3

assert {t[0] for t in timed} == {"two", "one", "three"}


@pytest.mark.asyncio
@pytest.mark.parametrize("use_string_name", (True, False))
async def test_registry_from_dict(use_string_name):
async def _one():
return 1

async def _two():
return 2

async def _three(one, two):
return one + two

registry = Registry.from_dict({"one": _one, "two": _two, "three": _three})
if use_string_name:
result = await registry.resolve("three")
else:
result = await registry.resolve(_three)
assert result == 3

0 comments on commit b73eead

Please sign in to comment.