Skip to content

Commit

Permalink
Fix an exception bug in Cache. Template context is not deleted from S…
Browse files Browse the repository at this point in the history
…equence after it was set (thus we can overwrite it with better values).
  • Loading branch information
ynikitenko committed Sep 5, 2023
1 parent 5831496 commit 257b9ed
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
19 changes: 11 additions & 8 deletions lena/core/lena_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ def __init__(self, *args):
update_recursively(context, el_context)

# Render that context that we can. Context is updated.
unknown_contexts = _update_unknown_contexts(unknown_contexts, context)
_update_unknown_contexts(unknown_contexts, context)
# unknown_contexts of this sequence are left as they are.
# We shall set them every time we update context.
# unknown_contexts = _update_unknown_contexts(unknown_contexts, context)
# There is no way to check (during init)
# that all static context was set,
# because sequences are allowed to separate/wrap any elements.
Expand All @@ -75,13 +78,13 @@ def __init__(self, *args):
# context is same for the whole sequence
# (and external ones, but not for Split)
if hasattr(el, "_set_context"):
if not unknown_contexts:
# we set context after all unknowns are known.
# el can't have unknown contexts
# (except subsequences of Split);
# at least not contexts
# that shall update the current context
el._set_context(context)
# if not unknown_contexts:
# we set context after all unknowns are known.
# el can't have unknown contexts
# (except subsequences of Split);
# at least not contexts
# that shall update the current context
el._set_context(context)
need_context.append(el)

# todo 0.7: or has context
Expand Down
2 changes: 1 addition & 1 deletion lena/flow/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def _set_context(self, context):
return
try:
filename = self._format_context(context)
except LenaKeyError:
except lena.core.LenaKeyError:
pass
else:
self._filename = filename
Expand Down
10 changes: 6 additions & 4 deletions tests/meta/test_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ def test_set_template_context_split():
# todo: this is too complicated for now
# # this will be overwritten
# SetContext("cycle", "{{cycle}}_indeed"),
SetContext("detector", "{{data.detector}}"),
SetContext("detector", "maybe_{{detector}}"),
SetContext("detector", "{{data.detector}}"),
)
# print(seq[-1]._context)
assert seq._get_context()["detector"] == "maybe_near"
Expand All @@ -132,16 +132,18 @@ def test_set_template_context_split():
split,
store4
)
assert store1._context == {'data': {'cycle': 1, 'lost': True}}
assert store1._context == {
'data': {'cycle': 1, 'lost': True},
'cycle': '1',
}
assert store2._context == {
'data': {'cycle': 1, 'detector': 'far', 'lost': True},
'cycle': '1',
}
assert store3._context == {
'data': {'cycle': 1, 'detector': 'near', 'lost': True},
'detector': 'maybe_near',
# todo: maybe this is indended?..
'cycle': '2',
'cycle': '1',
# 'cycle': '2_indeed',
}
# static context is the same for the same level of nesting
Expand Down

0 comments on commit 257b9ed

Please sign in to comment.