Skip to content

Commit

Permalink
🩹 Fix ParameterGroup repr when created with 'from_list' (#827)
Browse files Browse the repository at this point in the history
Improved representation of parameters
  • Loading branch information
s-weigand authored Sep 25, 2021
1 parent 0ba531d commit 9a2d733
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
17 changes: 10 additions & 7 deletions glotaran/parameter/parameter_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,14 +429,17 @@ def _repr_markdown_(self) -> str:

def __repr__(self):
"""Representation used by repl and tracebacks."""

parameter_short_notations = [
[str(parameter.label), parameter.value] for parameter in self._parameters.values()
]
if self.label is None:
return f"{type(self).__name__}.from_dict({super().__repr__()})"
elif len(self._parameters):
parameter_short_notations = [
f"[{parameter.label!r}, {parameter.value}]"
for _, parameter in self._parameters.items()
]
return f"[{', '.join(parameter_short_notations)}]"
if len(self._parameters) == 0:
return f"{type(self).__name__}.from_dict({super().__repr__()})"
else:
return f"{type(self).__name__}.from_list({parameter_short_notations})"
if len(self._parameters):
return parameter_short_notations.__repr__()
else:
return super().__repr__()

Expand Down
15 changes: 14 additions & 1 deletion glotaran/parameter/test/test_parameter_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,27 @@ def test_param_group_markdown_is_order_independent():


def test_param_group_repr():
"""Repr creates code to recreate the object."""
"""Repr creates code to recreate the object with from_dict."""
result = ParameterGroup.from_dict({"foo": {"bar": [["1", 1.0], ["2", 2.0], ["3", 3.0]]}})
result_short = ParameterGroup.from_dict({"foo": {"bar": [1, 2, 3]}})
expected = "ParameterGroup.from_dict({'foo': {'bar': [['1', 1.0], ['2', 2.0], ['3', 3.0]]}})"

assert result == result_short
assert result_short.__repr__() == expected
assert result.__repr__() == expected
assert result == eval(result.__repr__())


def test_param_group_repr_from_list():
"""Repr creates code to recreate the object with from_list."""
result = ParameterGroup.from_list([["1", 2.3], ["2", 3.0]])
result_short = ParameterGroup.from_list([2.3, 3.0])
expected = "ParameterGroup.from_list([['1', 2.3], ['2', 3.0]])"

assert result == result_short
assert result.__repr__() == expected
assert result_short.__repr__() == expected
assert result == eval(result.__repr__())


def test_param_group_ipython_rendering():
Expand Down

0 comments on commit 9a2d733

Please sign in to comment.