This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Unsupported provider

classification
Title: Add compact mode to pprint
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: pitrou, python-dev, r.david.murray, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2013-09-29 20:05 by serhiy.storchaka, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
pprint_compact.patch serhiy.storchaka, 2013-09-30 18:07 review
pprint_compact_2.patch serhiy.storchaka, 2013-09-30 19:56 review
pprint_compact_3.patch serhiy.storchaka, 2013-10-01 15:17 review
Messages (14)
msg198641 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-09-29 20:05
pprint produces not very nice output for collections with a large number of short elements (see msg198556). For example pprint.pprint(list(range(40))) outputs more than 40 short lines, while print(repr(list(range(40)))) takes only 2 lines on 80-column terminal.

I propose to add new boolean option "compact". With compact=True pprint will try combine as much short one-line subelements in one line as possible. Every multiline element will be printed on separated lines.

Examples:

>>> pprint.pprint(list(range(40)), width=50, compact=True)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
 38, 39]
>>> pprint.pprint(['one string', 'other string', 'very very long string which continued on several lines', 'and again', 'and again', 'and again', 'and again'], width=50, compact=True)
['one string', 'other string',
 'very very long string which is continued on '
 'several lines',
 'and again', 'and again', 'and again',
 'and again']
msg198643 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-09-29 20:16
I think it looks good on the principle.
msg198702 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-09-30 14:48
I like it.  If it isn't too difficult, I'd suggest that compact mode also indent the string continuation lines:

['one string', 'other string',
 'very very long string which is continued on '
  'several lines',
 'and again', 'and again', 'and again',
 'and again']
msg198711 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-09-30 18:07
Here is a patch. Please review and correct the documentation.
msg198713 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-09-30 18:09
> I'd suggest that compact mode also indent the string continuation lines:

Please open new issue for this.
msg198717 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-09-30 18:51
As noted in the review, I'm not as keen on having dictionaries displayed in compact form.
msg198726 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-09-30 19:56
Updated patch addresses David's comments. Thank you David.

> As noted in the review, I'm not as keen on having dictionaries displayed in compact form.

This makes sense. If no one will argue for compactifying mappings I'll remove this part of the patch.
msg198789 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-10-01 15:05
> As noted in the review, I'm not as keen on having dictionaries displayed
> in compact form.

Agreed with David.
msg198790 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-10-01 15:17
Updated patch doesn't compactify dicts.
msg198821 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-10-02 08:57
New changeset ae0306c8f7a3 by Serhiy Storchaka in branch 'default':
Issue #19132: The pprint module now supports compact mode.
http://hg.python.org/cpython/rev/ae0306c8f7a3
msg198836 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-10-02 13:07
Sorry I missed this on the review, but you are missing versionchanged tags (or versionadded, whichever you prefer :) and a what's new entry.
msg198855 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-10-02 17:17
I had added a what's new entry.
msg198858 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-10-02 18:40
New changeset 6e7b1aadea2f by Serhiy Storchaka in branch 'default':
Issue #19132: Add versionchanged tags.
http://hg.python.org/cpython/rev/6e7b1aadea2f
msg223761 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-07-23 19:01
New changeset 60301b9982b2 by Terry Jan Reedy in branch '2.7':
Issue #21597: Turtledemo text pane can now be widened to view or copy complete
http://hg.python.org/cpython/rev/60301b9982b2

New changeset 0fb515063324 by Terry Jan Reedy in branch '3.4':
Issue #21597: Turtledemo text pane can now be widened to view or copy complete
http://hg.python.org/cpython/rev/0fb515063324
History
Date User Action Args
2022-04-11 14:57:51adminsetgithub: 63331
2014-07-23 19:01:55python-devsetmessages: + msg223761
2013-10-02 18:40:52python-devsetmessages: + msg198858
2013-10-02 17:17:23serhiy.storchakasetmessages: + msg198855
2013-10-02 13:07:18r.david.murraysetmessages: + msg198836
2013-10-02 08:58:22serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2013-10-02 08:57:38python-devsetnosy: + python-dev
messages: + msg198821
2013-10-01 15:17:24serhiy.storchakasetfiles: + pprint_compact_3.patch

messages: + msg198790
2013-10-01 15:05:32pitrousetmessages: + msg198789
2013-09-30 19:56:06serhiy.storchakasetfiles: + pprint_compact_2.patch

messages: + msg198726
2013-09-30 18:51:08r.david.murraysetmessages: + msg198717
2013-09-30 18:09:10serhiy.storchakasetmessages: + msg198713
2013-09-30 18:07:15serhiy.storchakasetfiles: + pprint_compact.patch
keywords: + patch
messages: + msg198711

stage: needs patch -> patch review
2013-09-30 14:48:19r.david.murraysetnosy: + r.david.murray
messages: + msg198702
2013-09-29 20:22:18serhiy.storchakalinkissue19103 dependencies
2013-09-29 20:16:43pitrousetnosy: + pitrou
messages: + msg198643
2013-09-29 20:05:49serhiy.storchakacreate