From f311aa77ce468120e8fbb9ddac7b6fdfe5ab9b14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=BDiga=20Luk=C5=A1i=C4=8D?= <31988337+zigaLuksic@users.noreply.github.com> Date: Tue, 17 Oct 2023 14:35:01 +0200 Subject: [PATCH] Adjust EOPatch repr method (#762) * add rule for dictionaries, also apply rules to all non-sequence objects * recursively apply to subelements * simplify a bit * remove needless docstring * adjust changelog * Update eolearn/core/eodata.py Co-authored-by: Matic Lubej --------- Co-authored-by: Matic Lubej --- CHANGELOG.md | 1 + eolearn/core/eodata.py | 30 +++++++++++++++--------------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 669385c3..06d84471 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ - `MorphologicalFilterTask` adapted to work on boolean values. - Added `temporal_subset` method to `EOPatch`, which can be used to extract a subset of an `EOPatch` by filtering out temporal slices. Also added a corresponding `TemporalSubsetTask`. - `EOExecutor` now has an option to treat `TemporalDimensionWarning` as an exception. +- String representation of `EOPatch` objects was revisited to avoid edge cases where the output would print enormous objects. ## [Version 1.5.0] - 2023-09-06 diff --git a/eolearn/core/eodata.py b/eolearn/core/eodata.py index e771a77b..93660204 100644 --- a/eolearn/core/eodata.py +++ b/eolearn/core/eodata.py @@ -455,11 +455,7 @@ def __repr__(self) -> str: @staticmethod def _repr_value(value: object) -> str: - """Creates a representation string for different types of data. - - :param value: data in any type - :return: representation string - """ + """Creates a representation string for different types of data.""" if isinstance(value, np.ndarray): return f"{EOPatch._repr_value_class(value)}(shape={value.shape}, dtype={value.dtype})" @@ -467,24 +463,28 @@ def _repr_value(value: object) -> str: crs = CRS(value.crs).ogc_string() if value.crs else value.crs return f"{EOPatch._repr_value_class(value)}(columns={list(value)}, length={len(value)}, crs={crs})" + repr_str = str(value) + if len(repr_str) <= MAX_DATA_REPR_LEN: + return repr_str + if isinstance(value, (list, tuple, dict)) and value: - repr_str = str(value) - if len(repr_str) <= MAX_DATA_REPR_LEN: - return repr_str + lb, rb = ("[", "]") if isinstance(value, list) else ("(", ")") if isinstance(value, tuple) else ("{", "}") - l_bracket, r_bracket = ("[", "]") if isinstance(value, list) else ("(", ")") - if isinstance(value, (list, tuple)) and len(value) > 2: - repr_str = f"{l_bracket}{value[0]!r}, ..., {value[-1]!r}{r_bracket}" + if isinstance(value, dict): # generate representation of first element or (key, value) pair + some_key = next(iter(value)) + repr_of_el = f"{EOPatch._repr_value(some_key)}: {EOPatch._repr_value(value[some_key])}" + else: + repr_of_el = EOPatch._repr_value(value[0]) - if len(repr_str) > MAX_DATA_REPR_LEN and isinstance(value, (list, tuple)) and len(value) > 1: - repr_str = f"{l_bracket}{value[0]!r}, ...{r_bracket}" + many_elements_visual = ", ..." if len(value) > 1 else "" # add ellipsis if there are multiple elements + repr_str = f"{lb}{repr_of_el}{many_elements_visual}{rb}" if len(repr_str) > MAX_DATA_REPR_LEN: repr_str = str(type(value)) - return f"{repr_str}, length={len(value)}" + return f"{repr_str}" - return repr(value) + return str(type(value)) @staticmethod def _repr_value_class(value: object) -> str: