Skip to main content

No project description provided

Project description

DinoBytes

dinobytes

DinoBytes 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.

Features

  • Automatic 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 the from_bytes method, facilitating quick and reliable class parsing.
  • Streamlined Message Definition: Leverages Python's dataclass decorator within the custom dbyte decorator 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.

Installation

Install DinoBytes using pip:

pip install dinobytes

Quick Start

Defining a Message

from dinobytes import dbyte

@dbyte
class MyMessage:
    content: str

Serializing a Message

message = MyMessage(content="Hello, DinoBytes!")
serialized_message = message.to_bytes() # or bytes(message)

Deserializing a Message

received_message = MyMessage.from_bytes(serialized_message)
print(received_message.content)  # Output: Hello, DinoBytes!

The unpackd convenience method can also be used to automatically unpack into the correct type:

from dinobytes import unpackd

received_message = unpackd(serialized_message)
print(received_message.content)  # Output: Hello, DinoBytes!

More examples

from dataclasses import dataclass

from dinobytes import dbyte, unpackd

@dbyte
class Parent:
  def __init__(self, children: list[Child] | None =None):
    self.children = children or []

  def __repr__(self):
    return f"Parent(children={self.children})"

@dbyte
@dataclass
class Child:
  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')])

from dataclasses import dataclass

from dinobytes import dbyte, unpackd


@dbyte
@dataclass
class ClassA:
  value: int

@dbyte
@dataclass
class ClassB:
  value: str

@dbyte
@dataclass
class ClassC:
  value:list[int]


classA = ClassA(1)
classB = ClassB('hello')
classC = ClassC([1,2,3])

for x in [
  bytes(classB),
  bytes(classC),
  bytes(classA)
]:
  match unpackd(x):
    case ClassA(value):
      print(f'ClassA: {value=}')
    case ClassB(value):
      print(f'ClassB: {value=}')
    case ClassC(value):
      print(f'ClassC: {value=}')
    case _:
      print('unknown class')

Output:

ClassB: value='hello'
ClassC: value=[1, 2, 3]
ClassA: value=1

Contributing

Contributions 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.

License

DinoBytes is released under the Apache 2.0 License. See the LICENSE file for more details.

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

dinobytes-0.3.1.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dinobytes-0.3.1-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file dinobytes-0.3.1.tar.gz.

File metadata

  • Download URL: dinobytes-0.3.1.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.3 Darwin/23.2.0

File hashes

Hashes for dinobytes-0.3.1.tar.gz
Algorithm Hash digest
SHA256 138328dbca567d968814f97175a90bda91924b8328db9c33c96a0eb96189a8dc
MD5 85743948852c1a901b4b6d06a6e46888
BLAKE2b-256 62408be1c2f792a3700d62e458ba32ddc457b4bc23033ae6bf00ec78c2eac67e

See more details on using hashes here.

File details

Details for the file dinobytes-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: dinobytes-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.3 Darwin/23.2.0

File hashes

Hashes for dinobytes-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ce756a4ad4a9bd705a4e04f58f29d43b6fdb42d67aebd5b25d9081fa16fd89eb
MD5 86598e84da3d47434399ee9c1bf5e1ff
BLAKE2b-256 0ad56b45de390efff1c1d23500d39fa9db3dfb8187aa9e0c29f5e985a7ec2234

See more details on using hashes here.

Supported by

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