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

Add missing multiset predicates to collections.Counter #84932

Closed
rhettinger opened this issue May 24, 2020 · 7 comments
Closed

Add missing multiset predicates to collections.Counter #84932

rhettinger opened this issue May 24, 2020 · 7 comments
Assignees
Labels
3.10 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@rhettinger
Copy link
Contributor

BPO 40755
Nosy @rhettinger, @vedgar
PRs
  • bpo-40755: Add missing multiset operations to Counter() #20339
  • bpo-40755: Add rich comparisons to Counter #20548
  • 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/rhettinger'
    closed_at = <Date 2020-05-28.15:41:30.423>
    created_at = <Date 2020-05-24.15:11:00.498>
    labels = ['type-feature', 'library', '3.10']
    title = 'Add missing multiset predicates to collections.Counter'
    updated_at = <Date 2020-05-31.21:57:48.566>
    user = 'https://github.com/rhettinger'

    bugs.python.org fields:

    activity = <Date 2020-05-31.21:57:48.566>
    actor = 'rhettinger'
    assignee = 'rhettinger'
    closed = True
    closed_date = <Date 2020-05-28.15:41:30.423>
    closer = 'rhettinger'
    components = ['Library (Lib)']
    creation = <Date 2020-05-24.15:11:00.498>
    creator = 'rhettinger'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 40755
    keywords = ['patch']
    message_count = 7.0
    messages = ['369808', '369938', '370228', '370230', '370409', '370410', '370512']
    nosy_count = 2.0
    nosy_names = ['rhettinger', 'veky']
    pr_nums = ['20339', '20548']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue40755'
    versions = ['Python 3.10']

    @rhettinger
    Copy link
    Contributor Author

    These missing predicates have been requested a number of times:

       isequal()
       issubset()
       issuperset()
       isdisjoint()

    @rhettinger rhettinger added the 3.10 only security fixes label May 24, 2020
    @rhettinger rhettinger self-assigned this May 24, 2020
    @rhettinger rhettinger added stdlib Python modules in the Lib dir type-feature A feature request or enhancement 3.10 only security fixes labels May 24, 2020
    @rhettinger rhettinger self-assigned this May 24, 2020
    @rhettinger rhettinger added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels May 24, 2020
    @vedgar
    Copy link
    Mannequin

    vedgar mannequin commented May 26, 2020

    isequal is really strange considering we're talking about Python here. Do any of other stdlib types have that method instead of just using == (which works fine even now)? I'd even spell the second and third as <= and >=, same as set does.

    But if we're finally going to accept that Counters are just bags (CS term for multisets), then surely .add and .remove (and maybe .discard, setting the count to 0) would be more natural additions. I can't count (pun intended) all the times I had to write that '] += 1' just to count some element.

    @rhettinger
    Copy link
    Contributor Author

    New changeset 6039851 by Raymond Hettinger in branch 'master':
    bpo-40755: Add missing multiset operations to Counter() (GH-20339)
    6039851

    @rhettinger
    Copy link
    Contributor Author

    I would also have preferred to use the operators <, >, <=, >=, and ==. The docs in the patch explain why we can't go down this path.

    Also, while counters have support for multiset operations, they continue to support other use cases a well (negative counts and fractional counts). That support can't be removed without breaking existing code that relies on it.

    From the outset, a Counter was just a dictionary that return 0 for missing keys. Users are free to use that concept however they want.

    @rhettinger
    Copy link
    Contributor Author

    After more thought, I've found a way to use the rich comparisons as requested. Doing so consistently required that the __eq__ method treat missing elements as having a zero count. See attached PR.

    @vedgar
    Copy link
    Mannequin

    vedgar mannequin commented May 31, 2020

    I'm very glad for that. :-)

    For the other part of my message, I never intended to remove the support for non-natural counts. I just wanted to add some more methods to the natural part of Counter's API. It already has some methods which assume natural counts: .elements(), for example.

    @rhettinger
    Copy link
    Contributor Author

    New changeset b7d79b4 by Raymond Hettinger in branch 'master':
    bpo-40755: Add rich comparisons to Counter (GH-20548)
    b7d79b4

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

    1 participant