Skip to content

Support annotations to create struct types #814

Closed

Description

@joernheissler

Hi,
nice package!
But I'm wondering if you could support pep-526 annotations (python3.6+) as an alternative to the "name" / type syntax for constructing struct types:

from __future__ import annotations

from construct import Const, Int8ub, Array, Byte, this
from construct import Struct as Struct_

class StructMeta(type):
    def __new__(cls, name, parent, attrs):
        if not parent:
            return type.__new__(cls, name, parent, attrs)
        return Struct_(*(
            key / (eval(val) if isinstance(val, str) else val)
            for key, val in attrs["__annotations__"].items()
        ))

class Struct(metaclass=StructMeta):
    pass

class BMP(Struct):
    signature: Const(b"BMP")
    width: Int8ub
    height: Int8ub
    pixels: Array(this.width * this.height, Byte)

print(BMP.build(dict(width=3,height=2,pixels=[7,8,9,11,12,13])))
bmp = BMP.parse(b'BMP\x03\x02\x07\x08\t\x0b\x0c\r')
print(bmp)

Activity

  1. arekbulski commented on Oct 14, 2019

    @arekbulski
    Member

    Hi, short answer is no. I am not developing this library anymore. Especially if it requires Python 3.6 or later. Its a neat example tho. Thanks for sharing.

  2. Evidlo commented on Oct 16, 2019

    @Evidlo

    @arekbulski Have you considered bringing in new maintainers?

  3. Tadaboody commented on Oct 20, 2019

    @Tadaboody

    Would you accept a PR adding this as an alternative AnnotatedStruct class? (raising some error if imported from a <3.6 interperter?

  4. DanseMacabre commented on Oct 21, 2019

    @DanseMacabre

    I think that Construct as it stands could do with a more wholesale refactor/reworking too, at the moment. Perhaps to add this method of definition to replace the ""name" / type" syntax which does seem rather clunky (PyCharm warns users about this syntax, as an example).

    Construct is overdue a major release IMO, with one of the initial authors blogging about it 6(!) years ago. I think @arekbulski had several ideas of features currently baked into 2.X that make Construct a nightmare to maintain; Embeds, I think.

  5. arekbulski commented on Oct 21, 2019

    @arekbulski
    Member

    @DanseMacabre is correct. The library grew almost too big to be refactorable.I wont accept a PR because I wont understand how it works. Also correct about Embed. I am also struggling atm with infrastructure: readthedocs doesnt build uptodate docs anymore, uploading to pip doesnt work either, travisci stopped working also. And I have no idea how to fix those. Until those 3 are fixed, there is no PRs accepted and no features implemented.

  6. waszil commented on Nov 19, 2019

    @waszil

    Indeed, a great package! Can I ask, what is this "Embed" or "Embeds" that you've mentioned?

  7. biqqles commented on Jan 2, 2020

    @biqqles

    I had the same idea as @joernheissler and implemented something similar a few days earlier (before I knew about this project, which I've just found now) - deconstruct. It's much more simple than Construct, but it's a tiny codebase which means it should be easier to maintain (re @DanseMacabre) if that's all you need.

  8. arekbulski commented on Jan 3, 2020

    @arekbulski
    Member

    Deconstruct only supports structs, ints and floats AFAIK.

  9. biqqles commented on Jan 4, 2020

    @biqqles

    It supports all C's basic types and exact-width types (plus a couple more), and arrays of all those types. A full list is here.

  10. arekbulski commented on Jan 17, 2020

    @arekbulski
    Member

    I do not understand how annotations or metaclasses work, so I am rejecting this ticket. Case closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions