Skip to content

Multiple get "itemgetter" #76191

Closed
Closed

Description

@mariocj89
mannequin
BPO 32010
Nosy @rhettinger, @serhiy-storchaka, @mariocj89

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 2017-11-13.05:42:37.182>
created_at = <Date 2017-11-12.14:49:14.925>
labels = ['3.7', 'type-feature', 'library']
title = 'Multiple get "itemgetter"'
updated_at = <Date 2017-11-13.05:42:37.157>
user = 'https://github.com/mariocj89'

bugs.python.org fields:

activity = <Date 2017-11-13.05:42:37.157>
actor = 'rhettinger'
assignee = 'none'
closed = True
closed_date = <Date 2017-11-13.05:42:37.182>
closer = 'rhettinger'
components = ['Library (Lib)']
creation = <Date 2017-11-12.14:49:14.925>
creator = 'mariocj89'
dependencies = []
files = []
hgrepos = []
issue_num = 32010
keywords = []
message_count = 3.0
messages = ['306114', '306115', '306140']
nosy_count = 3.0
nosy_names = ['rhettinger', 'serhiy.storchaka', 'mariocj89']
pr_nums = []
priority = 'normal'
resolution = 'rejected'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue32010'
versions = ['Python 3.7']

Activity

  1. mariocj89 commented on Nov 12, 2017

    mariocj89mannequin
    MannequinAuthor

    At the moment we can get an operator that performs multiple "gets" in object attributes. Example:

    >>> getter = operator.attrgetter("child1.child2")
    >>> o = mock.Mock()
    >>> getter(o)
    <Mock name='mock.child1.child2' id='4317895480'>

    On the other hand, itemgetter -which can be used for mappings- can only access single level.

      a = itemgetter("a")(d)

    The proposal is to add a way to perform multiple fetches the say way attrgetter is doing it.

    The main worry here for me would be that it might break some existing callers if someone had "a.b" as a key in a dict and were using itemgetter.
    An option might be a new argument separator to split those so it'd look like:

      d = dict(a=dict(b=1), b=dict(c=2)) 
      ab = itemgetter("a.b", separator=".")(d)

    This is effectively sugar for: itemgetter("b")(itemgetter("a")(d))

    This should be available in the *args version as well so the following is valid:

      d = dict(a=dict(b=1), b=dict(c=2)) 
      ab, ac = itemgetter("a.b", "b.c", separator=".")(d)

    This is coming from python-dev mailing list thread: "[Python-Dev] Analog of PEP-448 for dicts (unpacking in assignment with dict rhs)"

    I have a sample implementation on the py side. If this is interesting I can send a PR with the full impl (I havent started yet with the C one)
    If anyone is interested in the ongoing implementation: https://github.com/mariocj89/cpython/tree/multiple_itemgetter

  2. added
    3.7 (EOL)
    stdlib
    type-feature
    on Nov 12, 2017
  3. serhiy-storchaka commented on Nov 12, 2017

    @serhiy-storchaka
    Member

    As you have said, this will break existing code.

    And this is not applicable to non-string indices.

    I think that it would be easier to use a lambda or local function for this: lambda x: x['a']['b']. It is shorter and cleaner.

    I don't see how this is related to the Python-Dev thread.

  4. rhettinger commented on Nov 13, 2017

    @rhettinger
    Contributor

    I also don't think this should be done to itemgetter() which should remain focused on its core task.

    Thank you for the suggestion, but am marking this as closed. Perhaps consider posting a recipe to the ASPN cookbook (I've posted a lot of ideas there) or publish something on PyPI to see if there is any uptake.

  5. transferred this issue fromon Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.7 (EOL)stdlibtype-feature

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions