Rewritting Bags from scratch using a namedtuple approach, along with other (less major) updates.
New bag implementation improves a lot how bonobo works, even if this is highly backward incompatible (sorry, that's needed, and better sooner than later). * New implementation uses the same approach as python's namedtuple, by dynamically creating the python type's code. This has drawbacks, as it feels like not the right way, but also a lot of benefits that cannot be achieved using a regular approach, especially the constructor parameter order, hardcoded. * Memory usage is now much more efficient. The "keys" memory space will be used only once per "io type", being spent in the underlying type definition instead of in the actual instances. * Transformations now needs to use tuples as output, which will be bound to its "output type". The output type can be infered from the tuple length, or explicitely set by the user using either `context.set_output_type(...)` or `context.set_output_fields(...)` (to build a bag type from a list of field names). Jupyter/Graphviz integration is more tight, allowing to easily display graphs in a notebook, or displaying the live transformation status in an html table instead of a simple <div>. For now, context processors were hacked to stay working as before but the current API is not satisfactory, and should be replaced. This new big change being unreasonable without some time to work on it properly, it is postponed for next versions (0.7, 0.8, ...). Maybe the best idea is to have some kind of "local services", that would use the same dependency injection mechanism as the execution-wide services. Services are now passed by keywoerd arguments only, to avoid confusion with data-arguments.
This commit is contained in:
@ -1,3 +1,5 @@
|
||||
import pprint
|
||||
|
||||
import pytest
|
||||
|
||||
from bonobo.config.configurables import Configurable
|
||||
|
||||
@ -7,7 +7,7 @@ class MethodBasedConfigurable(Configurable):
|
||||
foo = Option(positional=True)
|
||||
bar = Option()
|
||||
|
||||
def call(self, *args, **kwargs):
|
||||
def __call__(self, *args, **kwargs):
|
||||
self.handler(*args, **kwargs)
|
||||
|
||||
|
||||
|
||||
@ -14,7 +14,7 @@ class Bobby(Configurable):
|
||||
def think(self, context):
|
||||
yield 'different'
|
||||
|
||||
def call(self, think, *args, **kwargs):
|
||||
def __call__(self, think, *args, **kwargs):
|
||||
self.handler('1', *args, **kwargs)
|
||||
self.handler2('2', *args, **kwargs)
|
||||
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
from operator import attrgetter
|
||||
|
||||
from bonobo.config import Configurable
|
||||
from bonobo.config.processors import ContextProcessor, resolve_processors, ContextCurrifier
|
||||
from bonobo.config.processors import ContextProcessor, resolve_processors, ContextCurrifier, use_context_processor
|
||||
|
||||
|
||||
class CP1(Configurable):
|
||||
@ -59,5 +59,16 @@ def test_setup_teardown():
|
||||
o = CP1()
|
||||
stack = ContextCurrifier(o)
|
||||
stack.setup()
|
||||
assert o(*stack.context) == ('this is A', 'THIS IS b')
|
||||
assert o(*stack.args) == ('this is A', 'THIS IS b')
|
||||
stack.teardown()
|
||||
|
||||
|
||||
def test_processors_on_func():
|
||||
def cp(context):
|
||||
yield context
|
||||
|
||||
@use_context_processor(cp)
|
||||
def node(context):
|
||||
pass
|
||||
|
||||
assert get_all_processors_names(node) == ['cp']
|
||||
|
||||
@ -3,9 +3,9 @@ import time
|
||||
|
||||
import pytest
|
||||
|
||||
from bonobo.util import get_name
|
||||
from bonobo.config import Configurable, Container, Exclusive, Service, requires
|
||||
from bonobo.config import Configurable, Container, Exclusive, Service, use
|
||||
from bonobo.config.services import validate_service_name, create_container
|
||||
from bonobo.util import get_name
|
||||
|
||||
|
||||
class PrinterInterface():
|
||||
@ -30,7 +30,7 @@ SERVICES = Container(
|
||||
class MyServiceDependantConfigurable(Configurable):
|
||||
printer = Service(PrinterInterface, )
|
||||
|
||||
def __call__(self, printer: PrinterInterface, *args):
|
||||
def __call__(self, *args, printer: PrinterInterface):
|
||||
return printer.print(*args)
|
||||
|
||||
|
||||
@ -51,15 +51,15 @@ def test_service_name_validator():
|
||||
def test_service_dependency():
|
||||
o = MyServiceDependantConfigurable(printer='printer0')
|
||||
|
||||
assert o(SERVICES.get('printer0'), 'foo', 'bar') == '0;foo;bar'
|
||||
assert o(SERVICES.get('printer1'), 'bar', 'baz') == '1;bar;baz'
|
||||
assert o(*SERVICES.args_for(o), 'foo', 'bar') == '0;foo;bar'
|
||||
assert o('foo', 'bar', printer=SERVICES.get('printer0')) == '0;foo;bar'
|
||||
assert o('bar', 'baz', printer=SERVICES.get('printer1')) == '1;bar;baz'
|
||||
assert o('foo', 'bar', **SERVICES.kwargs_for(o)) == '0;foo;bar'
|
||||
|
||||
|
||||
def test_service_dependency_unavailable():
|
||||
o = MyServiceDependantConfigurable(printer='printer2')
|
||||
with pytest.raises(KeyError):
|
||||
SERVICES.args_for(o)
|
||||
SERVICES.kwargs_for(o)
|
||||
|
||||
|
||||
class VCR:
|
||||
@ -100,13 +100,13 @@ def test_requires():
|
||||
|
||||
services = Container(output=vcr.append)
|
||||
|
||||
@requires('output')
|
||||
@use('output')
|
||||
def append(out, x):
|
||||
out(x)
|
||||
|
||||
svcargs = services.args_for(append)
|
||||
svcargs = services.kwargs_for(append)
|
||||
assert len(svcargs) == 1
|
||||
assert svcargs[0] == vcr.append
|
||||
assert svcargs['output'] == vcr.append
|
||||
|
||||
|
||||
@pytest.mark.parametrize('services', [None, {}])
|
||||
|
||||
Reference in New Issue
Block a user