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 work well for counters, sometimes shows them like a dict #65741

Closed
cool-RR mannequin opened this issue May 20, 2014 · 5 comments
Closed

pprint doesn't work well for counters, sometimes shows them like a dict #65741

cool-RR mannequin opened this issue May 20, 2014 · 5 comments
Labels
stdlib Python modules in the Lib dir

Comments

@cool-RR
Copy link
Mannequin

cool-RR mannequin commented May 20, 2014

BPO 21542
Nosy @rhettinger, @cool-RR, @4kir4
Superseder
  • bpo-7434: general pprint rewrite
  • 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 2014-05-22.22:44:39.810>
    created_at = <Date 2014-05-20.15:32:41.402>
    labels = ['library']
    title = "pprint doesn't work well for counters, sometimes shows them like a dict"
    updated_at = <Date 2014-05-22.23:14:02.294>
    user = 'https://github.com/cool-RR'

    bugs.python.org fields:

    activity = <Date 2014-05-22.23:14:02.294>
    actor = 'rhettinger'
    assignee = 'none'
    closed = True
    closed_date = <Date 2014-05-22.22:44:39.810>
    closer = 'rhettinger'
    components = ['Library (Lib)']
    creation = <Date 2014-05-20.15:32:41.402>
    creator = 'cool-RR'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 21542
    keywords = []
    message_count = 5.0
    messages = ['218848', '218872', '218884', '218928', '218934']
    nosy_count = 3.0
    nosy_names = ['rhettinger', 'cool-RR', 'akira']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = None
    status = 'closed'
    superseder = '7434'
    type = None
    url = 'https://bugs.python.org/issue21542'
    versions = ['Python 3.4']

    @cool-RR
    Copy link
    Mannequin Author

    cool-RR mannequin commented May 20, 2014

    pprint doesn't work well for counters, sometimes shows them like a dict 
        
        Python 3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:25:23) [MSC v.1600 64 bit (AMD64)] on win32
        Type "help", "copyright", "credits" or "license" for more information.
        >>> dd={'a': 11640, 'b': 2614, 'c': 5261, 'd': 5311, 'e': 17722, 'f': 3041, 'g': 3570, 'h': 6740, 'i':
         10729, 'j': 279, 'k': 1812, 'l': 6391, 'm': 5037, 'n': 10111, 'o': 11922, 'p': 3863, 'q': 99, 'r': 94
        61, 's': 9444, 't': 13744, 'u': 4027, 'v': 1486, 'w': 3194, 'x': 540, 'y': 2976, 'z': 203}
        >>> import collections
        >>> collections.Counter(dd)
        Counter({'e': 17722, 't': 13744, 'o': 11922, 'a': 11640, 'i': 10729, 'n': 10111, 'r': 9461, 's': 9444,
         'h': 6740, 'l': 6391, 'd': 5311, 'c': 5261, 'm': 5037, 'u': 4027, 'p': 3863, 'g': 3570, 'w': 3194, 'f
        ': 3041, 'y': 2976, 'b': 2614, 'k': 1812, 'v': 1486, 'x': 540, 'j': 279, 'z': 203, 'q': 99})
        >>> import pprint
        >>> pprint.pprint(collections.Counter(dd))
        {'a': 11640,
         'b': 2614,
         'c': 5261,
         'd': 5311,
         'e': 17722,
         'f': 3041,
         'g': 3570,
         'h': 6740,
         'i': 10729,
         'j': 279,
         'k': 1812,
         'l': 6391,
         'm': 5037,
         'n': 10111,
         'o': 11922,
         'p': 3863,
         'q': 99,
         'r': 9461,
         's': 9444,
         't': 13744,
         'u': 4027,
         'v': 1486,
         'w': 3194,
         'x': 540,
         'y': 2976,
         'z': 203}
        >>>

    @cool-RR cool-RR mannequin added the stdlib Python modules in the Lib dir label May 20, 2014
    @4kir4
    Copy link
    Mannequin

    4kir4 mannequin commented May 21, 2014

    If it fits on a line then it seems Counter's repr is used:

      >>> pprint(Counter({i:i*i for i in range(10)}))
      Counter({9: 81, 8: 64, 7: 49, 6: 36, 5: 25, 4: 16, 3: 9, 2: 4, 1: 1, 0: 0})

    Otherwise It is shown as a dict (Counter is a dict subclass) if it is too
    large (multi-line):

      >>> pprint(Counter({i:i*i for i in range(10)}), width=20)
      {0: 0,
       1: 1,
       2: 4,
       3: 9,
       4: 16,
       5: 25,
       6: 36,
       7: 49,
       8: 64,
       9: 81}
     
    the behaviour is weird but pprint doesn't promise that custom objects such 
    as Counter that can't be created using Python literals will be printed in a 
    reversible manner.

    It seems there is a special support for some objects:

      >>> pprint(frozenset({i for i in range(10)}))
      frozenset({0, 1, 2, 3, 4, 5, 6, 7, 8, 9})
      >>> pprint(frozenset({i for i in range(10)}), width=20)
      frozenset({0,
                 1,
                 2,
                 3,
                 4,
                 5,
                 6,
                 7,
                 8,
                 9})

    Perhaps the support for Counter could be added using functools.singledispatch
    and/or __prettyprint__ hook from issue bpo-7434

    @rhettinger
    Copy link
    Contributor

    I think this tracker item should be subsumed by the effort to write a better pprint API. That would be better than trying to hack special cases into the current code which doesn't allow straight-forward customizations or extensions.

    I recommend this entry be closed as a known problem that is just one of many and needs to be solved in a much larger context.

    @cool-RR
    Copy link
    Mannequin Author

    cool-RR mannequin commented May 22, 2014

    Maybe though this item should result in at least a test case for the future pprint redesign?

    @rhettinger
    Copy link
    Contributor

    Please add a note to the other tracker item.

    @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
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant