Skip to content

Commit

Permalink
Rename _seq of Lena sequences to _data_seq. Rename LenaSequence._full…
Browse files Browse the repository at this point in the history
…_seq to _seq. This is more natural for metaprogramming.
  • Loading branch information
ynikitenko committed Sep 18, 2023
1 parent 9dc30b8 commit cc6f9c7
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 48 deletions.
18 changes: 9 additions & 9 deletions lena/core/fill_compute_seq.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from . import lena_sequence
from . import sequence
from . import adapters
from . import fill_seq
from .fill_seq import FillSeq
from . import exceptions
from . import check_sequence_type

Expand Down Expand Up @@ -38,19 +38,19 @@ def _init_sequence_with_el(self, args, el_attr, check_el_type,
# for syntactical reasons; otherwise (*before, el) is impossible
before.append(el)
try:
before_seq = fill_seq.FillSeq(*before)
before_seq = FillSeq(*before)
except exceptions.LenaTypeError as err:
raise err
self._fill_seq = before_seq
self.fill = self._fill_seq.fill
# to do: add exception handling here.
self._after = sequence.Sequence(*after)

# _seq is an attribute of LenaSequence
self._seq = []
self._seq.extend(self._fill_seq)
# self._seq.append(el)
self._seq.extend(self._after)
# data_seq is an attribute of LenaSequence
self._data_seq = []
self._data_seq.extend(self._fill_seq)
# self._data_seq.append(el)
self._data_seq.extend(self._after)


class FillComputeSeq(lena_sequence.LenaSequence):
Expand Down Expand Up @@ -81,7 +81,7 @@ def __init__(self, *args):
"""
self._name = "FillComputeSeq"
super(FillComputeSeq, self).__init__(*args)
seq = self._seq
seq = self._data_seq

before = []
fc_el = None
Expand All @@ -106,7 +106,7 @@ def __init__(self, *args):

before.append(fc_el)
try:
before_seq = fill_seq.FillSeq(*before)
before_seq = FillSeq(*before)
except exceptions.LenaTypeError as err:
raise err
self._fill_seq = before_seq
Expand Down
2 changes: 1 addition & 1 deletion lena/core/fill_request_seq.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def __init__(self, *args, **kwargs):
# fr = adapters.FillRequest(self, reset=reset, bufsize=self._bufsize)
self.run = fr.run
# todo: we will abandon FillRequestSeq in the next release
super(FillRequestSeq, self).__init__(*self._seq)
super(FillRequestSeq, self).__init__(*self._data_seq)

def fill(self, value):
"""Fill *self* with *value*.
Expand Down
12 changes: 6 additions & 6 deletions lena/core/fill_seq.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""FillSeq sequence and its helpers."""

from . import lena_sequence
from .lena_sequence import LenaSequence
from . import adapters
from . import exceptions

Expand All @@ -20,7 +20,7 @@ def fill(self, value):
self._fill_into_el.fill_into(self._fill_el, value)


class FillSeq(lena_sequence.LenaSequence):
class FillSeq(LenaSequence):
"""Sequence with :meth:`fill` method.
Sequence of :class:`.FillInto` elements,
Expand Down Expand Up @@ -50,15 +50,15 @@ def __init__(self, *args):
super(FillSeq, self).__init__(*args)

seq = []
last = self._seq[-1]
last = self._data_seq[-1]

if not callable(getattr(last, "fill", None)):
raise exceptions.LenaTypeError(
"the last argument must implement fill method, "
"{} given".format(last)
)
# convert all elements except last to FillInto (if needed)
for el in self._seq[:-1]:
for el in self._data_seq[:-1]:
if hasattr(el, "fill_into") and callable(el.fill_into):
seq.append(el)
else:
Expand All @@ -78,11 +78,11 @@ def __init__(self, *args):
fill_el = last
for el in reversed(seq[:-1]):
fill_el = _Fill(el, fill_el)
# note that self._seq consists of original FillInto elements.
# note that self._data_seq consists of original FillInto elements.
self._fill_el = fill_el
# self for these methods is different
self.fill = fill_el.fill
self._seq = seq
self._data_seq = seq

def fill(self, value):
"""Fill *value* into an *element*.
Expand Down
14 changes: 7 additions & 7 deletions lena/core/lena_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def __init__(self, *args):
from lena.context import update_recursively
# todo: this doesn't require jinja2,
# but we may want it to support template strings soon
self._full_seq = args
self._seq = args
seq = []

# for static (sequence initialisation time) context
Expand Down Expand Up @@ -95,16 +95,16 @@ def __init__(self, *args):
self._unknown_contexts = unknown_contexts
self._need_context = need_context

self._seq = seq
self._data_seq = seq

def __iter__(self):
return self._full_seq.__iter__()
return self._seq.__iter__()

def __len__(self):
return self._full_seq.__len__()
return self._seq.__len__()

def __getitem__(self, ind):
return self._full_seq[ind]
return self._seq[ind]

def _repr_nested(self, base_indent="", indent=" "*4, el_separ=",\n"):
# to get a one-line representation, use el_separ=", ", indent=""
Expand All @@ -117,9 +117,9 @@ def repr_maybe_nested(el, base_indent, indent):

elems = el_separ.join((repr_maybe_nested(el, base_indent=base_indent,
indent=indent)
for el in self._full_seq))
for el in self._seq))

if "\n" in el_separ and self._full_seq:
if "\n" in el_separ and self._seq:
# maybe new line
mnl = "\n"
# maybe base indent
Expand Down
13 changes: 7 additions & 6 deletions lena/core/sequence.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"""Sequence class."""
import sys

from . import lena_sequence
from .lena_sequence import LenaSequence
from . import adapters
from . import exceptions
from . import functions


class Sequence(lena_sequence.LenaSequence):
class Sequence(LenaSequence):
"""Sequence of elements, such that next takes input
from the previous during *run*.
Expand All @@ -33,8 +33,9 @@ def __init__(self, *args):

seq = []

# todo: we could change self._seq in place
for el in self._seq:
# todo: we could change self._seq in place,
# check performance (replacement + index access)
for el in self._data_seq:
if hasattr(el, "run") and callable(el.run):
seq.append(el)
else:
Expand All @@ -51,7 +52,7 @@ def __init__(self, *args):
else:
seq.append(run_el)

self._seq = seq
self._data_seq = seq

def run(self, flow):
"""Generator that transforms the incoming flow.
Expand All @@ -66,7 +67,7 @@ def run(self, flow):
"""
flow = functions.flow_to_iter(flow)

for el in self._seq:
for el in self._data_seq:
flow = el.run(flow)

flow = functions.flow_to_iter(flow)
Expand Down
26 changes: 13 additions & 13 deletions lena/core/source.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"""Source sequence."""
from __future__ import print_function

from . import lena_sequence
from . import sequence
from . import exceptions
from . import functions
from .lena_sequence import LenaSequence
from .sequence import Sequence
from .exceptions import LenaTypeError
from .functions import flow_to_iter


class Source(lena_sequence.LenaSequence):
class Source(LenaSequence):
"""Sequence with no input flow."""

def __init__(self, *args):
Expand All @@ -31,33 +31,33 @@ def __init__(self, *args):
use :class:`Sequence`.
"""
if not args:
raise exceptions.LenaTypeError(
raise LenaTypeError(
"Source must be initialized with 1 argument or more (0 given)"
)

self._name = "Source" # for repr
super(Source, self).__init__(*args)

first = self._seq[0]
first = self._data_seq[0]
if not callable(first):
raise exceptions.LenaTypeError(
raise LenaTypeError(
"first element {} ".format(first)
+ "must be callable"
)
self._first = first

if len(args) > 1:
self._sequence = sequence.Sequence(*(self._seq[1:]))
self._tail = Sequence(*(self._data_seq[1:]))
else:
self._sequence = ()
self._tail = ()

def __call__(self):
"""Generate flow."""
flow = self._first()
if self._sequence:
return self._sequence.run(flow)
if self._tail:
return self._tail.run(flow)
else:
return functions.flow_to_iter(flow)
return flow_to_iter(flow)

def __eq__(self, other):
if not isinstance(other, Source):
Expand Down
11 changes: 5 additions & 6 deletions tests/core/test_fill_seq.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
from __future__ import print_function

import pytest

import lena.core
from lena.core import Sequence, Source
from lena.core import FillSeq, FillInto
from lena.core import Sequence, Source, FillSeq, FillInto
from tests.examples.fill import StoreFilled
from tests.examples.numeric import Add

Expand All @@ -17,7 +13,10 @@ def test_lena_sequence_fill():
assert len(s1) == 1
assert len(s2) == 2
# can get item
assert isinstance(s2._seq[0], FillInto)
# data_seq wraps an element in an adapter
assert isinstance(s2._data_seq[0], FillInto)
# original sequence stores the element as it was
assert isinstance(s2._seq[0], Add)
# deletion is prohibited
with pytest.raises(TypeError):
del s2[0]
Expand Down

0 comments on commit cc6f9c7

Please sign in to comment.