Giter Site home page Giter Site logo

datastruct's Introduction

datastruct's People

Contributors

kuba2k2 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

robots

datastruct's Issues

How can I use substructures?

How can I use WString as substructure for CommandReadVariable? And is it possible to get length of WString inside CommandReadVariable?

def wstring(length: int, *, default: str = ...):
    return adapter(
        encode=lambda value, ctx: value.encode("utf-16le"),
        decode=lambda value, ctx: value.decode("utf-16le"),
    )(field(length, default=default))

@dataclass
class WString(DataStruct):
    variable_length: int = built("H", lambda ctx: len(ctx.variable_name))
    variable_name: str = wstring(lambda ctx: ctx.variable_length * 2)

@dataclass
class CommandReadVariable(DataStruct):
  tag_id: int = field('H', default= 1)
  message_length: int = built("H", lambda ctx: len(ctx.variable_name) * 2 + 3)
  message_type: MessageType = const(MessageType.CommandReadVariable)(field("B"))
  variable_name: WString

Alternative way of variable length bytes field.

Hi,

This is not issue, this is more of "documenting" of features for future users.

I needed something that is like "vartext" but with bytes. The example shown in README.md didn't work well.

So i implemented varbytes field.

def varbytes(
    length: Value[int],
    *,
    default: bytes = ...
):
    class Bytes(Adapter):
        def encode(self, value: bytes, ctx: Context) -> bytes:
            return value

        def decode(self, value: bytes, ctx: Context) -> bytes:
            return value

    return adapter(Bytes())(
        field(
            lambda ctx: (
                len(ctx.P.self) if ctx.G.packing else evaluate(ctx, length)
            ),
            default=default,
        )
    )

This is strongly inspired by vartext, but simplified as it only supports variable length. (Yes encode and decode could be lambdas, meh)

@dataclass
@datastruct(endianness=Endianness.LITTLE, padding_pattern=b"\x00")
class Packet1(DataStruct):
    list: int = field('B')
    num: int = field('H')
    count: int = field('H')
    data: bytes = varbytes(lambda ctx: ctx.G.root.length-5)

@dataclass
@datastruct(endianness=Endianness.LITTLE, padding_pattern=b"\x00")
class Packet(DataStruct):
    length: int = built("B", lambda ctx: ctx.body.sizeof())
    body: Any = switch(lambda ctx: ctx.typ)(
            PACKET1 = (Packet1, subfield()),
        )

When packing Packet.length is automatically calculated from length of data in Packet1 (or any other body packet)
When unpacking Packet1.data length is calculated from Packet.length - 5 (which is length of B+H+H))

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.