Skip to content

Commit

Permalink
Add representations for core.FillInto, variables.Variable, flow.{Slic…
Browse files Browse the repository at this point in the history
…e,Reverse,CountFrom}, improve and develop tests.
  • Loading branch information
ynikitenko committed Jul 3, 2023
1 parent ec3937e commit 79f3f93
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 23 deletions.
7 changes: 7 additions & 0 deletions lena/core/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ def __init__(self, el, fill_into=_SENTINEL, explicit=True):
format(fill_into, el)
)
self._el = el
self._explicit = explicit

def fill_into(self, element, value):
"""Fill *value* into an *element*.
Expand All @@ -210,6 +211,12 @@ def _run_fill_into(self, element, value):
for result in self._el.run([value]):
element.fill(result)

def __repr__(self):
if self._explicit:
return "FillInto({})".format(repr(self._el))
else:
return repr(self._el)


class FillRequest(object):
"""
Expand Down
13 changes: 13 additions & 0 deletions lena/flow/iterators.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,17 @@ class CountFrom(object):
"""
def __init__(self, start=0, step=1):
self._it = itertools.count(start, step)
self._start = start
self._step = step

def __call__(self):
"""Yield values from *start* to infinity with *step*."""
for val in self._it:
yield val

def __repr__(self):
return "CountFrom(start={}, step={})".format(self._start, self._step)


def ISlice(*args, **kwargs):
"""
Expand Down Expand Up @@ -83,6 +88,9 @@ def run(self, flow):
except IndexError:
return

def __repr__(self):
return "Reverse()"


class Slice(object):
"""Slice data flow from *start* to *stop* with *step*."""
Expand Down Expand Up @@ -156,6 +164,8 @@ def __init__(self, *args):
else:
self.run = lambda flow: self._run_negative_islice(flow)
self._step = step
# for repr
self._args = args

def fill_into(self, element, value):
"""Fill *element* with *value*.
Expand Down Expand Up @@ -261,6 +271,9 @@ def fill_deque(flow, maxlen):
return
ind += 1

def __repr__(self):
args_str = ", ".join((repr(arg) for arg in self._args))
return "Slice({})".format(args_str)

def run(self, flow):
"""Yield values from *flow* from *start* to *stop* with *step*.
Expand Down
9 changes: 9 additions & 0 deletions lena/variables/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,15 @@ def _update_context(context, var_context):
# https://en.wikipedia.org/wiki/Command%E2%80%93query_separation
# return context

def __repr__(self):
# We don't print all options to save space.
# Complete getter address is meaningless.
# This is repr, one can always get these options
# directly if needed.
# We don't enclose name in quotes,
# because this repr doesn't allow object creation.
return """Variable({})""".format(self.name)


class Combine(Variable):
r"""Combine variables into a tuple.
Expand Down
41 changes: 41 additions & 0 deletions tests/core/test_lena_sequence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import pytest

from lena.core import Source, Sequence
from lena.flow import Count, Slice

from tests.shortcuts import cnt0


def test_repr_sequence():
# zero-element sequence works
s01 = Sequence()
assert repr(s01) == "Sequence()"

# one-element sequence works
s02 = Sequence(Sequence())
assert repr(s02) == "Sequence(\n Sequence()\n)"

# nesting works
s03 = Sequence(Sequence(Sequence()))
# print(s03)
assert repr(s03) == "Sequence(\n Sequence(\n Sequence()\n )\n)"


def test_repr_source():
# not Lena elements can also be represented
cnt = cnt0
slice1 = Slice(1)

# representation with one element works
s1 = Source(cnt)
mnl = "\n"
# print(s1)
assert repr(s1) == "".join(["Source(", mnl,
" "*4, repr(cnt), mnl, ")"])

# representation with two elements works
s2 = Source(cnt, slice1)
# print(s2)
assert repr(s2) == "".join(["Source(", mnl,
" "*4, repr(cnt), ",", mnl,
" "*4, repr(slice1), mnl, ")"])
32 changes: 19 additions & 13 deletions tests/flow/test_iterators.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import print_function

import sys
from itertools import count, islice

Expand Down Expand Up @@ -55,21 +53,29 @@ def test_count_from():
assert list(islice(it(), 5)) == list(range(10, 20, 2))


def test_islice_run():
def test_slice_run():
c = count()
### test __init__ and run ###
## __init__ and run work
# stop works
isl = Slice(10)
list(isl.run(c)) == list(range(0, 10))
# start, stop works
isl = Slice(10, 10)
list(isl.run(c)) == []
sl1 = Slice(10)
assert repr(sl1) == "Slice(10)"
list(sl1.run(c)) == list(range(0, 10))

## start, stop work
# empty slice works
sl2 = Slice(10, 10)
assert repr(sl2) == "Slice(10, 10)"
list(sl2.run(c)) == []

c = count()
isl = Slice(10, 15)
list(isl.run(c)) == list(range(10, 15))
# non-empty slice works
sl3 = Slice(10, 15)
list(sl3.run(c)) == list(range(10, 15))

# start, stop, step work
isl = Slice(0, 10, 2)
list(isl.run(count())) == list(range(0, 10, 2))
sl4 = Slice(0, 10, 2)
assert repr(sl4) == "Slice(0, 10, 2)"
list(sl4.run(count())) == list(range(0, 10, 2))


def test_negative_islice():
Expand Down
27 changes: 17 additions & 10 deletions tests/variables/test_variable.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import print_function

import copy
import pytest

Expand Down Expand Up @@ -184,20 +182,29 @@ def test_compose():


def test_variable():
# test init
sq_m = Variable("sq", getter = lambda x: x*x, type="function")
# getter is None
## test initialization
# init works
sq_m = Variable("sq_m", getter = lambda x: x*x, type="function")

# repr works
assert repr(sq_m) == "Variable(sq_m)"

# getter=None raises
with pytest.raises(LenaTypeError):
sq_m = Variable("sq", getter=None, type="function")
# getter is not callable

# not callable getter raises
with pytest.raises(LenaTypeError):
sq_m = Variable("sq", getter=5, type="function")
# getter must be not a variable

# getter Variable raises
with pytest.raises(LenaTypeError):
Variable(sq_m, getter=sq_m)
mm = Variable("mm", unit="mm", getter=lambda x: x*10, type="length")

## test actual work
mm = Variable("mm", unit="mm", getter=lambda x: x*10, type="length")
sq_m = Variable("sq", getter = lambda x: x*x, type="function")

data = [1, 2, 3]
data = list(map(lambda v: (v, {str(v): v}), data))
results = map(sq_m, copy.deepcopy(data))
Expand All @@ -214,7 +221,7 @@ def test_variable():
}
)

# test Compose
# Compose works
mm = Variable("mm", unit="mm", getter=lambda x: x*10, type="length")
square = Variable("square", type="area", getter=lambda x: x*x, unit="mm^2")
sq_mm = Compose(mm, square)
Expand All @@ -241,7 +248,7 @@ def test_variable():
}
)

# test Combine
# Combine works
sq_m_mm = Combine(sq_m, sq_mm, name="sq_m_mm")
assert list(map(get_data, map(sq_m_mm, copy.deepcopy(data)))) == [
(1, 100), (4, 400), (9, 900)
Expand Down

0 comments on commit 79f3f93

Please sign in to comment.