diff options
| author | sblondon <sblondon@users.noreply.github.com> | 2021-03-24 09:23:20 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-03-24 01:23:20 -0700 |
| commit | 3ba3d513b1e3c63d09cb798b982a9e6c369cea4c (patch) | |
| tree | 22b40601f7e0464896a42a82bf7146b943830866 /Lib | |
| parent | a02683ac38183fa3a45c32319dfd329c5e622f0e (diff) | |
| download | cpython-git-3ba3d513b1e3c63d09cb798b982a9e6c369cea4c.tar.gz | |
bpo-42914: add a pprint underscore_numbers option (GH-24864)
pprint() gains a new boolean underscore_numbers kwarg to emit
integers with thousands separated by an underscore character
for improved readability (for example 1_000_000 instead of 1000000).
Diffstat (limited to 'Lib')
| -rw-r--r-- | Lib/pprint.py | 21 | ||||
| -rw-r--r-- | Lib/test/test_pprint.py | 13 |
2 files changed, 28 insertions, 6 deletions
diff --git a/Lib/pprint.py b/Lib/pprint.py index a8af50e5a6..b45cfdd99a 100644 --- a/Lib/pprint.py +++ b/Lib/pprint.py @@ -45,18 +45,19 @@ __all__ = ["pprint","pformat","isreadable","isrecursive","saferepr", def pprint(object, stream=None, indent=1, width=80, depth=None, *, - compact=False, sort_dicts=True): + compact=False, sort_dicts=True, underscore_numbers=False): """Pretty-print a Python object to a stream [default is sys.stdout].""" printer = PrettyPrinter( stream=stream, indent=indent, width=width, depth=depth, - compact=compact, sort_dicts=sort_dicts) + compact=compact, sort_dicts=sort_dicts, underscore_numbers=False) printer.pprint(object) def pformat(object, indent=1, width=80, depth=None, *, - compact=False, sort_dicts=True): + compact=False, sort_dicts=True, underscore_numbers=False): """Format a Python object into a pretty-printed representation.""" return PrettyPrinter(indent=indent, width=width, depth=depth, - compact=compact, sort_dicts=sort_dicts).pformat(object) + compact=compact, sort_dicts=sort_dicts, + underscore_numbers=underscore_numbers).pformat(object) def pp(object, *args, sort_dicts=False, **kwargs): """Pretty-print a Python object""" @@ -102,7 +103,7 @@ def _safe_tuple(t): class PrettyPrinter: def __init__(self, indent=1, width=80, depth=None, stream=None, *, - compact=False, sort_dicts=True): + compact=False, sort_dicts=True, underscore_numbers=False): """Handle pretty printing operations onto a stream using a set of configured parameters. @@ -143,6 +144,7 @@ class PrettyPrinter: self._stream = _sys.stdout self._compact = bool(compact) self._sort_dicts = sort_dicts + self._underscore_numbers = underscore_numbers def pprint(self, object): self._format(object, self._stream, 0, 0, {}, 0) @@ -525,6 +527,13 @@ class PrettyPrinter: return repr(object), True, False r = getattr(typ, "__repr__", None) + + if issubclass(typ, int) and r is int.__repr__: + if self._underscore_numbers: + return f"{object:_d}", True, False + else: + return repr(object), True, False + if issubclass(typ, dict) and r is dict.__repr__: if not object: return "{}", True, False @@ -592,7 +601,7 @@ class PrettyPrinter: rep = repr(object) return rep, (rep and not rep.startswith('<')), False -_builtin_scalars = frozenset({str, bytes, bytearray, int, float, complex, +_builtin_scalars = frozenset({str, bytes, bytearray, float, complex, bool, type(None)}) def _recursion(object): diff --git a/Lib/test/test_pprint.py b/Lib/test/test_pprint.py index c4a8578a9f..e5d2ac52d1 100644 --- a/Lib/test/test_pprint.py +++ b/Lib/test/test_pprint.py @@ -206,6 +206,7 @@ class QueryTestCase(unittest.TestCase): self.assertEqual(pprint.pformat(simple), native) self.assertEqual(pprint.pformat(simple, width=1, indent=0) .replace('\n', ' '), native) + self.assertEqual(pprint.pformat(simple, underscore_numbers=True), native) self.assertEqual(pprint.saferepr(simple), native) def test_container_repr_override_called(self): @@ -323,6 +324,18 @@ class QueryTestCase(unittest.TestCase): '1 ' '2']]]]]""") + def test_integer(self): + self.assertEqual(pprint.pformat(1234567), '1234567') + self.assertEqual(pprint.pformat(1234567, underscore_numbers=True), '1_234_567') + + class Temperature(int): + def __new__(cls, celsius_degrees): + return super().__new__(Temperature, celsius_degrees) + def __repr__(self): + kelvin_degrees = self + 273.15 + return f"{kelvin_degrees}°K" + self.assertEqual(pprint.pformat(Temperature(1000)), '1273.15°K') + def test_sorted_dict(self): # Starting in Python 2.5, pprint sorts dict displays by key regardless # of how small the dictionary may be. |
