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

Use terminal width by default in pprint #74182

Open
serhiy-storchaka opened this issue Apr 5, 2017 · 13 comments
Open

Use terminal width by default in pprint #74182

serhiy-storchaka opened this issue Apr 5, 2017 · 13 comments
Labels
3.8 (EOL) end of life 3.9 only security fixes 3.10 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@serhiy-storchaka
Copy link
Member

serhiy-storchaka commented Apr 5, 2017

BPO 29996
Nosy @freddrake, @rhettinger, @merwok, @serhiy-storchaka, @eryksun, @MojoVampire, @zhangyangyu
PRs
  • bpo-29996: The width of the terminal is now used as default width in pprint() #1008
  • 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 = None
    closed_at = <Date 2021-03-20.08:38:43.627>
    created_at = <Date 2017-04-05.16:29:16.812>
    labels = ['3.8', 'type-feature', 'library', '3.9', '3.10']
    title = 'Use terminal width by default in pprint'
    updated_at = <Date 2021-03-20.08:38:43.626>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2021-03-20.08:38:43.626>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-03-20.08:38:43.627>
    closer = 'serhiy.storchaka'
    components = ['Library (Lib)']
    creation = <Date 2017-04-05.16:29:16.812>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 29996
    keywords = []
    message_count = 12.0
    messages = ['291187', '291195', '291196', '291197', '291238', '291239', '291240', '291243', '291244', '291248', '301157', '389149']
    nosy_count = 7.0
    nosy_names = ['fdrake', 'rhettinger', 'eric.araujo', 'serhiy.storchaka', 'eryksun', 'josh.r', 'xiang.zhang']
    pr_nums = ['1008']
    priority = 'normal'
    resolution = 'rejected'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue29996'
    versions = ['Python 3.8', 'Python 3.9', 'Python 3.10']

    Linked PRs

    @serhiy-storchaka
    Copy link
    Member Author

    pprint() uses width=80 by default. But default output stream is sys.stdout which often is connected to a terminal, and terminals now usually have larger width than 80 columns.

    Proposed patch change the default value of the width parameter in pprint(). If the width is specified and the output is a terminal, then the width of the terminal is used.

    @serhiy-storchaka serhiy-storchaka added 3.7 (EOL) end of life stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Apr 5, 2017
    @rhettinger
    Copy link
    Contributor

    Some thoughts:

    • I'm not sure the terminal width will always be knowable, for example when using IDLE.

    • I often need smaller widths when printing short dicts and lists. The smaller widths better show the parallel structure. I think having it fill the width of my screen is rarely what I would want.

    • ISTM, this will cause reproducibility issues, esp for doctests.

    @freddrake
    Copy link
    Member

    This is not a problem for doctests, since the output stream is not a terminal; the check for terminal-ness seems reasonable. (Though I don't have any idea if it works on Windows, but it seems properly factored.)

    @serhiy-storchaka
    Copy link
    Member Author

    • In IDLE sys.stdout.isatty() returns True, but sys.stdout.fileno() raises an exception. Terminal width can't be determined and the default 80 is used.

    • You always can pass explicit width to pprint. This may be needed even with default width 80. Now on wide a terminal wider 80 columns you have unused space than can cause unneeded wrapping. On a terminal narrower 80 columns the pprint() output is formatted incorrectly and is wrapped twice: by pprint() and by terminal.

    • The output in doctests is not connected to a terminal.

    @rhettinger
    Copy link
    Contributor

    After more thought, put me down for -1 on this proposal.

    I use pprint() quite a bit when teaching Python courses. I expect that 100% of the time that users are following the live examples, I will get stopped and asked why their output is different from mine.

    Also, I question the usefulness of the auto-resizing. When I pprint a dir() listing or some such, the intended effect is almost always that I want a vertical presentation. If a horizontal one-liner was desired, I would have used print() instead. ISTM, this proposal is out of touch with the actual needs and desires of users.

    As a reference point, consider that IPython pretty prints output by default and is not terminal width sensitive.

    @rhettinger
    Copy link
    Contributor

    One other thought: This module has a very long history. It is widely used, has had many feature requests and bug reports, but this particular feature has never been requested. That should tell us something about whether there is an actual need. To me, this seems like a new made-up requirement, invented in complete isolation from users.

    @freddrake
    Copy link
    Member

    I wouldn't go so far as to say it's never come up; it's something I've thought about a number of times, and I've waffled on it a few times.

    It's not fundamentally unreasonable to want it to adapt to the current terminal window, though I think it would be in a minority among the Unix command line tools that I use.

    Raymond's point about potentially confusing students is an important one, though. If adaptive behavior is sufficiently desirable, the module should check the value of an environment variable, and default to the current behavior. PYTHONADAPTIVEPPRINT ?

    @MojoVampire
    Copy link
    Mannequin

    MojoVampire mannequin commented Apr 6, 2017

    If you want the dir listing output in a column, you're just relying on your listing to be wider than 80 characters, it still doesn't columnize unless you pass that point.

    Personally, I feel if I made the terminal wider, I'd like to actually use that width.

    I suppose an alternative, if you really don't want autosizing, is to make auto-sizing supported without having to explicitly call getterminalsize over and over, perhaps by creating some sentinel value (pprint.AUTOWIDTH or whatever) to make it trivial to do (or to allow those who desire it to use partial binding to make it the default for a session up front).

    @MojoVampire
    Copy link
    Mannequin

    MojoVampire mannequin commented Apr 6, 2017

    Thus, you keep your default behavior of width=80, while I just add the following to my PYTHONSTARTUP file:

    from functools import partial
    from pprint import pprint, AUTOWIDTH
    
    pprint = partial(pprint, width=AUTOWIDTH)

    and we both get what we want.

    @zhangyangyu
    Copy link
    Member

    I use screens and usually like to open a terminal to fill the screen. One of my use case is changed by this patch from:

             'manufacturers_specifications': '2U 2*Intel Xeon E5-2603 4核 8*8G '
                                             'DDR3-1333 ECC 1.35v,12*300G 15K '
                                             'RPM 3.5寸 SAS,RAID '
                                             '卡带电池cache大于等于1GB,支持 Raid0 1 10 5 '
                                             '6 50 60[D23]',
    

    to:

             'manufacturers_specifications': '2U 2*Intel Xeon E5-2603 4核 8*8G DDR3-1333 ECC 1.35v,12*300G 15K RPM 3.5寸 SAS,RAID 卡带电池cache大于等于1GB,支持 Raid0 1 10 5 6 50 60[D23]',
    

    Actually looks not bad. But why change the default behaviour? Can we provide this as a bonus and keep the default behaviour? Current defaults 80 is not unreasonable even for large terminals. If users prefer long lines and don't want to choose an arbitrary number, they could use this feature.

    @merwok
    Copy link
    Member

    merwok commented Sep 1, 2017

    I was about to open the same issue! I tend to print nested data structures for debugging; having lists take up a lot of vertical screen estate and leave unused all the horizontal space is an annoyance, so that I regularly have to go back and add the width param.

    Contrary to Fred, I think my tools tend to use the available width, but then I think that auto-width is a good behaviour for a tool, but maybe not a good change for a general library. Raymond’s note about students is also an important point. I don’t like adding envvars to control the behaviour of a module function at a distance, or magic constants; if I can pass a constant, I can pass a number (for quick debugging) or handle getting the terminal width at a higher layer (in a real command-line program). For my debugging use case, maybe it’s time I switch to q (on PyPI) and save even more typing.

    Even though I was about to request the same feature, I’m now -0.

    @eryksun eryksun added 3.8 (EOL) end of life 3.9 only security fixes 3.10 only security fixes and removed 3.7 (EOL) end of life labels Mar 20, 2021
    @serhiy-storchaka
    Copy link
    Member Author

    Closing then. I'll just add my implementation in my .pythonrc.py.

    @serhiy-storchaka
    Copy link
    Member Author

    Reopened because of a new discussion: https://discuss.python.org/t/the-pprint-module-should-use-the-terminal-width-where-available/27658.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 (EOL) end of life 3.9 only security fixes 3.10 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    6 participants