Giter Site home page Giter Site logo

stru's Issues

struct subclasses cannot be decorated

When decorating a subclass of Struct with @dataclass or @attr.define, the following errors occur at Point's construction time: TypeError: TestSelf.test_pack_unpack.<locals>.Point.__init__() got an unexpected keyword argument 'x'

    def test_pack_unpack(self):
        class Point(Struct):
            """
            Semantic eq method is provided by Struct
            @attr.define -- does not play well with the super-class
            @dataclass -- does not play well with the super-class
            """
            _endianess = Endianess.LittleEndian
            x = FieldType.WORD
            y = FieldType.WORD
            name = FieldType.String[6]("NONAME")

        original_point = Point(x=1, y=2)
        packed_point = original_point.pack()
        assert_that(packed_point,
                    is_(b'\x01\x00\x02\x00NONAME'))

        unpacked_point = Point.unpack(packed_point)
        assert_that(unpacked_point,
                    is_(original_point))
        self.log().debug(unpacked_point)

mulitple inheritance does not work

    def should_pack_unpack(self):
        class LEStruct(Struct):
            _endianess = Endianess.LittleEndian

        class HasId(LEStruct):
            id = FieldType.WORD(default=0)

        class HasName(LEStruct):
            name = FieldType.String[6]("NONAME")

        # ISSUE paprika provided @to_string decorator fails when used
        # together with Struct super-class
        # @to_string
        # https://github.com/rayanht/paprika/issues/28#issuecomment-2212366362
        # ISSUE multiple inheritance does not work, only first member is
        # recognized

        class Point(HasName, HasId):
            """
            Semantic eq method is provided by Struct
            @attr.define -- does not play well with the super-class
            @dataclass -- does not play well with the super-class
            """
            x = FieldType.WORD
            y = FieldType.WORD

        @to_string
        class Point3D(Point):
            z = FieldType.WORD

        original_point = Point3D(x=1, y=2, z=3)
        packed_point = original_point.pack()
        # assert_that(packed_point,
        # is_(b'NONAME\x00\x00\x01\x00\x02\x00\x03\x00'))
        self.log.debug(packed_point)
        unpacked_point = Point3D.unpack(packed_point)

        self.log.debug(unpacked_point)
        assert_that(unpacked_point,
                    is_(original_point))

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.