MCPcopy
hub / github.com/numpy/numpy / _array_repr_implementation

Function _array_repr_implementation

numpy/_core/arrayprint.py:1601–1652  ·  view source on GitHub ↗

Internal version of array_repr() that allows overriding array2string.

(
        arr, max_line_width=None, precision=None, suppress_small=None,
        array2string=array2string)

Source from the content-addressed store, hash-verified

1599
1600
1601def _array_repr_implementation(
1602 arr, max_line_width=None, precision=None, suppress_small=None,
1603 array2string=array2string):
1604 """Internal version of array_repr() that allows overriding array2string."""
1605 current_options = format_options.get()
1606 override_repr = current_options["override_repr"]
1607 if override_repr is not None:
1608 return override_repr(arr)
1609
1610 if max_line_width is None:
1611 max_line_width = current_options['linewidth']
1612
1613 if type(arr) is not ndarray:
1614 class_name = type(arr).__name__
1615 else:
1616 class_name = "array"
1617
1618 prefix = class_name + "("
1619 if (current_options['legacy'] <= 113 and
1620 arr.shape == () and not arr.dtype.names):
1621 lst = repr(arr.item())
1622 else:
1623 lst = array2string(arr, max_line_width, precision, suppress_small,
1624 ', ', prefix, suffix=")")
1625
1626 # Add dtype and shape information if these cannot be inferred from
1627 # the array string.
1628 extras = []
1629 if ((arr.size == 0 and arr.shape != (0,))
1630 or (current_options['legacy'] > 210
1631 and arr.size > current_options['threshold'])):
1632 extras.append(f"shape={arr.shape}")
1633 if not dtype_is_implied(arr.dtype) or arr.size == 0:
1634 extras.append(f"dtype={dtype_short_repr(arr.dtype)}")
1635
1636 if not extras:
1637 return prefix + lst + ")"
1638
1639 arr_str = prefix + lst + ","
1640 extra_str = ", ".join(extras) + ")"
1641 # compute whether we should put extras on a new line: Do so if adding the
1642 # extras would extend the last line past max_line_width.
1643 # Note: This line gives the correct result even when rfind returns -1.
1644 last_line_len = len(arr_str) - (arr_str.rfind('\n') + 1)
1645 spacer = " "
1646 if current_options['legacy'] <= 113:
1647 if issubclass(arr.dtype.type, flexible):
1648 spacer = '\n' + ' ' * len(prefix)
1649 elif last_line_len + len(extra_str) + 1 > max_line_width:
1650 spacer = '\n' + ' ' * len(prefix)
1651
1652 return arr_str + spacer + extra_str
1653
1654
1655def _array_repr_dispatcher(

Callers 1

array_reprFunction · 0.85

Calls 7

array2stringFunction · 0.85
dtype_is_impliedFunction · 0.85
dtype_short_reprFunction · 0.85
itemMethod · 0.80
joinMethod · 0.80
rfindMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…