Skip to main content

numproto provides numpy arrays to protobuf conversion

Project description

NumProto

NumProto is a simple python3.6+ library to serialize and deserialize numpy arrays into and from protobuf messages.

Installation

You can install NumProto from the PyPI package:

$ pip install numproto

Usage

NumProto serializes a numpy array into an NDArray message as specified in ndarray.proto:

syntax = "proto3";

package numproto.protobuf;

message NDArray {
    bytes ndarray = 1;
}

This library provides two methods: one for serialization ndarray_to_proto and one for deserialization proto_to_ndarray.

import numpy as np

from numproto import ndarray_to_proto, proto_to_ndarray

nda = np.arange(10)

serialized_nda = ndarray_to_proto(nda)
deserialized_nda = proto_to_ndarray(serialized_nda)

assert np.array_equal(nda, deserialized_nda)

Re-using the proto files in another project

Re-distributing *.proto files is notoriously difficult. In order to make this easier the ndarray.proto file is installed together with the python package.

This allows us to simply pass the site-packages path as an import path to protoc.

To get the correct path of site-packages check the Location key when running pip show numproto.

For example let's say we want to create a new proto file and import NDArray to use within one of our defined messages:

syntax = "proto3";

import "numproto/protobuf/ndarray.proto";

message MyMessage {
    numproto.protobuf.NDArray my_array = 1;
}

And to compile using grpcio-tools simply do:

$ python -m grpc_tools.protoc -I/usr/lib/python3.6/site-packages/ -I./ --python_out=. --grpc_python_out=. my_proto.proto

(you may need to adjust the location of site-packages)

Tests

To run the tests first install the numproto package from source in development mode and then run the tests using pytest:

$ git clone https://github.com/xainag/numproto.git
$ cd numproto
$ pip install -e .[dev]

$ pytest

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

numproto-0.2.0.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

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

numproto-0.2.0-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file numproto-0.2.0.tar.gz.

File metadata

  • Download URL: numproto-0.2.0.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.8

File hashes

Hashes for numproto-0.2.0.tar.gz
Algorithm Hash digest
SHA256 bd1fe6772625bbc623f165baa78ebbb3e4aad5f3be0b11999c987b282ecb92f7
MD5 b90aa98a3817ddd7905f40dce63d65d8
BLAKE2b-256 31e3404c0aadbd37e8eebd0afadf58969b9360a2df3a3e94f613cde111d157fd

See more details on using hashes here.

File details

Details for the file numproto-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: numproto-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.8

File hashes

Hashes for numproto-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b517faa19470e60919aba50e163a0888ea239451b96b0ee1b4cf8309fd18cdb2
MD5 1dee9c931a4fe3f389646e549809ef82
BLAKE2b-256 3d2bc18684002653da1fdc91a2d48c8ab6e6b742db7b6b9816005f04f5191e76

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