-
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.
Adds module meta for transforming Lena sequences. Adds SetContext and…
… UpdateContextFromStatic meta elements. LenaSequence supports static context, which allows to set output directories and Cache names during the initialisation phase. Technical changes: LenaSequence gets _full_seq field. lena.context does not depend on lena.flow (get_data_context) to avoid the circular dependency.
- Loading branch information
1 parent
351a12d
commit 1babf76
Showing
11 changed files
with
195 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
Meta | ||
====== | ||
**Elements:** | ||
|
||
.. currentmodule:: lena.meta | ||
.. autosummary:: | ||
|
||
SetContext | ||
UpdateContextFromStatic | ||
|
||
Elements | ||
-------- | ||
|
||
.. autoclass:: SetContext | ||
.. autoclass:: UpdateContextFromStatic |
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
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 @@ | ||
from .elements import SetContext, UpdateContextFromStatic |
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,63 @@ | ||
from copy import deepcopy | ||
|
||
import lena.context | ||
from lena.context import update_recursively | ||
|
||
|
||
class SetContext(object): | ||
"""Set static context for this sequence. | ||
Static context does not automatically update runtime context. | ||
Use :class:`UpdateContextFromStatic` for that. | ||
Static context can be used during the initialisation phase | ||
to set output directories, :class:`.Cache` names, etc. | ||
There is no way to update static context from runtime one. | ||
""" | ||
|
||
def __init__(self, key, value): | ||
"""*key* is a string representing a | ||
(possibly nested) dictionary key. *value* is its value. | ||
See :func:`.str_to_dict` for details. | ||
""" | ||
|
||
# todo: key could be a complete dictionary | ||
# self._subcontext = lena.context.str_to_list(key) | ||
# self._value = value | ||
self._key = key | ||
self._value = value | ||
self._context = lena.context.str_to_dict(key, value) | ||
self._has_no_data = True | ||
|
||
def _get_context(self): | ||
return deepcopy(self._context) | ||
|
||
def __repr__(self): | ||
val = self._value | ||
if isinstance(val, str): | ||
val = '"' + val + '"' | ||
return 'SetContext("{}", {})'.format(self._key, val) | ||
|
||
|
||
class UpdateContextFromStatic(object): | ||
"""Update runtime context with the static one. | ||
Note that for runtime context later elements | ||
update previous values, but for static context | ||
it is the opposite (external and previous elements | ||
take precedence). | ||
""" | ||
|
||
def __init__(self): | ||
self._context = {} | ||
|
||
def _set_context(self, context): | ||
self._context = context | ||
|
||
def run(self, flow): | ||
for val in flow: | ||
data, context = val | ||
# no template substitutions are done, | ||
# that would be too complicated, fragile and wrong | ||
update_recursively(context, deepcopy(self._context)) | ||
yield (data, context) |
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,42 @@ | ||
from copy import deepcopy | ||
|
||
import pytest | ||
|
||
from lena.core import Sequence, Source | ||
|
||
from lena.meta.elements import SetContext, UpdateContextFromStatic | ||
|
||
|
||
def test_set_context(): | ||
data = [(0, {})] | ||
set_context_far = SetContext("data.detector", "far") | ||
|
||
# repr works | ||
assert repr(set_context_far) == 'SetContext("data.detector", "far")' | ||
|
||
## static context does not alter context | ||
# within a sequence | ||
s0 = Sequence(set_context_far) | ||
assert list(s0.run(deepcopy(data))) == data | ||
|
||
# outside a sequence | ||
set_context_near = SetContext("data.detector", "near") | ||
s1 = Sequence(set_context_near, s0) | ||
assert list(s1.run(deepcopy(data))) == data | ||
|
||
# static context is updated after the element | ||
s0u0 = Sequence(set_context_far, UpdateContextFromStatic()) | ||
res0u0 = list(s0u0.run(deepcopy(data))) | ||
assert len(res0u0) == len(data) | ||
assert res0u0[0][0] == data[0][0] | ||
assert res0u0[0][1] == {'data': {'detector': 'far'}} | ||
|
||
# static context is updated after the sequence | ||
s0u1 = Sequence(s0, UpdateContextFromStatic()) | ||
res0u1 = list(s0u1.run(deepcopy(data))) | ||
assert res0u1[0][1] == {'data': {'detector': 'far'}} | ||
|
||
# external context overwrites internal (further) one | ||
s1u0 = Sequence(s1, UpdateContextFromStatic()) | ||
res1u0 = list(s1u0.run(deepcopy(data))) | ||
assert res1u0[0][1] == {'data': {'detector': 'near'}} |