From 30152b840a103383a843d426fe526e8f1992ffce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Lapeyre?= Date: Wed, 6 Feb 2019 11:58:09 +0100 Subject: [PATCH 01/10] Add pp function to the pprint module Add sort_dicts to pprint.PrettyPrinter, pprint.pformat and pprint.pprint --- Doc/library/pprint.rst | 40 ++++++++++++---- Lib/pprint.py | 48 ++++++++++++------- Lib/test/test_pprint.py | 7 +++ .../2019-02-06-12-07-46.bpo-30670.yffB3F.rst | 4 ++ 4 files changed, 74 insertions(+), 25 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2019-02-06-12-07-46.bpo-30670.yffB3F.rst diff --git a/Doc/library/pprint.rst b/Doc/library/pprint.rst index deadf182085159..b9d214dc3fa616 100644 --- a/Doc/library/pprint.rst +++ b/Doc/library/pprint.rst @@ -33,7 +33,7 @@ The :mod:`pprint` module defines one class: .. index:: single: ...; placeholder .. class:: PrettyPrinter(indent=1, width=80, depth=None, stream=None, *, \ - compact=False) + compact=False, sort_dicts=True) Construct a :class:`PrettyPrinter` instance. This constructor understands several keyword parameters. An output stream may be set using the *stream* @@ -50,7 +50,13 @@ The :mod:`pprint` module defines one class: structure cannot be formatted within the constrained width, a best effort will be made. If *compact* is false (the default) each item of a long sequence will be formatted on a separate line. If *compact* is true, as many items - as will fit within the *width* will be formatted on each output line. + as will fit within the *width* will be formatted on each output line. If + *sort_dicts* is true (the default), dictionaries will be formatted with their + keys sorted alphabetically, otherwise they will be sorted by their insertion + order. + + .. versionchanged:: 3.8 + Added the *sort_dicts* parameter. .. versionchanged:: 3.4 Added the *compact* parameter. @@ -81,25 +87,43 @@ The :mod:`pprint` module defines one class: The :mod:`pprint` module also provides several shortcut functions: -.. function:: pformat(object, indent=1, width=80, depth=None, *, compact=False) +.. function:: pformat(object, indent=1, width=80, depth=None, *, \ + compact=False, sort_dicts=True) Return the formatted representation of *object* as a string. *indent*, - *width*, *depth* and *compact* will be passed to the :class:`PrettyPrinter` - constructor as formatting parameters. + *width*, *depth*, *compact* and *sort_dicts* will be passed to the + :class:`PrettyPrinter` constructor as formatting parameters. + + .. versionchanged:: 3.8 + Added the *sort_dicts* parameter. .. versionchanged:: 3.4 Added the *compact* parameter. +.. function:: pp(object, *args, sort_dicts=False, **kwargs) + + Prints the formatted representation of *object* followed by a newline. + If *sort_dicts* is false (the default), dictionaries will be displayed with + their keys sorted in insertion order, otherwise they will be sorted + alphabetically. *args* an *kwargs* will be passed to :function:`pprint` as + formatting paramaters. + + .. versionadded:: 3.8 + + .. function:: pprint(object, stream=None, indent=1, width=80, depth=None, *, \ - compact=False) + compact=False, sort_dicts=True) Prints the formatted representation of *object* on *stream*, followed by a newline. If *stream* is ``None``, ``sys.stdout`` is used. This may be used in the interactive interpreter instead of the :func:`print` function for inspecting values (you can even reassign ``print = pprint.pprint`` for use - within a scope). *indent*, *width*, *depth* and *compact* will be passed - to the :class:`PrettyPrinter` constructor as formatting parameters. + within a scope). *indent*, *width*, *depth*, *compact* and *sort_dicts* will + be passed to the :class:`PrettyPrinter` constructor as formatting parameters. + + .. versionchanged:: 3.8 + Added the *sort_dicts* parameter. .. versionchanged:: 3.4 Added the *compact* parameter. diff --git a/Lib/pprint.py b/Lib/pprint.py index f2a117864e5e15..04bbe5a62157ce 100644 --- a/Lib/pprint.py +++ b/Lib/pprint.py @@ -45,29 +45,34 @@ def pprint(object, stream=None, indent=1, width=80, depth=None, *, - compact=False): + compact=False, sort_dicts=True): """Pretty-print a Python object to a stream [default is sys.stdout].""" printer = PrettyPrinter( stream=stream, indent=indent, width=width, depth=depth, - compact=compact) + compact=compact, sort_dicts=sort_dicts) printer.pprint(object) -def pformat(object, indent=1, width=80, depth=None, *, compact=False): +def pformat(object, indent=1, width=80, depth=None, *, + compact=False, sort_dicts=True): """Format a Python object into a pretty-printed representation.""" return PrettyPrinter(indent=indent, width=width, depth=depth, - compact=compact).pformat(object) + compact=compact, sort_dicts=sort_dicts).pformat(object) + +def pp(object, *args, sort_dicts=False, **kwargs): + """Pretty-print a Python object""" + pprint(object, *args, sort_dicts=sort_dicts, **kwargs) def saferepr(object): """Version of repr() which can handle recursive data structures.""" - return _safe_repr(object, {}, None, 0)[0] + return _safe_repr(object, {}, None, 0, True)[0] def isreadable(object): """Determine if saferepr(object) is readable by eval().""" - return _safe_repr(object, {}, None, 0)[1] + return _safe_repr(object, {}, None, 0, True)[1] def isrecursive(object): """Determine if object requires a recursive representation.""" - return _safe_repr(object, {}, None, 0)[2] + return _safe_repr(object, {}, None, 0, True)[2] class _safe_key: """Helper function for key functions when sorting unorderable objects. @@ -97,7 +102,7 @@ def _safe_tuple(t): class PrettyPrinter: def __init__(self, indent=1, width=80, depth=None, stream=None, *, - compact=False): + compact=False, sort_dicts=True): """Handle pretty printing operations onto a stream using a set of configured parameters. @@ -117,6 +122,9 @@ def __init__(self, indent=1, width=80, depth=None, stream=None, *, compact If true, several items will be combined in one line. + sort_dicts + If true, dicts key are sorted alphabetically. + """ indent = int(indent) width = int(width) @@ -134,6 +142,7 @@ def __init__(self, indent=1, width=80, depth=None, stream=None, *, else: self._stream = _sys.stdout self._compact = bool(compact) + self._sort_dicts = sort_dicts def pprint(self, object): self._format(object, self._stream, 0, 0, {}, 0) @@ -184,7 +193,10 @@ def _pprint_dict(self, object, stream, indent, allowance, context, level): write((self._indent_per_level - 1) * ' ') length = len(object) if length: - items = sorted(object.items(), key=_safe_tuple) + if self._sort_dicts: + items = sorted(object.items(), key=_safe_tuple) + else: + items = object.items() self._format_dict_items(items, stream, indent, allowance + 1, context, level) write('}') @@ -402,7 +414,7 @@ def format(self, object, context, maxlevels, level): and flags indicating whether the representation is 'readable' and whether the object represents a recursive construct. """ - return _safe_repr(object, context, maxlevels, level) + return _safe_repr(object, context, maxlevels, level, self._sort_dicts) def _pprint_default_dict(self, object, stream, indent, allowance, context, level): if not len(object): @@ -487,7 +499,7 @@ def _pprint_user_string(self, object, stream, indent, allowance, context, level) # Return triple (repr_string, isreadable, isrecursive). -def _safe_repr(object, context, maxlevels, level): +def _safe_repr(object, context, maxlevels, level, sort_dicts): typ = type(object) if typ in _builtin_scalars: return repr(object), True, False @@ -507,11 +519,13 @@ def _safe_repr(object, context, maxlevels, level): components = [] append = components.append level += 1 - saferepr = _safe_repr - items = sorted(object.items(), key=_safe_tuple) + if sort_dicts: + items = sorted(object.items(), key=_safe_tuple) + else: + items = object.items() for k, v in items: - krepr, kreadable, krecur = saferepr(k, context, maxlevels, level) - vrepr, vreadable, vrecur = saferepr(v, context, maxlevels, level) + krepr, kreadable, krecur = _safe_repr(k, context, maxlevels, level, sort_dicts) + vrepr, vreadable, vrecur = _safe_repr(v, context, maxlevels, level, sort_dicts) append("%s: %s" % (krepr, vrepr)) readable = readable and kreadable and vreadable if krecur or vrecur: @@ -543,7 +557,7 @@ def _safe_repr(object, context, maxlevels, level): append = components.append level += 1 for o in object: - orepr, oreadable, orecur = _safe_repr(o, context, maxlevels, level) + orepr, oreadable, orecur = _safe_repr(o, context, maxlevels, level, sort_dicts) append(orepr) if not oreadable: readable = False @@ -569,7 +583,7 @@ def _perfcheck(object=None): object = [("string", (1, 2), [3, 4], {5: 6, 7: 8})] * 100000 p = PrettyPrinter() t1 = time.perf_counter() - _safe_repr(object, {}, None, 0) + _safe_repr(object, {}, None, 0, True) t2 = time.perf_counter() p.pformat(object) t3 = time.perf_counter() diff --git a/Lib/test/test_pprint.py b/Lib/test/test_pprint.py index 7ebc298337ad5c..414c874c11ca05 100644 --- a/Lib/test/test_pprint.py +++ b/Lib/test/test_pprint.py @@ -81,6 +81,7 @@ def test_init(self): pp = pprint.PrettyPrinter(indent=4, width=40, depth=5, stream=io.StringIO(), compact=True) pp = pprint.PrettyPrinter(4, 40, 5, io.StringIO()) + pp = pprint.PrettyPrinter(sort_dicts=False) with self.assertRaises(TypeError): pp = pprint.PrettyPrinter(4, 40, 5, io.StringIO(), True) self.assertRaises(ValueError, pprint.PrettyPrinter, indent=-1) @@ -293,6 +294,12 @@ def test_sorted_dict(self): self.assertEqual(pprint.pformat({"xy\tab\n": (3,), 5: [[]], (): {}}), r"{5: [[]], 'xy\tab\n': (3,), (): {}}") + def test_sort_dict(self): + d = dict.fromkeys('cba') + self.assertEqual(pprint.pformat(d, sort_dicts=False), "{'c': None, 'b': None, 'a': None}") + self.assertEqual(pprint.pformat([d, d], sort_dicts=False), + "[{'c': None, 'b': None, 'a': None}, {'c': None, 'b': None, 'a': None}]") + def test_ordered_dict(self): d = collections.OrderedDict() self.assertEqual(pprint.pformat(d, width=1), 'OrderedDict()') diff --git a/Misc/NEWS.d/next/Library/2019-02-06-12-07-46.bpo-30670.yffB3F.rst b/Misc/NEWS.d/next/Library/2019-02-06-12-07-46.bpo-30670.yffB3F.rst new file mode 100644 index 00000000000000..6b06a4bd5d2e05 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-02-06-12-07-46.bpo-30670.yffB3F.rst @@ -0,0 +1,4 @@ +:func:`pprint.pp` has been added to pretty-print objects with dictionaries +keys being sorted with their insertion order by default. Parameter +*sort_dicts* has been added to `pprint.pprint`, `pprint.pformat` and +`pprint.PrettyPrinter`. Contributed by Rémi Lapeyre. From c75298b490d7c57ed03287ceffef04698f6dbae9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Lapeyre?= Date: Wed, 6 Feb 2019 12:16:16 +0100 Subject: [PATCH 02/10] Run `make patchcheck` --- Doc/library/pprint.rst | 10 +++++----- Lib/pprint.py | 2 +- Lib/test/test_pprint.py | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Doc/library/pprint.rst b/Doc/library/pprint.rst index b9d214dc3fa616..c9d9ca9154577a 100644 --- a/Doc/library/pprint.rst +++ b/Doc/library/pprint.rst @@ -91,7 +91,7 @@ The :mod:`pprint` module also provides several shortcut functions: compact=False, sort_dicts=True) Return the formatted representation of *object* as a string. *indent*, - *width*, *depth*, *compact* and *sort_dicts* will be passed to the + *width*, *depth*, *compact* and *sort_dicts* will be passed to the :class:`PrettyPrinter` constructor as formatting parameters. .. versionchanged:: 3.8 @@ -104,9 +104,9 @@ The :mod:`pprint` module also provides several shortcut functions: .. function:: pp(object, *args, sort_dicts=False, **kwargs) Prints the formatted representation of *object* followed by a newline. - If *sort_dicts* is false (the default), dictionaries will be displayed with - their keys sorted in insertion order, otherwise they will be sorted - alphabetically. *args* an *kwargs* will be passed to :function:`pprint` as + If *sort_dicts* is false (the default), dictionaries will be displayed with + their keys sorted in insertion order, otherwise they will be sorted + alphabetically. *args* an *kwargs* will be passed to :function:`pprint` as formatting paramaters. .. versionadded:: 3.8 @@ -119,7 +119,7 @@ The :mod:`pprint` module also provides several shortcut functions: newline. If *stream* is ``None``, ``sys.stdout`` is used. This may be used in the interactive interpreter instead of the :func:`print` function for inspecting values (you can even reassign ``print = pprint.pprint`` for use - within a scope). *indent*, *width*, *depth*, *compact* and *sort_dicts* will + within a scope). *indent*, *width*, *depth*, *compact* and *sort_dicts* will be passed to the :class:`PrettyPrinter` constructor as formatting parameters. .. versionchanged:: 3.8 diff --git a/Lib/pprint.py b/Lib/pprint.py index 04bbe5a62157ce..79f7be1d9cbf81 100644 --- a/Lib/pprint.py +++ b/Lib/pprint.py @@ -52,7 +52,7 @@ def pprint(object, stream=None, indent=1, width=80, depth=None, *, compact=compact, sort_dicts=sort_dicts) printer.pprint(object) -def pformat(object, indent=1, width=80, depth=None, *, +def pformat(object, indent=1, width=80, depth=None, *, compact=False, sort_dicts=True): """Format a Python object into a pretty-printed representation.""" return PrettyPrinter(indent=indent, width=width, depth=depth, diff --git a/Lib/test/test_pprint.py b/Lib/test/test_pprint.py index 414c874c11ca05..269ac0624eeb8b 100644 --- a/Lib/test/test_pprint.py +++ b/Lib/test/test_pprint.py @@ -297,7 +297,7 @@ def test_sorted_dict(self): def test_sort_dict(self): d = dict.fromkeys('cba') self.assertEqual(pprint.pformat(d, sort_dicts=False), "{'c': None, 'b': None, 'a': None}") - self.assertEqual(pprint.pformat([d, d], sort_dicts=False), + self.assertEqual(pprint.pformat([d, d], sort_dicts=False), "[{'c': None, 'b': None, 'a': None}, {'c': None, 'b': None, 'a': None}]") def test_ordered_dict(self): From e7242c0599ad7f85ba16878b37a78964898f59fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Lapeyre?= Date: Wed, 6 Feb 2019 13:32:59 +0100 Subject: [PATCH 03/10] Fix blurb --- .../next/Library/2019-02-06-12-07-46.bpo-30670.yffB3F.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2019-02-06-12-07-46.bpo-30670.yffB3F.rst b/Misc/NEWS.d/next/Library/2019-02-06-12-07-46.bpo-30670.yffB3F.rst index 6b06a4bd5d2e05..392b7f8800ff40 100644 --- a/Misc/NEWS.d/next/Library/2019-02-06-12-07-46.bpo-30670.yffB3F.rst +++ b/Misc/NEWS.d/next/Library/2019-02-06-12-07-46.bpo-30670.yffB3F.rst @@ -1,4 +1,4 @@ -:func:`pprint.pp` has been added to pretty-print objects with dictionaries +`pprint.pp` has been added to pretty-print objects with dictionaries keys being sorted with their insertion order by default. Parameter *sort_dicts* has been added to `pprint.pprint`, `pprint.pformat` and `pprint.PrettyPrinter`. Contributed by Rémi Lapeyre. From 52f6ed3c87ac23dbac717fd41837dfff3a3f1626 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Lapeyre?= Date: Wed, 6 Feb 2019 14:16:20 +0100 Subject: [PATCH 04/10] Fix changes order in documentation --- Doc/library/pprint.rst | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Doc/library/pprint.rst b/Doc/library/pprint.rst index c9d9ca9154577a..16293b4b248a12 100644 --- a/Doc/library/pprint.rst +++ b/Doc/library/pprint.rst @@ -94,12 +94,12 @@ The :mod:`pprint` module also provides several shortcut functions: *width*, *depth*, *compact* and *sort_dicts* will be passed to the :class:`PrettyPrinter` constructor as formatting parameters. - .. versionchanged:: 3.8 - Added the *sort_dicts* parameter. - .. versionchanged:: 3.4 Added the *compact* parameter. + .. versionchanged:: 3.8 + Added the *sort_dicts* parameter. + .. function:: pp(object, *args, sort_dicts=False, **kwargs) @@ -122,12 +122,12 @@ The :mod:`pprint` module also provides several shortcut functions: within a scope). *indent*, *width*, *depth*, *compact* and *sort_dicts* will be passed to the :class:`PrettyPrinter` constructor as formatting parameters. - .. versionchanged:: 3.8 - Added the *sort_dicts* parameter. - .. versionchanged:: 3.4 Added the *compact* parameter. + .. versionchanged:: 3.8 + Added the *sort_dicts* parameter. + >>> import pprint >>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni'] >>> stuff.insert(0, stuff) From 9a7aaed35cf804b1d05a464d58fae146143ca919 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Lapeyre?= Date: Wed, 6 Feb 2019 14:44:19 +0100 Subject: [PATCH 05/10] Fix inline reference in documentation --- Doc/library/pprint.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/pprint.rst b/Doc/library/pprint.rst index 16293b4b248a12..cac56a03e1599f 100644 --- a/Doc/library/pprint.rst +++ b/Doc/library/pprint.rst @@ -106,7 +106,7 @@ The :mod:`pprint` module also provides several shortcut functions: Prints the formatted representation of *object* followed by a newline. If *sort_dicts* is false (the default), dictionaries will be displayed with their keys sorted in insertion order, otherwise they will be sorted - alphabetically. *args* an *kwargs* will be passed to :function:`pprint` as + alphabetically. *args* an *kwargs* will be passed to :func:`pprint` as formatting paramaters. .. versionadded:: 3.8 From bb2c055bdeb12754dc2d5c8885287d7edc7898f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Lapeyre?= Date: Wed, 6 Feb 2019 14:46:35 +0100 Subject: [PATCH 06/10] Fix order of change notes in documentation --- Doc/library/pprint.rst | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Doc/library/pprint.rst b/Doc/library/pprint.rst index cac56a03e1599f..f8ddadb077f8cd 100644 --- a/Doc/library/pprint.rst +++ b/Doc/library/pprint.rst @@ -55,11 +55,13 @@ The :mod:`pprint` module defines one class: keys sorted alphabetically, otherwise they will be sorted by their insertion order. + .. versionchanged:: 3.4 + Added the *compact* parameter. + .. versionchanged:: 3.8 Added the *sort_dicts* parameter. - .. versionchanged:: 3.4 - Added the *compact* parameter. + >>> import pprint >>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni'] From 765a9f968e26f644185799250a08862d3864ff98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Lapeyre?= Date: Wed, 6 Feb 2019 15:30:22 +0100 Subject: [PATCH 07/10] Fix sphinx suspicious check --- Doc/library/pprint.rst | 1 - Doc/tools/susp-ignored.csv | 18 +++++++++--------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/Doc/library/pprint.rst b/Doc/library/pprint.rst index f8ddadb077f8cd..8ff6df8c99833d 100644 --- a/Doc/library/pprint.rst +++ b/Doc/library/pprint.rst @@ -62,7 +62,6 @@ The :mod:`pprint` module defines one class: Added the *sort_dicts* parameter. - >>> import pprint >>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni'] >>> stuff.insert(0, stuff[:]) diff --git a/Doc/tools/susp-ignored.csv b/Doc/tools/susp-ignored.csv index 3c23dc12e3ad6e..f31b48de749690 100644 --- a/Doc/tools/susp-ignored.csv +++ b/Doc/tools/susp-ignored.csv @@ -180,15 +180,15 @@ library/pickle,,:memory,"conn = sqlite3.connect("":memory:"")" library/posix,,`,"CFLAGS=""`getconf LFS_CFLAGS`"" OPT=""-g -O2 $CFLAGS""" library/pprint,,::,"'Programming Language :: Python :: 2.6'," library/pprint,,::,"'Programming Language :: Python :: 2.7'," -library/pprint,225,::,"'classifiers': ['Development Status :: 3 - Alpha'," -library/pprint,225,::,"'Intended Audience :: Developers'," -library/pprint,225,::,"'License :: OSI Approved :: MIT License'," -library/pprint,225,::,"'Programming Language :: Python :: 2'," -library/pprint,225,::,"'Programming Language :: Python :: 3'," -library/pprint,225,::,"'Programming Language :: Python :: 3.2'," -library/pprint,225,::,"'Programming Language :: Python :: 3.3'," -library/pprint,225,::,"'Programming Language :: Python :: 3.4'," -library/pprint,225,::,"'Topic :: Software Development :: Build Tools']," +library/pprint,252,::,"'classifiers': ['Development Status :: 3 - Alpha'," +library/pprint,252,::,"'Intended Audience :: Developers'," +library/pprint,252,::,"'License :: OSI Approved :: MIT License'," +library/pprint,252,::,"'Programming Language :: Python :: 2'," +library/pprint,252,::,"'Programming Language :: Python :: 3'," +library/pprint,252,::,"'Programming Language :: Python :: 3.2'," +library/pprint,252,::,"'Programming Language :: Python :: 3.3'," +library/pprint,252,::,"'Programming Language :: Python :: 3.4'," +library/pprint,252,::,"'Topic :: Software Development :: Build Tools']," library/profile,,:lineno,filename:lineno(function) library/pyexpat,,:elem1, library/pyexpat,,:py,"xmlns:py = ""http://www.python.org/ns/"">" From 3f30a20c605105049c88a5c63132ee1b7f236b00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Lapeyre?= Date: Mon, 11 Feb 2019 10:07:34 +0100 Subject: [PATCH 08/10] Remove line numbers in susp-ignored.csv --- Doc/tools/susp-ignored.csv | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Doc/tools/susp-ignored.csv b/Doc/tools/susp-ignored.csv index f31b48de749690..fcd8dfa19e1323 100644 --- a/Doc/tools/susp-ignored.csv +++ b/Doc/tools/susp-ignored.csv @@ -180,15 +180,15 @@ library/pickle,,:memory,"conn = sqlite3.connect("":memory:"")" library/posix,,`,"CFLAGS=""`getconf LFS_CFLAGS`"" OPT=""-g -O2 $CFLAGS""" library/pprint,,::,"'Programming Language :: Python :: 2.6'," library/pprint,,::,"'Programming Language :: Python :: 2.7'," -library/pprint,252,::,"'classifiers': ['Development Status :: 3 - Alpha'," -library/pprint,252,::,"'Intended Audience :: Developers'," -library/pprint,252,::,"'License :: OSI Approved :: MIT License'," -library/pprint,252,::,"'Programming Language :: Python :: 2'," -library/pprint,252,::,"'Programming Language :: Python :: 3'," -library/pprint,252,::,"'Programming Language :: Python :: 3.2'," -library/pprint,252,::,"'Programming Language :: Python :: 3.3'," -library/pprint,252,::,"'Programming Language :: Python :: 3.4'," -library/pprint,252,::,"'Topic :: Software Development :: Build Tools']," +library/pprint,,::,"'classifiers': ['Development Status :: 3 - Alpha'," +library/pprint,,::,"'Intended Audience :: Developers'," +library/pprint,,::,"'License :: OSI Approved :: MIT License'," +library/pprint,,::,"'Programming Language :: Python :: 2'," +library/pprint,,::,"'Programming Language :: Python :: 3'," +library/pprint,,::,"'Programming Language :: Python :: 3.2'," +library/pprint,,::,"'Programming Language :: Python :: 3.3'," +library/pprint,,::,"'Programming Language :: Python :: 3.4'," +library/pprint,,::,"'Topic :: Software Development :: Build Tools']," library/profile,,:lineno,filename:lineno(function) library/pyexpat,,:elem1, library/pyexpat,,:py,"xmlns:py = ""http://www.python.org/ns/"">" From c286cece588aece40f761a04ef26d2205d5a066b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Lapeyre?= Date: Mon, 11 Feb 2019 10:08:17 +0100 Subject: [PATCH 09/10] Fix typo in blurb --- .../next/Library/2019-02-06-12-07-46.bpo-30670.yffB3F.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2019-02-06-12-07-46.bpo-30670.yffB3F.rst b/Misc/NEWS.d/next/Library/2019-02-06-12-07-46.bpo-30670.yffB3F.rst index 392b7f8800ff40..63cdbb363f762d 100644 --- a/Misc/NEWS.d/next/Library/2019-02-06-12-07-46.bpo-30670.yffB3F.rst +++ b/Misc/NEWS.d/next/Library/2019-02-06-12-07-46.bpo-30670.yffB3F.rst @@ -1,4 +1,4 @@ -`pprint.pp` has been added to pretty-print objects with dictionaries +`pprint.pp` has been added to pretty-print objects with dictionary keys being sorted with their insertion order by default. Parameter *sort_dicts* has been added to `pprint.pprint`, `pprint.pformat` and `pprint.PrettyPrinter`. Contributed by Rémi Lapeyre. From fbae1d0b1cf7a92ff472dd177a63226532399749 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Fri, 22 Mar 2019 09:55:19 -0700 Subject: [PATCH 10/10] Make minor touch-ups * Add "pp" to __all__ * Drop mention of alphabetical sorting. It is just plain sorting (i.e. dict with integer keys get sorted as well). * Fix spelling of "parameter" --- Doc/library/pprint.rst | 9 ++++----- Lib/pprint.py | 4 ++-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/Doc/library/pprint.rst b/Doc/library/pprint.rst index 8ff6df8c99833d..988f85bed103aa 100644 --- a/Doc/library/pprint.rst +++ b/Doc/library/pprint.rst @@ -52,8 +52,7 @@ The :mod:`pprint` module defines one class: will be formatted on a separate line. If *compact* is true, as many items as will fit within the *width* will be formatted on each output line. If *sort_dicts* is true (the default), dictionaries will be formatted with their - keys sorted alphabetically, otherwise they will be sorted by their insertion - order. + keys sorted, otherwise they will display in insertion order. .. versionchanged:: 3.4 Added the *compact* parameter. @@ -106,9 +105,9 @@ The :mod:`pprint` module also provides several shortcut functions: Prints the formatted representation of *object* followed by a newline. If *sort_dicts* is false (the default), dictionaries will be displayed with - their keys sorted in insertion order, otherwise they will be sorted - alphabetically. *args* an *kwargs* will be passed to :func:`pprint` as - formatting paramaters. + their keys in insertion order, otherwise the dict keys will be sorted. + *args* an *kwargs* will be passed to :func:`pprint` as formatting + parameters. .. versionadded:: 3.8 diff --git a/Lib/pprint.py b/Lib/pprint.py index 79f7be1d9cbf81..4bfcc31b25ea8e 100644 --- a/Lib/pprint.py +++ b/Lib/pprint.py @@ -41,7 +41,7 @@ from io import StringIO as _StringIO __all__ = ["pprint","pformat","isreadable","isrecursive","saferepr", - "PrettyPrinter"] + "PrettyPrinter", "pp"] def pprint(object, stream=None, indent=1, width=80, depth=None, *, @@ -123,7 +123,7 @@ def __init__(self, indent=1, width=80, depth=None, stream=None, *, If true, several items will be combined in one line. sort_dicts - If true, dicts key are sorted alphabetically. + If true, dict keys are sorted. """ indent = int(indent)