Package: dinobytes
DinoBytesDinoBytes is a dynamic serialization framework designed to simplify network communication by providing a robust set of tools for serializing and deserializing messages using the MessagePack format. With its easy-to-use decorator and base class, DinoBytes makes defining and handling network messages both efficient and intuitive, suitable for applications ranging from simple messaging systems to complex distributed architectures.FeaturesAutomatic Message Type Registration: Utilizes a decorator to automatically register class types and assign unique identifiers, ensuring a smooth serialization and deserialization process.Simplified Serialization: Offers a straightforward method (to_bytes) for converting class objects into serialized bytes.Effortless Deserialization: Allows for easy conversion of bytes back into class objects using thefrom_bytesmethod, facilitating quick and reliable class parsing.Streamlined Message Definition: Leverages Python's dataclass decorator within the customdbytedecorator to reduce boilerplate and enhance readability when defining new class types.Message Registry: Automatically maintains a registry of class types, simplifying the process of managing different kinds of network messages within your application.InstallationInstall DinoBytes using pip:pipinstalldinobytesQuick StartDefining a Messagefromdinobytesimportdbyte@dbyteclassMyMessage:content:strSerializing a Messagemessage=MyMessage(content="Hello, DinoBytes!")serialized_message=message.to_bytes()# or bytes(message)Deserializing a Messagereceived_message=MyMessage.from_bytes(serialized_message)print(received_message.content)# Output: Hello, DinoBytes!Theunpackdconvenience method can also be used to automatically unpack into the correct type:fromdinobytesimportunpackdreceived_message=unpackd(serialized_message)print(received_message.content)# Output: Hello, DinoBytes!More examplesfromdataclassesimportdataclassfromdinobytesimportdbyte,unpackd@dbyteclassParent:def__init__(self,children:list[Child]|None=None):self.children=childrenor[]def__repr__(self):returnf"Parent(children={self.children})"@dbyte@dataclassclassChild:name:str='<default>'parent=Parent()child=Child(name="Jimothy")parent.children.append(child)serialized=bytes(parent)print(serialized)print(unpackd(serialized))Output:b'\x92\x00\x91\xc4\n\x92\x01\xa7Jimothy'Parent(children=[Child(name='Jimothy')])fromdataclassesimportdataclassfromdinobytesimportdbyte,unpackd@dbyte@dataclassclassClassA:value:int@dbyte@dataclassclassClassB:value:str@dbyte@dataclassclassClassC:value:list[int]classA=ClassA(1)classB=ClassB('hello')classC=ClassC([1,2,3])forxin[bytes(classB),bytes(classC),bytes(classA)]:matchunpackd(x):caseClassA(value):print(f'ClassA:{value=}')caseClassB(value):print(f'ClassB:{value=}')caseClassC(value):print(f'ClassC:{value=}')case_:print('unknown class')Output:ClassB:value='hello'ClassC:value=[1,2,3]ClassA:value=1ContributingContributions to DinoBytes are welcome! Whether it's bug reports, feature requests, or code contributions, please feel free to reach out or submit a pull request. For major changes, please open an issue first to discuss what you would like to change.LicenseDinoBytes is released under the Apache 2.0 License. See the LICENSE file for more details.