Skip to content

Commit 8f74642

Browse files
Updating ParticleFile repr
And also adding support for dictionaries in the _format_list_items_multiline function
1 parent 15867e6 commit 8f74642

2 files changed

Lines changed: 31 additions & 19 deletions

File tree

src/parcels/_core/particleset.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import numpy as np
88
import xarray as xr
99
from tqdm import tqdm
10-
from zarr.storage import DirectoryStore
1110

1211
from parcels._core.converters import _convert_to_flat_array
1312
from parcels._core.kernel import Kernel
@@ -21,7 +20,7 @@
2120
)
2221
from parcels._core.warnings import ParticleSetWarning
2322
from parcels._logger import logger
24-
from parcels._reprs import particleset_repr
23+
from parcels._reprs import _format_zarr_output_location, particleset_repr
2524

2625
__all__ = ["ParticleSet"]
2726

@@ -446,7 +445,7 @@ def execute(
446445

447446
# Set up pbar
448447
if output_file:
449-
logger.info(f"Output files are stored in {_format_output_location(output_file.store)}")
448+
logger.info(f"Output files are stored in {_format_zarr_output_location(output_file.store)}")
450449

451450
if verbose_progress:
452451
pbar = tqdm(total=end_time - start_time, file=sys.stdout)
@@ -591,9 +590,3 @@ def _get_start_time(first_release_time, time_interval, sign_dt, runtime):
591590

592591
start_time = first_release_time if not np.isnan(first_release_time) else fieldset_start
593592
return start_time
594-
595-
596-
def _format_output_location(zarr_obj):
597-
if isinstance(zarr_obj, DirectoryStore):
598-
return zarr_obj.path
599-
return repr(zarr_obj)

src/parcels/_reprs.py

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import numpy as np
99
import xarray as xr
10+
from zarr.storage import DirectoryStore
1011

1112
if TYPE_CHECKING:
1213
from parcels import Field, FieldSet, ParticleSet
@@ -77,7 +78,7 @@ def particleset_repr(pset: ParticleSet) -> str:
7778
if len(pset) < 10:
7879
particles = [repr(p) for p in pset]
7980
else:
80-
particles = [repr(pset[i]) for i in range(7)] + ["..."]
81+
particles = [repr(pset[i]) for i in range(7)] + ["..."] + [repr(pset[-1])]
8182

8283
out = f"""<{type(pset).__name__}>
8384
Number of particles: {len(pset)}
@@ -101,6 +102,7 @@ def particlesetview_repr(pview: Any) -> str:
101102

102103

103104
def particleclass_repr(pclass: Any) -> str:
105+
"""Return a pretty repr for ParticleClass"""
104106
vars = [repr(v) for v in pclass.variables]
105107
out = f"""
106108
{_format_list_items_multiline(vars, level=1, with_brackets=False)}
@@ -109,18 +111,24 @@ def particleclass_repr(pclass: Any) -> str:
109111

110112

111113
def variable_repr(var: Any) -> str:
114+
"""Return a pretty repr for Variable"""
112115
return f"Variable(name={var._name!r}, dtype={var.dtype!r}, initial={var.initial!r}, to_write={var.to_write!r}, attrs={var.attrs!r})"
113116

114117

115118
def timeinterval_repr(ti: Any) -> str:
119+
"""Return a pretty repr for TimeInterval"""
116120
return f"TimeInterval(left={ti.left!r}, right={ti.right!r})"
117121

118122

119123
def particlefile_repr(pfile: Any) -> str:
120-
out = f"""{type(pfile).__name__}
121-
outputdt : {pfile.outputdt!r}
122-
chunks : {pfile.chunks!r}
123-
create_new_zarrfile: {pfile.create_new_zarrfile!r}
124+
"""Return a pretty repr for ParticleFile"""
125+
out = f"""<{type(pfile).__name__}>
126+
store : {_format_zarr_output_location(pfile.store)}
127+
outputdt : {pfile.outputdt!r}
128+
chunks : {pfile.chunks!r}
129+
create_new_zarrfile : {pfile.create_new_zarrfile!r}
130+
metadata :
131+
{_format_list_items_multiline(pfile.metadata, level=2, with_brackets=False)}
124132
"""
125133
return textwrap.dedent(out).strip()
126134

@@ -131,8 +139,8 @@ def default_repr(obj: Any):
131139
return object.__repr__(obj)
132140

133141

134-
def _format_list_items_multiline(items: list[str], level: int = 1, with_brackets: bool = True) -> str:
135-
"""Given a list of strings, formats them across multiple lines.
142+
def _format_list_items_multiline(items: list[str] | dict, level: int = 1, with_brackets: bool = True) -> str:
143+
"""Given a list of strings or a dict, formats them across multiple lines.
136144
137145
Uses indentation levels of 4 spaces provided by ``level``.
138146
@@ -149,15 +157,26 @@ def _format_list_items_multiline(items: list[str], level: int = 1, with_brackets
149157
if len(items) == 0:
150158
return "[]"
151159

152-
assert level >= 0, "Indentation level >=0 supported"
160+
assert level >= 1, "Indentation level >=1 supported"
153161
indentation_str = level * 4 * " "
154162
indentation_str_end = (level - 1) * 4 * " "
155163

156-
items_str = ",\n".join([textwrap.indent(i, indentation_str) for i in items])
164+
if isinstance(items, dict):
165+
entries = [f"{k!r}: {v!r}" for k, v in items.items()]
166+
else:
167+
entries = [i if isinstance(i, str) else repr(i) for i in items]
168+
157169
if with_brackets:
170+
items_str = ",\n".join([textwrap.indent(e, indentation_str) for e in entries])
158171
return f"[\n{items_str}\n{indentation_str_end}]"
159172
else:
160-
return f"{items_str}"
173+
return "\n".join([textwrap.indent(e, indentation_str) for e in entries])
174+
175+
176+
def _format_zarr_output_location(zarr_obj):
177+
if isinstance(zarr_obj, DirectoryStore):
178+
return zarr_obj.path
179+
return repr(zarr_obj)
161180

162181

163182
def is_builtin_object(obj):

0 commit comments

Comments
 (0)