Skip to main content

0.1.11

Project description

version 0.1.11

Python DSL to leverage translation of dictionaries and SQLAlchemy into Protobuf objects

PyPi Project Page Travis Build Documentation Status Test Coverage Reports

What is Mercator ?

Mercator is a Python library that simplifies the following of serializing dictionary data into Protobuf binary data.

Mercator actually supports extracting data from:

  • dictionaries

  • SQLAlchemy model instances

  • Any opaque python objects (e.g.: collections.namedtuple)

When should I use Mercator ?

  • When migrating custom implementations of RPC to gGRPC.

  • When migrating in-memory data to Protobuf.

When should I not use Mercator ?

  • When writing gRPC services from scratch.

  • When writing anything that uses Protobuf gets called.

Install

pip install mercator

Documentation

https://mercator.readthedocs.org

Basic Usage

1. Given a protobuf declaration

syntax = "proto3";
package services.social_platform;

import "google/protobuf/timestamp.proto";

message User {
  message AuthToken {
    string value = 1;
    google.protobuf.Timestamp created_at = 2;
    google.protobuf.Timestamp expires_at = 3;
  }
}

2. Declare Mappings

from mercator import (
    ProtoMapping,
    ProtoKey,
    ProtoList,
    SinglePropertyMapping,
)
from google.protobuf.timestamp_pb2 import Timestamp
from google.protobuf.struct_pb2 import Struct
from google.protobuf import struct_pb2


ProtobufTimestamp = SinglePropertyMapping(int, Timestamp, 'seconds')

class UserAuthTokenMapping(ProtoMapping):
    __proto__ = domain_pb2.User.AuthToken
    value = ProtoKey('data', str)
    created_at = ProtoKey('created_at', ProtobufTimestamp)
    expires_at = ProtoKey('expires_at', ProtobufTimestamp)


class UserMapping(ProtoMapping):
    __proto__ = domain_pb2.User

    tokens = ProtoList('tokens', UserAuthTokenMapping)

3. Generate python files

python -m grpc_tools.protoc -I ./ --python_out=./ --grpc_python_out=./ ./*.proto

4. Process data!

info = {
    'login': 'Hulk',
    'email': 'bruce@avengers.world',
    'tokens': [
        {
            'data': 'this is the token',
            'created_at': 1552240433,
            'expires_at': 1552240733,
        }
    ],
}

user = UserMapping(info).to_protobuf()

assert isinstance(user, domain_pb2.User)

Contributing

  1. Check the code structure documentation

  2. Write tests

  3. Write code

  4. Send a pull-request

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

mercator-0.1.11.tar.gz (9.6 kB view details)

Uploaded Source

File details

Details for the file mercator-0.1.11.tar.gz.

File metadata

  • Download URL: mercator-0.1.11.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.0.0 requests-toolbelt/0.8.0 tqdm/4.36.1 CPython/3.7.6

File hashes

Hashes for mercator-0.1.11.tar.gz
Algorithm Hash digest
SHA256 7053e6eeccddccbfc0be38a8b74f9e343b1d1c97a5385fac61aa487be49555e1
MD5 886b6ad765a7663ac77f932c340a8a6e
BLAKE2b-256 18e876e799281f7282543f476bc86d8634f085af545a69c4498766eb4af3a81b

See more details on using hashes here.

Supported by

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