Skip to main content

PyCerializer

Lightweight serialization module for Python.

Package version Package version

PyCerializer is a lightweight serialization module for Python. The aim of PyCerializer is to produce serialized data that can be easily read in other programming languages such as C/C++ and others.

Requirements

There are no external dependencies. The package is based on standard python module struct that is available in all supported python versions, but the PyCerializer use also typing module that was introduced in Python 3.5.

Supported types:

  • Numbers
    • [u]int64_t
    • [u]int32_t
    • [u]int16_t
    • [u]int8_t
  • Strings
  • Structures

Endianess:

  • little,
  • big.

Installation

pip install pycerializer

Examples

Serialize the Python list and save to file:

original = (1, 123, 4321)
packed = pack_list_num(original, 'int16_t', 'little')
with open('file.bin', 'wb') as f:
    f.write(packed)

Deserialize the list using Python:

with open('file.bin', 'rb') as f:
    packed = f.read()
    unpacked = unpack_list_num(*packed, 'int16_t', 'little')

Deserialize the list using C/C++:

FILE *f = fopen("file.bin", "rb");
const int n = 3;
int16_t buff[n];
fread(buff, sizeof(int16_t), n, f);
for (int i = 0; i < n; i++) printf("%d ", buff[i] );

Serialize and deserialize the list of dictionaries:

    # data
    data = [{
        'name': b'John',
        'age': 34,
        'height': 177,
        'surname': b'Smith',
        'weight': 86
    }, 
    {
        'name': b'Andrew',
        'age': 43,
        'height': 187,
        'surname': b'Bluebaum',
        'weight': 67
    }, {
        'name': b'Michael',
        'age': 38,
        'height': 189,
        'surname': b'Brown',
        'weight': 99
    }]

    # data field:type mapping
    data_map = {
        'name': 'string',
        'age': 'int8_t',
        'height': 'int32_t',
        'surname': 'string',
        'weight': 'int8_t'
    }

    # packing
    data_bytes, data_elements, _, data_elements_size = pack_list_dict(data, data_map)

    # unpacking
    data_unpacked = unpack_list_dict(data_bytes, data_map, data_elements)

    # metadata
    meta = {
        'number_of_elements': data_elements,
    }

    # metadata field:type mapping
    meta_map = {
        'number_of_elements': 'uint64_t'
    }

    meta_bytes, _ = pack_dict(meta, meta_map)
    elements_size_bytes, _ = pack_list_num(data_elements_size, 'int64_t')

    # write bytes to a file
    with open('list_of_dicts.bin', 'wb') as f:
        f.write(meta_bytes)
        f.write(elements_size_bytes)
        f.write(data_bytes)

    # Get the C struct to use it later in C code
    print(get_c_struct(meta_map, name='meta_t'))
    print(get_c_struct(data_map, name='data_t'))

Deserialize and print the data from file using C/C++:

// Copied from Python output
typedef struct _meta_t {
    uint64_t number_of_elements;
} meta_t;


typedef struct _data_t {
    char * name;
    int8_t age;
    int32_t height;
    char * surname;
    int8_t weight;
} data_t;

// Read the data from buffer
size_t data_t_read(data_t *obj, uint8_t *bytes) {
    int64_t *name_size = (int64_t *)bytes;           bytes += sizeof(int64_t);
    obj->name = (char*)calloc(*name_size + 1, sizeof(char));

    memcpy(obj->name, bytes, *name_size);            bytes += *name_size;
    memcpy(&(obj->age), bytes, sizeof(uint8_t));     bytes += sizeof(uint8_t);
    memcpy(&(obj->height), bytes, sizeof(uint32_t)); bytes += sizeof(uint32_t);

    int64_t *surname_size = (int64_t *)bytes;        bytes += sizeof(int64_t);
    obj->surname = calloc(*surname_size + 1, sizeof(char));

    memcpy(obj->surname, bytes, *surname_size);      bytes += *surname_size;
    memcpy(&(obj->weight), bytes, sizeof(int8_t));   bytes += sizeof(int8_t);

    return 0;
}

// Never forget to deallocate the memory
void data_t_free(data_t obj) {
    free(obj.name);
    free(obj.surname);
}


int main() {
    FILE *f = fopen("list_of_dicts.bin", "rb");
    meta_t meta;
    fread(&meta, sizeof(meta_t), 1, f);

    data_t *data = (data_t*)malloc(sizeof(data_t) * meta.number_of_elements);
    int64_t* sizes = (int64_t*)malloc(meta.number_of_elements * sizeof(int64_t*));
    fread(sizes, meta.number_of_elements, sizeof(int64_t), f);
    printf("Number of elements: %lu\n", meta.number_of_elements);

    for (uint64_t i = 0; i < meta.number_of_elements; i++) {
        uint8_t *buff = (uint8_t*)malloc(sizes[i]);
        fread(buff, sizes[i], 1, f);
        data_t_read(data + i, buff);
        printf("%lu. %s %s, age: %d, height: %d, weight: %d\n", i, data[i].name, data[i].surname, data[i].age, data[i].height, data[i].weight);
        data_t_free(data[i]);
        free(buff);
    }
    fclose(f);
    free(sizes);
    free(data);

    return 0;
}

Limitation

This module works well for flat data, and definitely, there is much more effort needed to store and read data than using, for example, pickle. On the other hand, it may take much more effort to read pickled data in C++. Pycerializer was written ad-hoc for another project and was used for prototyping in Python, where there was a need to read the output in C++, which is the case where this module works quite well. The number of supported types is very limited but can be easily extended. Any contribution is welcome.

Metadata

Release files for pycerializer 0.5.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pycerializer 0.5.7
File Size Uploaded Provenance Actions
pycerializer-0.5.7.tar.gz 18.5 kB Without provenance Details for pycerializer-0.5.7.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pycerializer 0.5.7
File Interpreter ABI Platform Actions
pycerializer-0.5.7-py2.py3-none-any.whl Python 3, Python 2 none any Details for pycerializer-0.5.7-py2.py3-none-any.whl

Total release size: 37.0 kB

Release files / pycerializer-0.5.7.tar.gz

Download URL pycerializer-0.5.7.tar.gz
Size 18.5 kB
Tags Source
SHA-256 checksum
How to use checksums
f355bc3f122002aee5211656bd270fcace6fb818bc9469e915465124a57204e4
BLAKE2b-256 checksum
How to use checksums
1bd1cf76761618430735c8b442b3b5d6c63dca762a7664843b20b2744718b286
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.23.0 requests-toolbelt/0.9.1 tqdm/4.44.1 CPython/3.7.4

Release files / pycerializer-0.5.7-py2.py3-none-any.whl

Download URL pycerializer-0.5.7-py2.py3-none-any.whl
Size 18.5 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
0b17873c79954d6c5adb402e899d1f315e24acfb4d709d31d9f385986dce1a75
BLAKE2b-256 checksum
How to use checksums
69a6bb6d32ef3bffcb5c86dfbf486175920f72b22a3322358d156f43062c0e89
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.23.0 requests-toolbelt/0.9.1 tqdm/4.44.1 CPython/3.7.4

Security for pycerializer 0.5.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page