Coder Social home page Coder Social logo

dinobytes's Introduction

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.

dinobytes's People

Contributors

f0ursqu4r3 avatar

Stargazers

wegfawefgawefg avatar Ben Aaron avatar

Watchers

 avatar Kostas Georgiou avatar

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.