77
88import numpy as np
99import xarray as xr
10+ from zarr .storage import DirectoryStore
1011
1112if 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
103104def 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
111113def 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
115118def 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
119123def 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
163182def is_builtin_object (obj ):
0 commit comments