Skip to content

Commit

Permalink
Adjust EOPatch repr method (#762)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
zigaLuksic and Matic Lubej authored Oct 17, 2023
1 parent b1f5489 commit f311aa7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
30 changes: 15 additions & 15 deletions eolearn/core/eodata.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,36 +455,36 @@ 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})"

if isinstance(value, gpd.GeoDataFrame):
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}<length={len(value)}>"

return repr(value)
return str(type(value))

@staticmethod
def _repr_value_class(value: object) -> str:
Expand Down

0 comments on commit f311aa7

Please sign in to comment.