Package: dataclass-struct
dataclass_struct 0.9.3Decorator providing capability to emit and read the decorated dataclass as a binary buffer.
A special new metadata fieldSTRUCT_TYPEcontains thestructformat for the dataclass field.Seestructdocumentation for the formatting options.FUNCTIONSThe decorator@dataclass_structadds 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 byteinstance_from_buffer(buffer: bytes, offset=0)Construct a wrapped class instance from a buffer.
       
 :param buffer: buffer with source binary data
 :return: class instanceto_buffer(self, buffer=b'')Store the wrapped dataclass to a binary buffer.
       
 :param self: wrapped instance
 :return: resulting bufferLimitationsCurrently, some features are not supported:no inheritance hierarchyno tuplesIf a dataclass member provides methodsfrom_bufferandto_buffer,
it will be included into the resulting buffer and loaded from it
without havingSTRUCT_TYPEmetadata on its own.Lists of primitive types can be used, if the number of list members matches the number
of format elements. If not, the error message from underlying struct call
will be emitted.Lists of user defined objects will be written/loaded
when the object class provides corresponding methods.
These also don't needSTRUCT_TYPEmetadata.UsageSimple buffer for a float and an integerFields having metadata fieldSTRUCT_TYPEwill be written/read out from the buffer.fromdataclass_structimportSTRUCT_TYPE,dataclass_struct@dataclass_structclassMyDataClass: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'Class containing another instance of dataclass_structFields providing methodsfrom_bufferandto_bufferfromdataclassesimportfieldfromdataclass_structimportSTRUCT_TYPE,dataclass_struct@dataclass_structclassDataClassA:my_flt:float=field(default=0,metadata={STRUCT_TYPE:'<f'})my_num:int=field(default=0,metadata={STRUCT_TYPE:'<i'})@dataclass_structclassDataClassB:data_part_a:DataClassA=field(default=DataClassA())just_a_num:int=field(default=0,metadata={STRUCT_TYPE:'<i'})test_obj=DataClassB(DataClassA(3.14,42),42)buff=test_obj.to_buffer()# buff equals to b'\xc3\xf5H@*\x00\x00\x00*\x00\x00\x00'String with custom encodingDefault encoding for strings is'utf-8'.
This can be changed for the whole dataclass by
the decorator parameteruse_encoding,
or for the particular field using metadataENCODING.@dataclass_struct(use_encoding='ascii')classDefaultEncodingTest: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)classExplicitDataclass:my_flt:float=field(default=0,metadata={STRUCT_TYPE:'<f'})my_num:int=field(default=0,metadata={STRUCT_TYPE:'<i'})