MCPcopy
hub / github.com/pydata/xarray / format_array_flat

Function format_array_flat

xarray/core/formatting.py:227–273  ·  view source on GitHub ↗

Return a formatted string for as many items in the flattened version of array that will fit within max_width characters.

(array, max_width: int)

Source from the content-addressed store, hash-verified

225
226
227def format_array_flat(array, max_width: int):
228 """Return a formatted string for as many items in the flattened version of
229 array that will fit within max_width characters.
230 """
231 # every item will take up at least two characters, but we always want to
232 # print at least first and last items
233 max_possibly_relevant = min(max(array.size, 1), max(math.ceil(max_width / 2.0), 2))
234 relevant_front_items = format_items(
235 first_n_items(array, (max_possibly_relevant + 1) // 2)
236 )
237 relevant_back_items = format_items(last_n_items(array, max_possibly_relevant // 2))
238 # interleave relevant front and back items:
239 # [a, b, c] and [y, z] -> [a, z, b, y, c]
240 relevant_items = sum(
241 zip_longest(relevant_front_items, reversed(relevant_back_items)), ()
242 )[:max_possibly_relevant]
243
244 cum_len = np.cumsum([len(s) + 1 for s in relevant_items]) - 1
245 if (array.size > 2) and (
246 (max_possibly_relevant < array.size) or array_any(cum_len > max_width)
247 ):
248 padding = " ... "
249 max_len = max(int(np.argmax(cum_len + len(padding) - 1 > max_width)), 2)
250 count = min(array.size, max_len)
251 else:
252 count = array.size
253 padding = "" if (count <= 1) else " "
254
255 num_front = (count + 1) // 2
256 num_back = count - num_front
257 # note that num_back is 0 <--> array.size is 0 or 1
258 # <--> relevant_back_items is []
259 pprint_str = "".join(
260 [
261 " ".join(relevant_front_items[:num_front]),
262 padding,
263 " ".join(relevant_back_items[-num_back:]),
264 ]
265 )
266
267 # As a final check, if it's still too long even with the limit in values,
268 # replace the end with an ellipsis
269 # NB: this will still returns a full 3-character ellipsis when max_width < 3
270 if len(pprint_str) > max_width:
271 pprint_str = pprint_str[: max(max_width - 3, 0)] + "..."
272
273 return pprint_str
274
275
276# mapping of tuple[modulename, classname] to repr

Callers 4

_repr_inline_Method · 0.90
_repr_inline_Method · 0.90
__repr__Method · 0.90

Calls 8

array_anyFunction · 0.90
format_itemsFunction · 0.85
first_n_itemsFunction · 0.85
last_n_itemsFunction · 0.85
ceilMethod · 0.45
cumsumMethod · 0.45
argmaxMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…