Skip to content

Commit

Permalink
Add meta.StoreContext for debugging purposes (moved from tests).
Browse files Browse the repository at this point in the history
  • Loading branch information
ynikitenko committed Sep 5, 2023
1 parent c39bdf4 commit 5831496
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 22 deletions.
2 changes: 2 additions & 0 deletions docs/source/meta.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ Meta
.. autosummary::

SetContext
StoreContext
UpdateContextFromStatic

Elements
--------

.. autoclass:: SetContext
.. autoclass:: StoreContext
.. autoclass:: UpdateContextFromStatic
2 changes: 1 addition & 1 deletion lena/meta/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .elements import SetContext, UpdateContextFromStatic
from .elements import SetContext, StoreContext, UpdateContextFromStatic
19 changes: 19 additions & 0 deletions lena/meta/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,25 @@ def __repr__(self):
return 'SetContext("{}", {})'.format(self._key, val)


class StoreContext():
"""Store static context. Use for debugging."""

def __init__(self, name="", verbose=False):
"""*name* and *verbose* affect output and representation."""
self._name = name
self._context = {}
self._verbose = verbose
self._has_no_data = True

def _set_context(self, context):
if self._verbose:
print("StoreContext({}): storing {}".format(self._name, context))
self._context = context

def __repr__(self):
return "StoreContext({})".format(repr(self._context))


class UpdateContextFromStatic(object):
"""Update runtime context with the static one.
Expand Down
2 changes: 1 addition & 1 deletion lena/output/write.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ def _set_context(self, context):
# set static context to format the output directory name
try:
outdir = self._format_context(context)
except LenaKeyError:
except lena.core.LenaKeyError:
pass
else:
self.output_directory = outdir
Expand Down
29 changes: 9 additions & 20 deletions tests/meta/test_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,7 @@

from lena.core import Sequence, Source, Split

from lena.meta.elements import SetContext, UpdateContextFromStatic


class StoreContext():

def __init__(self, name=""):
self._name = name
self._has_no_data = True

def _set_context(self, context):
print("StoreContext({}), {}".format(self._name, context))
self.context = context
from lena.meta.elements import SetContext, UpdateContextFromStatic, StoreContext


def test_set_context():
Expand Down Expand Up @@ -86,15 +75,15 @@ def test_set_context_split():
split,
store4
)
assert store1.context == {'data': {'cycle': 1, 'lost': True}}
assert store2.context == {
assert store1._context == {'data': {'cycle': 1, 'lost': True}}
assert store2._context == {
'data': {'cycle': 1, 'detector': 'far', 'lost': True}
}
assert store3.context == {
assert store3._context == {
'data': {'cycle': 1, 'detector': 'near', 'lost': True}
}
# static context is the same for the same level of nesting
assert store4.context == store1.context
assert store4._context == store1._context


def test_set_template_context_split():
Expand Down Expand Up @@ -143,17 +132,17 @@ def test_set_template_context_split():
split,
store4
)
assert store1.context == {'data': {'cycle': 1, 'lost': True}}
assert store2.context == {
assert store1._context == {'data': {'cycle': 1, 'lost': True}}
assert store2._context == {
'data': {'cycle': 1, 'detector': 'far', 'lost': True},
'cycle': '1',
}
assert store3.context == {
assert store3._context == {
'data': {'cycle': 1, 'detector': 'near', 'lost': True},
'detector': 'maybe_near',
# todo: maybe this is indended?..
'cycle': '2',
# 'cycle': '2_indeed',
}
# static context is the same for the same level of nesting
assert store4.context == store1.context
assert store4._context == store1._context

0 comments on commit 5831496

Please sign in to comment.