pyparcel

Release v1.0.0 (Installation)

https://travis-ci.org/najaco/pyparcel.svg?branch=master https://pepy.tech/badge/pyparcel

pyparcel is the simple and secure way to convert python objects to bytes. When pyparcel performs load on an object, it uses recursive introspection on the objects variables and uses the struct module to load each one in order. (some additional rules may apply on dynamic data structures, e.g. str, list, etc.) When pyparcel performs unload on a string of bytes, it again performs recursive introspection and fills variables passed with what is apparent in the byte string. By default, int s are stored as "i" with the standard size of 4 bytes, and float s are stored as "f" with a standard size of 4 bytes. If you would like to change how specific values are stored, see strict types.

Example

import pyparcel

foo: Foo = Foo(8, 5.7, "Hello World")  # Foo(int, float, str)
data: bytes = pyparcel.load(foo)  # b'\x08\x00\x00\x00ff\xb6@\x0b\x00\x00\x00Hello World'
# ...
baz: Foo = pyparcel.unload(data, Foo())  # foo == baz

Comparison to other modules

Comparison to pickle

While pickle is great for convenience, it also lacks security because it allows the execution of arbitrary functions. pyparcel is different because the user knows what objects are expected when unpacking the byte string. pyparcel recursively introspects the passed object and fills the instance variables with what was given in the byte string.

Comparison to struct

struct is not as scalable as pyparcel. struct requires you to specify the order and way data is packed, and does not allow you to pack objects without deconstructing it first. While it may be faster during runtime, it requires the developer to change code in multiple places, if they decide to customize the data they are passing. pyparcel acts as a wrapper around struct, allowing objects to be converted to byte strings, since the format specifier for struct is decided at runtime.