classification
Title: pprint doesn't use all width
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: fdrake, pitrou, python-dev, serhiy.storchaka
Priority: normal Keywords: needs review, patch

Created on 2013-09-27 13:50 by serhiy.storchaka, last changed 2015-04-05 05:24 by python-dev. This issue is now closed.

Files
File name Uploaded Description Edit
pprint_all_width.patch serhiy.storchaka, 2013-10-02 16:47 review
pprint_all_width_2.patch serhiy.storchaka, 2014-11-28 19:38 review
pprint_all_width_3.patch serhiy.storchaka, 2014-12-20 19:48 review
Messages (7)
msg198475 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-09-27 13:50
pprint not only adds indentation, but also increases right margin for nested items.

>>> pprint.pprint([' '.join(str(i) for i in range(30))]*2)
['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',
 '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']
>>> pprint.pprint([[[[[[[[[[[[' '.join(str(i) for i in range(30))]]]]]]]]]]]]*2)
[[[[[[[[[[[['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']]]]]]]]]]],
 [[[[[[[[[[['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']]]]]]]]]]]]

As you can see in second example there is a place for at least '21 22 23 ' at the right. In case of multiline representation of nested items pprint can reserve space only at the end of last line and continue inner lines to full width.
msg198851 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-10-02 16:47
Here is a patch.

Without patch:

>>> pprint.pprint([[[[[[[[[[0, 0, 0]]*3]]]]]]]], width=21)
[[[[[[[[[[0,
          0,
          0],
         [0,
          0,
          0],
         [0,
          0,
          0]]]]]]]]]]

With patch:

>>> pprint.pprint([[[[[[[[[[0, 0, 0]]*3]]]]]]]], width=21)
[[[[[[[[[[0, 0, 0],
         [0, 0, 0],
         [0,
          0,
          0]]]]]]]]]]
msg231656 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-11-25 12:45
Could anyone please make a review?
msg231824 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-11-28 19:38
Thank you Antoine for your review.

But first variant of the patch doesn't affect an example at the top of this issue, it doesn't change string formatting. The second variant makes string formatting use all free space at the right. With the patch:

>>> import pprint
>>> print('='*80)  # a rule
================================================================================
>>> pprint.pprint([' '.join(str(i) for i in range(30))]*2)
['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',
 '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']
>>> pprint.pprint([[[[[[[[[[[[' '.join(str(i) for i in range(30))]]]]]]]]]]]]*2)
[[[[[[[[[[[['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']]]]]]]]]]],
 [[[[[[[[[[['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']]]]]]]]]]]]

Could you please make a review of new patch?
msg232982 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-12-20 19:48
Resolved conflicts with issue19104.
msg235954 - (view) Author: Roundup Robot (python-dev) Date: 2015-02-14 09:02
New changeset 7a6671d491da by Serhiy Storchaka in branch 'default':
Issue #19105: pprint now more efficiently uses free space at the right.
https://hg.python.org/cpython/rev/7a6671d491da
msg240104 - (view) Author: Roundup Robot (python-dev) Date: 2015-04-05 05:24
New changeset 6d9520e2223f by Serhiy Storchaka in branch 'default':
Updated pprint examples in according to issue #19105.
https://hg.python.org/cpython/rev/6d9520e2223f
History
Date User Action Args
2015-04-05 05:24:43python-devsetmessages: + msg240104
2015-02-14 14:09:23serhiy.storchakasetstatus: open -> closed
assignee: serhiy.storchaka
resolution: fixed
stage: patch review -> resolved
2015-02-14 09:02:38python-devsetnosy: + python-dev
messages: + msg235954
2014-12-20 19:48:38serhiy.storchakasetfiles: + pprint_all_width_3.patch

messages: + msg232982
2014-12-20 18:50:23serhiy.storchakalinkissue17530 dependencies
2014-11-28 19:38:51serhiy.storchakasetfiles: + pprint_all_width_2.patch

messages: + msg231824
2014-11-25 12:45:53serhiy.storchakasetkeywords: + needs review

messages: + msg231656
versions: + Python 3.5, - Python 3.4
2013-10-02 16:47:56serhiy.storchakasetfiles: + pprint_all_width.patch
keywords: + patch
messages: + msg198851

stage: needs patch -> patch review
2013-09-27 13:50:34serhiy.storchakacreate