Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    Bush-Lee
    @Bush-Lee
    I'll leave code snippet if needed
    wtc-peg
    @wtc-peg

    @Bush-Lee, Here is a simple extract from a running project. With hope it will be helpful:

    Occurence = BitStruct(
        "Extended" / Flag,
        "Value"    / Switch(this.Extended,{ 
            False : BitsInteger(7),
            True  : Struct(
                "Reserved"       / Const(0x00,BitsInteger(7)),
                "OccurencesHigh" / BitsInteger(16),
                "OccurencesLow"  / BitsInteger(16),
                ),
        })
    )

    yess... succeded in putting formated code ;O) (for all rookies like me, see: Gitter Markdown basics)

    Bush-Lee
    @Bush-Lee
    @wtc-peg Thanks for the snippet, I already managed to make the struct i need but struct.build is throwing me off (it's throwing errors too in fact)
    CommandArgument = Enum(
        Byte, SetOutputs=1, SetPWM=2, SetConfiguration=3, SetConfigMode=5,
        GetNID=16, GetStatus=17, GetDidStauts=18, GetConfigMemory=19,
        GetCalibrationMemory=20, ForceResetRouter=21, GetPacketPath=22
    )
    
    ConfigRecord = Struct(
        'Adress' / Int8ub,
        'Value' / Int8ub
    )
    
    ConfigData = ConfigRecord[16]
    
    Command = Struct(
        'CommandArgument' / CommandArgument,
        'Data' / Switch(
            this.CommandArgument, {
                'SetConfiguration': ConfigData,
                'SetOutputs': ConfigRecord,
                'SetPWM': ConfigRecord,
            }, default=ConfigRecord
        )
    )
    # Works
    print(ConfigData.build([dict(Adress=i, Value=i) for i in range(16)]))
    
    # Doesen't work throws error
    # last 2 lines of the error log are
    #    subobj = obj[sc.name] # raises KeyError
    #    TypeError: list indices must be integers or slices, not str
    print(Command.build(
        dict(CommandArgument=3,
             Data=[dict(Adress=i, Value=i) for i in range(16)]
             )
        )
    )
    
    
    # Shouldn't work
    print(Command.build(
        dict(CommandArgument=3,
             Data=dict(Adress=1, Value=1)
             )
        )
    )
    Bush-Lee
    @Bush-Lee
    Ok so in case someone stumbles in this i'm getting the error because Switch isnt working so it goes to the default case automaticly
    wtc-peg
    @wtc-peg
    Oups, I remember a previous experience like this some time ago: Enum ID must be used when building, not enum Value .
    Maybe "Support of value OR ID of Enum" could be added in a wichlist ?
    print(Command.build(
        dict(
            CommandArg = 'SetConfiguration',
            Data=[dict(Adress=i, Value=i) for i in range(16)]
            )
        )
    )
    Bush-Lee
    @Bush-Lee
    @wtc-peg That was it. Thanks, saved me a lot of time with that.
    Alex Railean
    @ralienpp

    Is it possible to infer certain values based on the context? For example

    Tlv = Struct(
        'tag' / Int16ub,
        'length' / Int32ub,
        'value' / Array(this.length, Byte)
        )
    
    tlv_data = dict(tag=1, length=5, value=b'\xFF\xFF\xFF\xFF\xFF')
    Tlv.build(tlv_data)

    In this case I have to explicitly provide all the values when I call build, but is it possible to do tlv_data = dict(tag=1, value=b'\xFF\xFF\xFF\xFF\xFF') and have it figure out on its own what the adequate value of the length field should be?

    What is not clear to me is the duality... When serializing, I want to use a short-hand notation, but at the same time, the structure definition should also work when deserializing raw data.

    MaruthiRathnakar
    @MaruthiRathnakar
    I am getting an error while running nose test , AssertionError: MappingError not raised by parse..... Below is the snippet, can i know where i am going wrong

    mar_payload_type = "payload_type" / Enum(BitsInteger(7),
    RTP101=0x65)

    def test_payload_type_enum():
    test = "" / BitStruct(Padding(1), mar_payload_type)
    assert_equal(test.parse(b"\x65").payload_type, "RTP101")
    assert_raises(MappingError, test.parse, b"\xff")

    Stephan Gerhard
    @unidesigner
    Hi! Any suggestions on how to properly write the struct for a binary header sequence like: b'M1\n7\n17 17\n0 0\n0/0015/0015_0012_0_b align\n' ?
    I do have
    map_header = Struct(
        "type" / Const(b"M1"),
        "level" / Int32un,
        "width" / Int32un,
        "height" / Int32un,
        "xmin" / Int32un,
        "ymin" / Int32un,
        "imgName" / CString("ascii"),
        "refName" / CString("ascii"),
        "data" / Array(this.width * this.height * 3, Int32un),
    )
    But this does not seem to parse the spaces properly.
    Stephan Gerhard
    @unidesigner
    Thanks!
    Paul Moore
    @pfmoore

    Hi! I want to create a struct where one of the field types depends on another at runtime - so basically something like this

    def get_the_parser(id):
        if id == 1: return Byte
        if id == 2: return Int32un
    
    item = Struct(
        "id" / Byte,
        "data" / lambda ctx: get_the_parser(ctx.id),
    )

    Obviously the above doesn't work, because the "data" item is a function, not a Construct subclass. But LazyBound doesn't work, because the lambda it wraps doesn't have access to the context. I feel like this should be really easy, but I've been working on it most of the day, and it looks like I'm missing something, because I've no idea how to do it :-(

    Any suggestions?

    MaruthiRathnakar
    @MaruthiRathnakar

    repeat_enabled = "dispatch_controlled_repeat" / Struct("dispatch_controlled_repeat" / Flag,
    Padding(6),
    "repeat_enabled" / Flag)

    note = "note" / BitStruct(
    repeat_enabled
    )
    when i build this using below line

    note.build(dict(dispatch_controlled_repeat=True,repeat_enabled=True))

    I get this Error

    File "C:\Python37\lib\site-packages\construct\lib\containers.py", line 183, in update
    for k,v in seqordict:
    TypeError: 'bool' object is not iterable

    Where i am going Wrong, Any suggestions?

    I am quite new to construct, so please help me with this
    MaruthiRathnakar
    @MaruthiRathnakar
    @arekbulski , i m using construct 2.9.45 with python 3.7.. Using optional inside the Bitstruct raises Stream Error, I did find one similar raised in github, I also read your answer, tat If the struct is of Restreamed type, it wont work.. But i cant make my structure as Transformed and i want Optional to work inside Bitstruct, any way to do it.
    Any help would be highly appreciated
    Thanks
    Dan Gass
    @dangass_gitlab
    Is anyone from this or related projects going to be at PyCon? I am working on a similar project for flexible and fast memory packing/unpacking (https://plum-py.readthedocs.io/en/latest/index.html) that is leveraging features in Py3. I'm hoping to meet folks from this project to share ideas. I've also done a little benchmarking comparing basic pack/unpack speed performance for various packages (the standard library struct module, xnd/ndtypes, plum, construct, and a couple others). If anyone is interested in seeing what I have I'd be happy to share. My company (Rockwell Automation) is sponsoring a booth and hopefully you can find me there.
    Nguyễn Hồng Quân
    @hongquan
    Hi, how to use sizeof or other method to get the size of "root" struct?
    DanseMacabre
    @DanseMacabre
    @arekbulski Cheers for steering this library BTW, It's super useful in my life at the mo!
    reox
    @reox
    Hi! I have some wonderful file format which has an ID for some sections, but the ID is also a counter - that means there might be n sections of type A but then they have the IDs A, A+1, A+2, ... A+n-1. They define two values for each section ID, namely a valid start and valid end value. Is there some magic in construct to parse such stuff directly into a form where I get the section ID as well as the counter? Maybe someone has a hint how this could work?
    possibly I have to define my own type of Enum right?
    reox
    @reox
    So I put something together which make it work, but there might be a better way. If someone has one, please let me know!
    class StartEndEnum(Enum):
        def __init__(self, *args, **kwargs):
            new_args = dict()
            for k, v in kwargs.items():
                if k.endswith('_START'):
                    key = k[:-6]
                    end = key + "_END"
                    if end not in kwargs:
                        raise ValueError("{} without END item!".format(key))
                    for i in range(v, kwargs[end] + 1):
                        new_args[key + "_{}".format(i - v)] = i
                elif k.endswith('_END'):
                    pass
                else:
                    new_args[k] = v
            super().__init__(*args, **new_args)
    Edvard Pettersen
    @tiepettersen
    Hi, after parsing a construct and I get a container, are there any mechanisms to ensure fields in the construct are of appropriate length and such when setting new values based on what was defined in the construct?
    Something along these lines:
    definition = Struct(
        "name" / Bytes(8),
        "id" / Int32ul,
    )
    
    cont = definition.parse(b'ABCDEFGH\x00\x11\x22\x33')
    cont.name = b'ABCDEFGHX'  # I expect 8 bytes, not 9. Can I make this fail?
    st.build(cont)
    Adrien Guinet
    @aguinet

    Hello everyone!
    I have a construction that I try to parse w/ construct, but can't gigure out how with the documentation.
    Basically, in the stream, I first have a list of bytes that are mapped to an enum, whose count is already known:

    E0 E1 E2 E3

    And then, I have a list of structures whose type depends on E0,E1,...
    The easy part is to figure out E*, like this:

    "types" / Array(this.count, MyEnum)

    But then, I need to do a switch on top of the values that are in types. Is there an easy way to do this?
    Thanks for the help!

    Adrien Guinet
    @aguinet
    I managed to do it using a custom Construct object, but that looks like a hack to me :)
    Nguyễn Hồng Quân
    @hongquan
    👍
    DanseMacabre
    @DanseMacabre
    @arekbulski What can be done to return Construct to active development? I'd obviously be happy to contribute/refactor where I can to help improve the quality of the library. It's exceptionally useful for me! Perhaps some discussion about next steps, desired features, desired deprecations, backwards compat/translation of the declarative format etc etc etc? Then a roadmap can be laid out to help focus the development. I'd be quite keen in making the project more modular, as core.py is a monster of a file at the mo! Might be an idea to create a meta-issue on GitHub (or use the wiki?) to track and discuss rather than gitter?
    Arkadiusz Bulski
    @arekbulski
    @DanseMacabre I am not using Gitter at all. If you seen the red text in main docs, I highly recommend using Issues for asking questions.
    So you see, I just found 2 years old messegaes on Gitter...
    Guys, please post as Issues
    Arkadiusz Bulski
    @arekbulski
    @DanseMacabre What can be done to jumpstart development? Well, I could use some help with translating deprecated gallery examples. At least few of them, but with quality constructs.
    Arkadiusz Bulski
    @arekbulski
    I fixed today all (almost all) outstanding tickets, so there is no need for a second maintainer. But I do accept good PRs.
    Arkadiusz Bulski
    @arekbulski
    @DanseMacabre Feel free to open a Issue ticket asking what to do... and move conversation there,
    Arkadiusz Bulski
    @arekbulski
    I created a ticket as appropriate...
    Arkadiusz Bulski
    @arekbulski
    Only now I found out how to turn on email notifications on Gitter, yay
    Arkadiusz Bulski
    @arekbulski
    @all What do you think guys should be the next major change added? Any ideas?
    Arkadiusz Bulski
    @arekbulski
    Regarding the size of core.py, originally the code was divided into several files but I found out that is just counter productive. Its easier to keep all in one place.
    I am very interested in your suggestions on what to add next. I ran out of PRs to merge.
    Arkadiusz Bulski
    @arekbulski
    @/all MESSAGE TO EVERYONE: I am about to remove Embedded and EmbeddedSwitch from the core library. I know its in use here and there but please understand, the semantics have changed so much that using this as is now is pointless.
    Arkadiusz Bulski
    @arekbulski
    @tiepettersen Did you check the Terminated class?
    Arkadiusz Bulski
    @arekbulski
    @ralienpp You need to use Rebuild class.
    silverhawke
    @silverhawke249
    Is there an easy way to convert a Container/ListContainer to a dict/list? I want to save the data in other formats but the BytesIO objects are preventing me from directly converting
    silverhawke
    @silverhawke249
    I ended up just making a recursive function for it, haha
    Arkadiusz Bulski
    @arekbulski
    Container already derives from a dict, so it is a dict.