Skip to content

Commit

Permalink
flow.RunIf accepts a variable number of arguments instead of one sequ…
Browse files Browse the repository at this point in the history
…ence (which still works). Improve docs, add new Sphinx directives (deprecated and versionadded).
  • Loading branch information
ynikitenko committed Nov 10, 2021
1 parent 1640c59 commit f0b9c5b
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 11 deletions.
5 changes: 5 additions & 0 deletions docs/source/flow.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,14 @@ Flow

Chain
CountFrom
ISlice
Reverse
Slice

**Split into bins:**

Since Lena 0.5 moved to :doc:`structures`.

Elements
--------
Elements form Lena sequences.
Expand Down
1 change: 1 addition & 0 deletions docs/source/output.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Output
PDFToPNG
ToCSV
Write
Writer
.. WriteROOTTree
.. not covered yet: JSONEncoder, ExtendedHistToHist
Expand Down
21 changes: 12 additions & 9 deletions lena/flow/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class RunIf(object):
[
(
select,
seq
Sequence(*args)
),
not_select
# not selected values pass unchanged
Expand All @@ -119,14 +119,17 @@ class RunIf(object):
for more flexibility.
"""

def __init__(self, select, seq):
def __init__(self, select, *args):
"""*select* is a function that accepts a value
(maybe with context) and returns a boolean.
It is converted to a :class:`.Selector`.
See its specifications for available options.
*seq* is a sequence that will be run for selected values.
If it is not a :class:`.Sequence`, it is converted to that.
*args* are an arbitrary number of elements
that will be run for selected values.
They are joined into a :class:`.Sequence`.
.. versionadded:: 0.4
"""
# this element was present in Lena for a long time,
# but it was called TransformIf
Expand All @@ -145,15 +148,15 @@ def __init__(self, select, seq):
else:
self._select = select

if isinstance(seq, lena.core.Sequence):
self._seq = seq
if len(args) == 1 and isinstance(args[0], lena.core.Sequence):
self._seq = args[0]
else:
try:
seq = lena.core.Sequence(seq)
seq = lena.core.Sequence(*args)
except lena.core.LenaTypeError:
raise lena.core.LenaTypeError(
"seq must be a Sequence or convertible to that, "
"{} provided".format(seq)
"args must be a Sequence or convertible to that, "
"{} provided".format(*args)
)
else:
self._seq = seq
Expand Down
2 changes: 2 additions & 0 deletions lena/flow/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ def __init__(self, selector):
sometimes it can be quick and explicit, and if one's
class name provides absolutely no clue what it does,
a general :class:`Filter` would be more readable.
.. versionadded:: 0.4
"""
if not callable(selector):
selector = _Selector(selector)
Expand Down
5 changes: 4 additions & 1 deletion lena/flow/iterators.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ def __call__(self):


def ISlice(*args, **kwargs):
"""Deprecated since Lena 0.4. Use :class:`Slice`."""
"""
.. deprecated:: 0.4
use :class:`Slice`.
"""
warnings.warn("ISlice is deprecated since Lena 0.4. Use Slice. In:",
DeprecationWarning, stacklevel=2)
return Slice(*args, **kwargs)
Expand Down
5 changes: 4 additions & 1 deletion lena/output/write.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@


def Writer(*args, **kwargs):
"""Deprecated since Lena 0.4. Use :class:`Write`."""
"""
.. deprecated:: 0.4
use :class:`Write`.
"""
warnings.warn("Writer is deprecated since Lena 0.4. Use Write. In:",
DeprecationWarning, stacklevel=2)
return Write(*args, **kwargs)
Expand Down
5 changes: 5 additions & 0 deletions tests/flow/test_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,17 @@ def test_count():
def test_run_if():
data = [1, 2.5]
add_1 = lambda num: num + 1
add_2 = lambda num: num + 2
select_all = Selector(lambda _: True)

# select all works
t0 = RunIf(select_all, add_1)
assert list(t0.run(data)) == [2, 3.5]

# several elements work
t0 = RunIf(select_all, add_1, add_2)
assert list(t0.run(data)) == [4, 5.5]

# select none works
# ready Sequence works
t1 = RunIf(lambda _: False, lena.core.Sequence(add_1))
Expand Down

0 comments on commit f0b9c5b

Please sign in to comment.