Skip to content

Commit

Permalink
List are now formatted one line per element.
Browse files Browse the repository at this point in the history
isinstance(obj, list) was not working because traitlets.List and derivatives are not considered list type.

config_files trait is also commented because it crashed with the empty list as default.
  • Loading branch information
ccossou committed Nov 29, 2023
1 parent 93d0899 commit 8642e35
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions ctapipe/core/config_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,12 @@ def _trait_to_str(trait, help=True, indent_level=0):
trait_value = f"'{trait_value}'"

Check warning on line 79 in ctapipe/core/config_writer.py

View check run for this annotation

Codecov / codecov/patch

ctapipe/core/config_writer.py#L78-L79

Added lines #L78 - L79 were not covered by tests

# Make sure that list of values end up on multiple lines (In particular quality criteria)
# Since traitlets.List derivative can't be compared with it, nor list/tuple, I have to check if "List" is in the
# type (converted as str)
value_repr = ""
if isinstance(trait_value, list):
trait_value_type = str(type(trait_value))
trait_value_is_list = ("List" in trait_value_type) or isinstance(trait_value, list)
if trait_value_is_list:
for v in trait_value:

Check warning on line 88 in ctapipe/core/config_writer.py

View check run for this annotation

Codecov / codecov/patch

ctapipe/core/config_writer.py#L84-L88

Added lines #L84 - L88 were not covered by tests
# tuples are not correctly handled by yaml, so we convert them to list here. They'll be converted to tuple
# when reading again.
Expand All @@ -92,7 +96,9 @@ def _trait_to_str(trait, help=True, indent_level=0):

# Automatically comment all parameters that are unvalid
commented = ""
if trait_value == traitlets.Undefined:
if trait_value in (traitlets.Undefined, None):
commented = "#"
elif trait_type.startswith("List") and len(trait_value) == 0:
commented = "#"

Check warning on line 102 in ctapipe/core/config_writer.py

View check run for this annotation

Codecov / codecov/patch

ctapipe/core/config_writer.py#L98-L102

Added lines #L98 - L102 were not covered by tests

trait_repr += f"{indent_str*indent_level}{commented}{trait.name}: {value_repr}\n"

Check warning on line 104 in ctapipe/core/config_writer.py

View check run for this annotation

Codecov / codecov/patch

ctapipe/core/config_writer.py#L104

Added line #L104 was not covered by tests
Expand Down Expand Up @@ -205,3 +211,30 @@ def wrap_comment(text, indent_level=0, width=144, indent_str=" "):
initial_indent=indent_str * indent_level + "# ",
subsequent_indent=indent_str * indent_level + "# ",
)


# def check_conf_repr(conf_repr):
# """
# Check the conf representation for empty sections that will crash on read
#
# e.g. section EventSource has no valid parameter in the default, and crash with the error:
# > ValueError: values whose keys begin with an uppercase char must be Config instances: 'EventSource', None
#
# :param str conf_repr: Config representation for Tool or Component
#
# :return: Cleaned configuration representation
# """
# in_lines = conf_repr.split("\n")
#
# work_lines = [l.strip().split("#")[0] for l in in_lines]
#
# section_idx = -1
# section_indent = -1
# inside_section
# for i in range(len(work_lines)):
# line = work_lines[i]
# section_line = line.strip()[0].isupper() # Bool
# if section_line:
# section_idx = i
# section_indent = len(line) - len(line.lstrip())
#

0 comments on commit 8642e35

Please sign in to comment.