Commit 8566a72b authored by Xavier Barbosa's avatar Xavier Barbosa

mapping interface

parent be40bc5c
Pipeline #1050 failed with stages
in 85 minutes and 16 seconds
......@@ -172,7 +172,7 @@ Closing callback can be registered::
assert foo.closed == True
Partial::
Annotated functions can be rendered partially::
@annotate('foo', 'bar')
def fun(foo, bar):
......@@ -184,5 +184,10 @@ Partial::
'bar': 'I am bar'}
Injector has a mapping interface, which allows to register arbitrary values::
services["foo"] = "yes"
assert await services["foo"] == "yes"
.. _asyncio: https://pypi.python.org/pypi/asyncio
.. _jeni: https://pypi.python.org/pypi/jeni
......@@ -166,6 +166,15 @@ class Injector(metaclass=ABCMeta):
task.add_done_callback(lambda x: future.set_result(x.result()))
return future
def set(self, name: str, value):
self.services[name] = value
def __getitem__(self, name: str):
return self.get(name)
def __setitem__(self, name: str, value):
self.set(name, value)
def apply(self, *args, **kwargs) -> asyncio.Future:
with self.auto():
func, *args = args # type: ignore
......
......@@ -190,3 +190,14 @@ async def test_partial_outsider(services):
partial = services.partial(fun)
assert await partial() == {"foo": "bar"}
@pytest.mark.asyncio
async def test_mapping_interface(services):
services["foo"] = "yes"
services.set("bar", "no")
assert await services.get("foo") == "yes"
assert await services.get("bar") == "no"
assert await services["foo"] == "yes"
assert await services["bar"] == "no"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment