Skip to main content

No project description provided

Project description

dataclass_struct

Decorator providing capability to emit and read the decorated dataclass as a binary buffer. A special new metadata field STRUCT_TYPE contains the struct format for the dataclass field.

See struct documentation for the formatting options.

FUNCTIONS

The decorator @dataclass_struct adds following methods to the decorated dataclass.

from_buffer(self, buffer: bytes, offset=0)

 Read the wrapped dataclass from a binary buffer.
      
 :param self: wrapped instance
 :param buffer: buffer tp read
 :param offset: (optional) offset o start reading
 :return: offset after last consumed byte

instance_from_buffer(buffer: bytes, offset=0)

 Construct a wrapped class instance from a buffer.
       
 :param buffer: buffer with source binary data
 :param offset: (optional) offset o start reading
 :return: class instance

to_buffer(self, buffer=b'')

 Store the wrapped dataclass to a binary buffer.
       
 :param self: wrapped instance
 :param buffer: (optional) buffer to continue, if any
 :return: resulting buffer

Limitations

Currently, only flat classes with simple fields are supported, meaning

  • no inheritance hierarchy
  • no lists
  • no tuples
  • no nested dataclasses

Usage

Simple buffer for a float and an integer

Only fields having metadata field STRUCT_TYPE will be written/read out from the buffer.

from dataclass_struct import STRUCT_TYPE, dataclass_struct

@dataclass_struct
class MyDataClass:
    my_flt: float = field(default=0, metadata={STRUCT_TYPE: '<f'})
    my_num: int = field(default=0, metadata={STRUCT_TYPE: '<i'})
    my_name: str = ''

test_obj = TestModel(3.14, 42)
buff = test_obj.to_buffer()

# buff equals to b'\xc3\xf5H@*\x00\x00\x00'

String with custom encoding

Default encoding for strings is 'utf-8'. This can be changed for the whole dataclass by the decorator parameter use_encoding, or for the particular field using metadata ENCODING.

@dataclass_struct(use_encoding='ascii')
class DefaultEncodingTest:
    byte_name: bytes = field(default=b'', metadata={STRUCT_TYPE: '16s'})
    str_name: str = field(default='', metadata={STRUCT_TYPE: '16s'})
    str_with_enc: str = field(default='', metadata={STRUCT_TYPE: '32s', ENCODING: 'utf-16'})


test_obj = StringTest(b'Hello World', 'Bye bye', 'another one')
buff = test_obj.to_buffer())

# buff equals to b'Hello World\x00\x00\x00\x00\x00Bye bye\x00\x00\x00'\
#            b'\x00\x00\x00\x00\x00\x00\xff\xfea\x00n\x00o\x00t\x00h'\
#            b'\x00e\x00r\x00 \x00o\x00n\x00e\x00\x00\x00\x00\x00\x00'\
#            b'\x00\x00\x00'

Use custom parameters for the underlying dataclass

@dataclass_struct
@dataclass(order=True)
class ExplicitDataclass:
    my_flt: float = field(default=0, metadata={STRUCT_TYPE: '<f'})
    my_num: int = field(default=0, metadata={STRUCT_TYPE: '<i'})

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dataclass_struct-0.9.1.tar.gz (3.9 kB view details)

Uploaded Source

File details

Details for the file dataclass_struct-0.9.1.tar.gz.

File metadata

  • Download URL: dataclass_struct-0.9.1.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.1

File hashes

Hashes for dataclass_struct-0.9.1.tar.gz
Algorithm Hash digest
SHA256 3b3a4f3415883588a45fed0205d82718fc02d0232685923abc6c175b715aab47
MD5 7ec7d2e9da07928e8a1a54e18fc3f7e1
BLAKE2b-256 d5c0de874944e9304603bcabdce50fdc1c0e2d1a8e6cf017f70e0fdc99e291eb

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page