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 key argument to heapq functions #46199

Closed
gpolo mannequin opened this issue Jan 22, 2008 · 3 comments
Closed

Add key argument to heapq functions #46199

gpolo mannequin opened this issue Jan 22, 2008 · 3 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@gpolo
Copy link
Mannequin

gpolo mannequin commented Jan 22, 2008

BPO 1904
Nosy @rhettinger
Files
  • heapq_key.py.patch: Adds key argument to heapq functions (C version not patched)
  • 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 2008-03-24.08:13:40.503>
    created_at = <Date 2008-01-22.10:54:02.209>
    labels = ['type-bug', 'library']
    title = 'Add key argument to heapq functions'
    updated_at = <Date 2008-03-24.08:13:40.502>
    user = 'https://bugs.python.org/gpolo'

    bugs.python.org fields:

    activity = <Date 2008-03-24.08:13:40.502>
    actor = 'rhettinger'
    assignee = 'rhettinger'
    closed = True
    closed_date = <Date 2008-03-24.08:13:40.503>
    closer = 'rhettinger'
    components = ['Library (Lib)']
    creation = <Date 2008-01-22.10:54:02.209>
    creator = 'gpolo'
    dependencies = []
    files = ['9260']
    hgrepos = []
    issue_num = 1904
    keywords = ['patch']
    message_count = 3.0
    messages = ['61496', '61497', '64397']
    nosy_count = 2.0
    nosy_names = ['rhettinger', 'gpolo']
    pr_nums = []
    priority = 'low'
    resolution = 'rejected'
    stage = None
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue1904'
    versions = ['Python 2.6', 'Python 3.0']

    @gpolo
    Copy link
    Mannequin Author

    gpolo mannequin commented Jan 22, 2008

    Wouldn't it be useful to add a "key" argument to some heapq functions ?
    So you could construct a heap from dict items list based on the second
    item of each tuple, for example.

    @gpolo gpolo mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Jan 22, 2008
    @gpolo
    Copy link
    Mannequin Author

    gpolo mannequin commented Jan 22, 2008

    Adding a simple patch that adds the "key" argument. To test this you
    need to comment lines where it tries to import the C version of heapq.

    @rhettinger
    Copy link
    Contributor

    Rejecting the key-function patch in favor of the current typical
    approach of heapifying tuples that link a priority level with
    individual records. I've been a heavy user of the module and one of
    its principal maintainers. To date, I've seen very little need for key-
    function argument.

    Use cases aside, there is another design issue in that the key-function
    approach doesn't work well with the heap functions on regular lists.
    Successive calls to heap functions will of necessity call the key-
    function multiple times for any given element. This contrasts with sort
    () where the whole purpose of the key function was to encapsulate the
    decorate-sort-undecorate pattern which was desirable because the key-
    function called exactly once per element.

    @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-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant