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

struct.Struct.unpack to return a namedtuple for easier attribute access #47158

Closed
habnabit mannequin opened this issue May 18, 2008 · 4 comments
Closed

struct.Struct.unpack to return a namedtuple for easier attribute access #47158

habnabit mannequin opened this issue May 18, 2008 · 4 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@habnabit
Copy link
Mannequin

habnabit mannequin commented May 18, 2008

BPO 2909
Nosy @rhettinger, @vadmium
Files
  • named-struct.patch: Patch against the py3k branch
  • named-struct2.patch: Patch against the python trunk
  • 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-05-23.17:22:36.533>
    created_at = <Date 2008-05-18.20:13:19.410>
    labels = ['type-feature', 'library']
    title = 'struct.Struct.unpack to return a namedtuple for easier attribute access'
    updated_at = <Date 2012-10-27.04:17:52.105>
    user = 'https://bugs.python.org/habnabit'

    bugs.python.org fields:

    activity = <Date 2012-10-27.04:17:52.105>
    actor = 'martin.panter'
    assignee = 'rhettinger'
    closed = True
    closed_date = <Date 2008-05-23.17:22:36.533>
    closer = 'rhettinger'
    components = ['Library (Lib)']
    creation = <Date 2008-05-18.20:13:19.410>
    creator = 'habnabit'
    dependencies = []
    files = ['10368', '10374']
    hgrepos = []
    issue_num = 2909
    keywords = ['patch']
    message_count = 4.0
    messages = ['67038', '67048', '67050', '67253']
    nosy_count = 3.0
    nosy_names = ['rhettinger', 'habnabit', 'martin.panter']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue2909'
    versions = ['Python 2.6', 'Python 3.0']

    @habnabit
    Copy link
    Mannequin Author

    habnabit mannequin commented May 18, 2008

    With the advent of collections.namedtuple, I thought that having a
    counterpart in the struct module would make having to deal with unpacked
    data much easier. Per suggestion, this extends the behavior of
    _struct.Struct rather than a separate NamedStruct class.

    The regexp might not be immediately obvious as to what the format
    required is. The format string is represented like this:
    "attribute_name1(attribute_format) attribute_name2(attribute_format2)"
    and so on. Formats given in parentheses without an attribute name are
    allowed, so that byte order and pad bytes can be specified. For example:
    "(!) x1(h) x2(h) (2x) y1(h) y2(h)"

    Suggestions and criticism are welcome. I think it would simplify using
    the struct module a lot to be able to have named attributes like this.

    @habnabit habnabit mannequin added the stdlib Python modules in the Lib dir label May 18, 2008
    @rhettinger rhettinger self-assigned this May 18, 2008
    @rhettinger rhettinger added the type-feature A feature request or enhancement label May 18, 2008
    @habnabit
    Copy link
    Mannequin Author

    habnabit mannequin commented May 18, 2008

    Okay, here's a new version of my patch. Instead of replacing the default
    functionality of struct.Struct, this patch now adds the functionality to a
    separate class called NamedStruct, so as to not break backwards
    compatibility. The coding style has been revised, and it now also raises a
    more descriptive error if the regex fails to parse. Also included: a unit
    test.

    @rhettinger
    Copy link
    Contributor

    Thanks for submitting the patch and the idea.

    I've previously looked at this approach and decided against it. The
    struct module is highly optimized and typically called many times in a
    loop and a patch like this would hurt performance. Also, it adds
    complexity to the module making it more difficult to learn an remember.

    It is better to leave named tuples separate and allow users to combine
    them downstream -- see the SQLite example for instance -- the cast to
    named tuples could be applied directly, so there was no need for a
    change to the SQL module. It is better design for us to leave the data
    retrieval (struct, csv, sql, etc) separate from the code for
    collections.namedtuple.

    Instead of this patch, I propose to add a simple example to the struct
    module documentation showing how to cast to a named tuple. Also,
    FWIW, the preferred way to cast tuples to named tuples is to use the
    _make() method instead of using the constructor with the star
    operator. nt._make(t) instead of nt(*t).

    @rhettinger
    Copy link
    Contributor

    Added example to docs. See r63564.

    @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-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant