-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add flow.Filter (with tests and extensive docs). Update documentation…
… for newer Sphinx.
- Loading branch information
1 parent
365c8bd
commit e934e77
Showing
8 changed files
with
115 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from .selectors import Selector as _Selector | ||
|
||
|
||
class Filter(): | ||
"""Filter values from flow.""" | ||
|
||
def __init__(self, selector): | ||
"""*selector* is a boolean function. | ||
If it returns ``True``, the value passes :class:`Filter`. | ||
If *selector* is not callable, it is converted to | ||
a :class:`.Selector`. | ||
If the conversion could not be done, | ||
:exc:`.LenaTypeError` is raised. | ||
Note | ||
---- | ||
:class:`Filter` appeared in Lena only in version 0.4. | ||
There may be better alternatives to using this element: | ||
- don't produce values that you will discard later. | ||
If you want to select data from a specific file, | ||
read only that file. | ||
- use a custom class. *SelectPosition("border")* is more | ||
readable and maintainable than a :class:`Filter` | ||
with many conditions, and it is also more *cohesive* | ||
if you group several options | ||
like "center" or "top" in a single place. | ||
If you make a selection, it can be useful | ||
to add information about that to the *context* | ||
(and :class:`Filter` does not do that). | ||
This doesn't mean that we recommend against this class: | ||
sometimes it can be quick and useful, and if one's | ||
class name provides absolutely no clue what it does, | ||
a general :class:`Filter` would be more readable. | ||
""" | ||
if not callable(selector): | ||
selector = _Selector(selector) | ||
self._selector = selector | ||
|
||
def fill_into(self, element, value): | ||
"""Fill *value* into an *element* if | ||
*selector(value)* is ``True``. | ||
*Element* must have a *fill(value)* method. | ||
""" | ||
if self._selector(value): | ||
element.fill(value) | ||
|
||
def run(self, flow): | ||
"""Yield values from the *flow* for which | ||
the *selector* is ``True``. | ||
""" | ||
return (val for val in flow if self._selector(val)) | ||
# or | ||
# for val in flow: | ||
# if self._selector(val): | ||
# yield event |
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,35 @@ | ||
import pytest | ||
import lena | ||
|
||
from lena.flow import Filter, Selector | ||
|
||
from tests.examples.fill import StoreFilled | ||
|
||
|
||
def test_filter(): | ||
# initialization argument must be callable, a Selector, | ||
# or should be able to be converted to a Selector | ||
with pytest.raises(lena.core.LenaTypeError): | ||
Filter(1) | ||
|
||
# filter with a selector works | ||
s = Selector([int, str]) | ||
f0 = Filter(s) | ||
|
||
## Test run | ||
f1 = Filter([int, str]) | ||
data = [1, 2, "s", [], (), (3, {})] | ||
res_data = [1, 2, "s", (3, {})] | ||
assert list(f0.run(data)) == res_data # list(f1.run(data)) | ||
assert list(f1.run(data)) == [1, 2, "s", (3, {})] | ||
|
||
# filter with a function works | ||
fun = lambda val: val | ||
f2 = Filter(fun) | ||
assert list(f2.run(data + [0, 4])) == [1, 2, "s", (3, {}), 4] | ||
|
||
## Test fill_into | ||
sf1 = StoreFilled() | ||
for val in data: | ||
f1.fill_into(sf1, val) | ||
assert sf1.list == res_data |
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