Skip to content

Commit f311aa7

Browse files
zigaLuksicMatic Lubej
and
Matic Lubej
authored
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 <[email protected]> --------- Co-authored-by: Matic Lubej <[email protected]>
1 parent b1f5489 commit f311aa7

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- `MorphologicalFilterTask` adapted to work on boolean values.
44
- 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`.
55
- `EOExecutor` now has an option to treat `TemporalDimensionWarning` as an exception.
6+
- String representation of `EOPatch` objects was revisited to avoid edge cases where the output would print enormous objects.
67

78
## [Version 1.5.0] - 2023-09-06
89

Diff for: eolearn/core/eodata.py

+15-15
Original file line numberDiff line numberDiff line change
@@ -455,36 +455,36 @@ def __repr__(self) -> str:
455455

456456
@staticmethod
457457
def _repr_value(value: object) -> str:
458-
"""Creates a representation string for different types of data.
459-
460-
:param value: data in any type
461-
:return: representation string
462-
"""
458+
"""Creates a representation string for different types of data."""
463459
if isinstance(value, np.ndarray):
464460
return f"{EOPatch._repr_value_class(value)}(shape={value.shape}, dtype={value.dtype})"
465461

466462
if isinstance(value, gpd.GeoDataFrame):
467463
crs = CRS(value.crs).ogc_string() if value.crs else value.crs
468464
return f"{EOPatch._repr_value_class(value)}(columns={list(value)}, length={len(value)}, crs={crs})"
469465

466+
repr_str = str(value)
467+
if len(repr_str) <= MAX_DATA_REPR_LEN:
468+
return repr_str
469+
470470
if isinstance(value, (list, tuple, dict)) and value:
471-
repr_str = str(value)
472-
if len(repr_str) <= MAX_DATA_REPR_LEN:
473-
return repr_str
471+
lb, rb = ("[", "]") if isinstance(value, list) else ("(", ")") if isinstance(value, tuple) else ("{", "}")
474472

475-
l_bracket, r_bracket = ("[", "]") if isinstance(value, list) else ("(", ")")
476-
if isinstance(value, (list, tuple)) and len(value) > 2:
477-
repr_str = f"{l_bracket}{value[0]!r}, ..., {value[-1]!r}{r_bracket}"
473+
if isinstance(value, dict): # generate representation of first element or (key, value) pair
474+
some_key = next(iter(value))
475+
repr_of_el = f"{EOPatch._repr_value(some_key)}: {EOPatch._repr_value(value[some_key])}"
476+
else:
477+
repr_of_el = EOPatch._repr_value(value[0])
478478

479-
if len(repr_str) > MAX_DATA_REPR_LEN and isinstance(value, (list, tuple)) and len(value) > 1:
480-
repr_str = f"{l_bracket}{value[0]!r}, ...{r_bracket}"
479+
many_elements_visual = ", ..." if len(value) > 1 else "" # add ellipsis if there are multiple elements
480+
repr_str = f"{lb}{repr_of_el}{many_elements_visual}{rb}"
481481

482482
if len(repr_str) > MAX_DATA_REPR_LEN:
483483
repr_str = str(type(value))
484484

485-
return f"{repr_str}, length={len(value)}"
485+
return f"{repr_str}<length={len(value)}>"
486486

487-
return repr(value)
487+
return str(type(value))
488488

489489
@staticmethod
490490
def _repr_value_class(value: object) -> str:

0 commit comments

Comments
 (0)