Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pprint doesn't use all width #63304

Closed
serhiy-storchaka opened this issue Sep 27, 2013 · 7 comments
Closed

pprint doesn't use all width #63304

serhiy-storchaka opened this issue Sep 27, 2013 · 7 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@serhiy-storchaka
Copy link
Member

BPO 19105
Nosy @freddrake, @pitrou, @serhiy-storchaka
Files
  • pprint_all_width.patch
  • pprint_all_width_2.patch
  • pprint_all_width_3.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/serhiy-storchaka'
    closed_at = <Date 2015-02-14.14:09:23.510>
    created_at = <Date 2013-09-27.13:50:34.595>
    labels = ['type-feature', 'library']
    title = "pprint doesn't use all width"
    updated_at = <Date 2015-04-05.05:24:43.198>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2015-04-05.05:24:43.198>
    actor = 'python-dev'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2015-02-14.14:09:23.510>
    closer = 'serhiy.storchaka'
    components = ['Library (Lib)']
    creation = <Date 2013-09-27.13:50:34.595>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = ['31945', '37308', '37517']
    hgrepos = []
    issue_num = 19105
    keywords = ['patch', 'needs review']
    message_count = 7.0
    messages = ['198475', '198851', '231656', '231824', '232982', '235954', '240104']
    nosy_count = 4.0
    nosy_names = ['fdrake', 'pitrou', 'python-dev', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue19105'
    versions = ['Python 3.5']

    @serhiy-storchaka
    Copy link
    Member Author

    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.

    @serhiy-storchaka serhiy-storchaka added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Sep 27, 2013
    @serhiy-storchaka
    Copy link
    Member Author

    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]]]]]]]]]]

    @serhiy-storchaka
    Copy link
    Member Author

    Could anyone please make a review?

    @serhiy-storchaka
    Copy link
    Member Author

    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?

    @serhiy-storchaka
    Copy link
    Member Author

    Resolved conflicts with bpo-19104.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Feb 14, 2015

    New changeset 7a6671d491da by Serhiy Storchaka in branch 'default':
    Issue bpo-19105: pprint now more efficiently uses free space at the right.
    https://hg.python.org/cpython/rev/7a6671d491da

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Apr 5, 2015

    New changeset 6d9520e2223f by Serhiy Storchaka in branch 'default':
    Updated pprint examples in according to issue bpo-19105.
    https://hg.python.org/cpython/rev/6d9520e2223f

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant