Last active
January 9, 2018 01:30
-
-
Save mexisme/9aeaa4161117854817daad954ba4a890 to your computer and use it in GitHub Desktop.
Protobuf Python Experiment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PIPENV=pipenv | |
PIP=pip | |
PROTO_DIR=proto | |
VENDOR_DIR=vendored | |
PROTO_SOURCES=$(wildcard *.proto) | |
PROTO_TARGETS=$(patsubst %.proto,$(PROTO_DIR)/%_pb2.py,$(PROTO_SOURCES)) | |
all: vendorise gen-proto | |
run: test.py | |
vendorise: $(VENDOR_DIR) | |
$(VENDOR_DIR): requirements.txt | |
$(PIPENV) run -- $(PIP) install -r "$<" --target "$@" --no-deps | |
.INTERMEDIATE: requirements.txt | |
Pipfile.lock requirements.txt: Pipfile | |
$(PIPENV) lock -r >"$@" | |
gen-proto: $(PROTO_TARGETS) | |
$(PROTO_TARGETS): | $(PROTO_DIR)/ | |
$(PROTO_DIR)/%_pb2.py: %.proto | |
protoc --python_out='$(PROTO_DIR)' '$<' | |
$(PROTO_DIR)/: | |
@mkdir -p "$@" | |
test.py: vendorise gen-proto | |
./test.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[[source]] | |
url = "https://pypi.python.org/simple" | |
verify_ssl = true | |
name = "pypi" | |
[packages] | |
protobuf = ">3.0" | |
[dev-packages] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
import sys | |
# Add vendor directory to module search path | |
parent_dir = os.path.abspath(os.path.dirname(__file__)) | |
vendor_dir = os.path.join(parent_dir, 'vendored') | |
sys.path.append(vendor_dir) | |
from proto import test_message1_pb2, test_message2_pb2 | |
from google.protobuf import text_format | |
from google.protobuf import json_format | |
def first(): | |
print("First parser") | |
a = test_message1_pb2.Smoke() | |
a_parse_text = test_message1_pb2.Smoke() | |
a_parse_json = test_message1_pb2.Smoke() | |
a.jones = "Test" | |
a.wales = 512 | |
a_text = text_format.MessageToString(a) | |
a_json = json_format.MessageToJson(a) | |
print("PB:\n{!r}\nText: {!r}\nJSON: {!r}".format(a, a_text, a_json)) | |
text_format.Merge(a_text, a_parse_text) | |
print("Text parsed:\n{!r}".format(a_parse_text)) | |
json_format.Parse(a_json, a_parse_json) | |
print("JSON parsed:\n{!r}".format(a_parse_json)) | |
def second(): | |
print("Second parser") | |
a = test_message2_pb2.Event() | |
a.api.name = "eapr.event" | |
a.api.version = "1.2.3" | |
a.smoke.jones = "Woah!!" | |
a.smoke.wales = 1024 | |
a.smoke.blown.is_it = True | |
a.smoke.blown.texture = "rough" | |
print("Event to JSON:\n{!s}".format(json_format.MessageToJson(a))) | |
first() | |
second() | |
print("DONE") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
syntax = "proto3"; | |
message Smoke { | |
string jones = 1; | |
int64 wales = 2; | |
Blown blown = 3; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
syntax = "proto3"; | |
import "google/protobuf/api.proto"; | |
message Event { | |
google.protobuf.Api api = 1; | |
Smoke smoke = 2; | |
} | |
message Smoke { | |
string jones = 1; | |
int64 wales = 2; | |
Blown blown = 3; | |
} | |
message Blown { | |
bool is_it = 1; | |
string texture = 2; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment