-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds method based options, limited to one, to allow nodes based on a …
…specific method (think filter, join, etc)... Reimplementation of "Filter", with (rather simple) code from rdc.etl.
- Loading branch information
Showing
10 changed files
with
181 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import bonobo | ||
|
||
from bonobo.filter import Filter | ||
|
||
|
||
class OddOnlyFilter(Filter): | ||
def filter(self, i): | ||
return i % 2 | ||
|
||
|
||
@Filter | ||
def MultiplesOfThreeOnlyFilter(self, i): | ||
return not (i % 3) | ||
|
||
|
||
graph = bonobo.Graph( | ||
lambda: tuple(range(50)), | ||
OddOnlyFilter(), | ||
MultiplesOfThreeOnlyFilter(), | ||
print, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from bonobo.constants import NOT_MODIFIED | ||
|
||
from bonobo.config import Configurable, Method | ||
|
||
|
||
class Filter(Configurable): | ||
"""Filter out hashes from the stream depending on the :attr:`filter` callable return value, when called with the | ||
current hash as parameter. | ||
Can be used as a decorator on a filter callable. | ||
.. attribute:: filter | ||
A callable used to filter lines. | ||
If the callable returns a true-ish value, the input will be passed unmodified to the next items. | ||
Otherwise, it'll be burnt. | ||
""" | ||
|
||
filter = Method() | ||
|
||
def call(self, *args, **kwargs): | ||
if self.filter(*args, **kwargs): | ||
return NOT_MODIFIED | ||
|
||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import pytest | ||
|
||
from bonobo.config import Configurable, Method, Option | ||
from bonobo.errors import ConfigurationError | ||
|
||
|
||
class MethodBasedConfigurable(Configurable): | ||
handler = Method() | ||
foo = Option(positional=True) | ||
bar = Option() | ||
|
||
def call(self, *args, **kwargs): | ||
self.handler(*args, **kwargs) | ||
|
||
|
||
def test_one_wrapper_only(): | ||
with pytest.raises(ConfigurationError): | ||
class TwoMethods(Configurable): | ||
h1 = Method() | ||
h2 = Method() | ||
|
||
|
||
def test_define_with_decorator(): | ||
calls = [] | ||
|
||
@MethodBasedConfigurable | ||
def Concrete(self, *args, **kwargs): | ||
calls.append((args, kwargs,)) | ||
|
||
t = Concrete('foo', bar='baz') | ||
assert len(calls) == 0 | ||
t() | ||
assert len(calls) == 1 | ||
|
||
|
||
def test_define_with_argument(): | ||
calls = [] | ||
|
||
def concrete_handler(*args, **kwargs): | ||
calls.append((args, kwargs,)) | ||
|
||
t = MethodBasedConfigurable('foo', bar='baz', handler=concrete_handler) | ||
assert len(calls) == 0 | ||
t() | ||
assert len(calls) == 1 | ||
|
||
def test_define_with_inheritance(): | ||
calls = [] | ||
|
||
class Inheriting(MethodBasedConfigurable): | ||
def handler(self, *args, **kwargs): | ||
calls.append((args, kwargs,)) | ||
|
||
t = Inheriting('foo', bar='baz') | ||
assert len(calls) == 0 | ||
t() | ||
assert len(calls) == 1 | ||
|
||
|
||
def test_inheritance_then_decorate(): | ||
calls = [] | ||
|
||
class Inheriting(MethodBasedConfigurable): | ||
pass | ||
|
||
@Inheriting | ||
def Concrete(self, *args, **kwargs): | ||
calls.append((args, kwargs,)) | ||
|
||
t = Concrete('foo', bar='baz') | ||
assert len(calls) == 0 | ||
t() | ||
assert len(calls) == 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters