|
59 | 59 | 'formatter': None,
|
60 | 60 | # Internally stored as an int to simplify comparisons; converted from/to
|
61 | 61 | # str/False on the way in/out.
|
62 |
| - 'legacy': sys.maxsize} |
| 62 | + 'legacy': sys.maxsize, |
| 63 | + 'override_repr': None, |
| 64 | +} |
63 | 65 |
|
64 | 66 | def _make_options_dict(precision=None, threshold=None, edgeitems=None,
|
65 | 67 | linewidth=None, suppress=None, nanstr=None, infstr=None,
|
66 |
| - sign=None, formatter=None, floatmode=None, legacy=None): |
| 68 | + sign=None, formatter=None, floatmode=None, legacy=None, |
| 69 | + override_repr=None): |
67 | 70 | """
|
68 | 71 | Make a dictionary out of the non-None arguments, plus conversion of
|
69 | 72 | *legacy* and sanity checks.
|
@@ -119,7 +122,7 @@ def _make_options_dict(precision=None, threshold=None, edgeitems=None,
|
119 | 122 | def set_printoptions(precision=None, threshold=None, edgeitems=None,
|
120 | 123 | linewidth=None, suppress=None, nanstr=None,
|
121 | 124 | infstr=None, formatter=None, sign=None, floatmode=None,
|
122 |
| - *, legacy=None): |
| 125 | + *, legacy=None, override_repr=None): |
123 | 126 | """
|
124 | 127 | Set printing options.
|
125 | 128 |
|
@@ -224,6 +227,9 @@ def set_printoptions(precision=None, threshold=None, edgeitems=None,
|
224 | 227 |
|
225 | 228 | .. versionadded:: 1.14.0
|
226 | 229 | .. versionchanged:: 1.22.0
|
| 230 | + override_repr: callable, optional |
| 231 | + If set a passed function will be used for generating arrays' repr. |
| 232 | + Other options will be ignored. |
227 | 233 |
|
228 | 234 | See Also
|
229 | 235 | --------
|
@@ -285,9 +291,10 @@ def set_printoptions(precision=None, threshold=None, edgeitems=None,
|
285 | 291 | """
|
286 | 292 | opt = _make_options_dict(precision, threshold, edgeitems, linewidth,
|
287 | 293 | suppress, nanstr, infstr, sign, formatter,
|
288 |
| - floatmode, legacy) |
289 |
| - # formatter is always reset |
| 294 | + floatmode, legacy, override_repr) |
| 295 | + # formatter and override_repr are always reset |
290 | 296 | opt['formatter'] = formatter
|
| 297 | + opt['override_repr'] = override_repr |
291 | 298 | _format_options.update(opt)
|
292 | 299 |
|
293 | 300 | # set the C variable for legacy mode
|
@@ -333,7 +340,7 @@ def get_printoptions():
|
333 | 340 | --------
|
334 | 341 |
|
335 | 342 | >>> np.get_printoptions()
|
336 |
| - {'edgeitems': 3, 'threshold': 1000, ..., 'legacy': False} |
| 343 | + {'edgeitems': 3, 'threshold': 1000, ..., 'override_repr': None} |
337 | 344 |
|
338 | 345 | >>> np.get_printoptions()['linewidth']
|
339 | 346 | 75
|
@@ -1552,6 +1559,10 @@ def _array_repr_implementation(
|
1552 | 1559 | arr, max_line_width=None, precision=None, suppress_small=None,
|
1553 | 1560 | array2string=array2string):
|
1554 | 1561 | """Internal version of array_repr() that allows overriding array2string."""
|
| 1562 | + override_repr = _format_options["override_repr"] |
| 1563 | + if override_repr is not None: |
| 1564 | + return override_repr(arr) |
| 1565 | + |
1555 | 1566 | if max_line_width is None:
|
1556 | 1567 | max_line_width = _format_options['linewidth']
|
1557 | 1568 |
|
@@ -1727,78 +1738,3 @@ def array_str(a, max_line_width=None, precision=None, suppress_small=None):
|
1727 | 1738 | array2string=_array2string_impl)
|
1728 | 1739 | _default_array_repr = functools.partial(_array_repr_implementation,
|
1729 | 1740 | array2string=_array2string_impl)
|
1730 |
| - |
1731 |
| - |
1732 |
| -def set_string_function(f, repr=True): |
1733 |
| - """ |
1734 |
| - Set a Python function to be used when pretty printing arrays. |
1735 |
| -
|
1736 |
| - .. deprecated:: 2.0 |
1737 |
| - Use `np.set_printoptions` instead with a formatter for custom |
1738 |
| - printing of NumPy objects. |
1739 |
| -
|
1740 |
| - Parameters |
1741 |
| - ---------- |
1742 |
| - f : function or None |
1743 |
| - Function to be used to pretty print arrays. The function should expect |
1744 |
| - a single array argument and return a string of the representation of |
1745 |
| - the array. If None, the function is reset to the default NumPy function |
1746 |
| - to print arrays. |
1747 |
| - repr : bool, optional |
1748 |
| - If True (default), the function for pretty printing (``__repr__``) |
1749 |
| - is set, if False the function that returns the default string |
1750 |
| - representation (``__str__``) is set. |
1751 |
| -
|
1752 |
| - See Also |
1753 |
| - -------- |
1754 |
| - set_printoptions, get_printoptions |
1755 |
| -
|
1756 |
| - Examples |
1757 |
| - -------- |
1758 |
| - >>> from numpy._core.arrayprint import set_string_function |
1759 |
| - >>> def pprint(arr): |
1760 |
| - ... return 'HA! - What are you going to do now?' |
1761 |
| - ... |
1762 |
| - >>> set_string_function(pprint) |
1763 |
| - >>> a = np.arange(10) |
1764 |
| - >>> a |
1765 |
| - HA! - What are you going to do now? |
1766 |
| - >>> _ = a |
1767 |
| - >>> # [0 1 2 3 4 5 6 7 8 9] |
1768 |
| -
|
1769 |
| - We can reset the function to the default: |
1770 |
| -
|
1771 |
| - >>> set_string_function(None) |
1772 |
| - >>> a |
1773 |
| - array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) |
1774 |
| -
|
1775 |
| - `repr` affects either pretty printing or normal string representation. |
1776 |
| - Note that ``__repr__`` is still affected by setting ``__str__`` |
1777 |
| - because the width of each array element in the returned string becomes |
1778 |
| - equal to the length of the result of ``__str__()``. |
1779 |
| -
|
1780 |
| - >>> x = np.arange(4) |
1781 |
| - >>> set_string_function(lambda x:'random', repr=False) |
1782 |
| - >>> x.__str__() |
1783 |
| - 'random' |
1784 |
| - >>> x.__repr__() |
1785 |
| - 'array([0, 1, 2, 3])' |
1786 |
| -
|
1787 |
| - """ |
1788 |
| - |
1789 |
| - # Deprecated in NumPy 2.0, 2023-07-11 |
1790 |
| - warnings.warn( |
1791 |
| - "`set_string_function` is deprecated. Use `np.set_printoptions` " |
1792 |
| - "with a formatter for custom printing NumPy objects. " |
1793 |
| - "(deprecated in NumPy 2.0)", |
1794 |
| - DeprecationWarning, |
1795 |
| - stacklevel=2 |
1796 |
| - ) |
1797 |
| - |
1798 |
| - if f is None: |
1799 |
| - if repr: |
1800 |
| - return multiarray.set_string_function(_default_array_repr, 1) |
1801 |
| - else: |
1802 |
| - return multiarray.set_string_function(_default_array_str, 0) |
1803 |
| - else: |
1804 |
| - return multiarray.set_string_function(f, repr) |
0 commit comments