-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
avoid errors when protoc is not installed (#61)
- Loading branch information
Showing
1 changed file
with
83 additions
and
55 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,9 +90,16 @@ | |
|
||
|
||
cmd = "protoc --python_out=. schema.proto" | ||
out, err = run_cmd(cmd=cmd, wait=True) | ||
print(out) | ||
print(err) | ||
try: | ||
out, err = run_cmd(cmd=cmd, wait=True) | ||
use_protoc = True | ||
except FileNotFoundError as e: | ||
print(f"error: {e}") | ||
print("unable to use protoc") | ||
use_protoc = False | ||
if use_protoc: | ||
print(out) | ||
print(err) | ||
|
||
|
||
######################################## | ||
|
@@ -104,10 +111,12 @@ | |
|
||
######################################## | ||
|
||
|
||
with open("schema_pb2.py", "r") as f: | ||
content = f.read() | ||
print(content[:1000]) | ||
if os.path.exists("schema_pb2.py"): | ||
with open("schema_pb2.py", "r") as f: | ||
content = f.read() | ||
print(content[:1000]) | ||
else: | ||
print("schema_pb2.py missing") | ||
|
||
|
||
######################################## | ||
|
@@ -117,68 +126,81 @@ | |
# Pour utliser *protobuf*, il faut importer le module créé. | ||
|
||
|
||
sys.path.append(".") | ||
import schema_pb2 # noqa: E402 | ||
if use_protoc: | ||
sys.path.append(".") | ||
import schema_pb2 # noqa: E402 | ||
|
||
######################################## | ||
# On créé un enregistrement. | ||
|
||
|
||
person = schema_pb2.Person() | ||
person.id = 1234 | ||
person.name = "John Doe" | ||
person.email = "[email protected]" | ||
phone = person.phones.add() | ||
phone.number = "555-4321" | ||
phone.type = schema_pb2.Person.HOME | ||
if use_protoc: | ||
person = schema_pb2.Person() | ||
person.id = 1234 | ||
person.name = "John Doe" | ||
person.email = "[email protected]" | ||
phone = person.phones.add() | ||
phone.number = "555-4321" | ||
phone.type = schema_pb2.Person.HOME | ||
|
||
|
||
######################################## | ||
# | ||
|
||
person | ||
if use_protoc: | ||
person | ||
|
||
|
||
######################################## | ||
# Sérialisation en chaîne de caractères | ||
# ===================================== | ||
|
||
|
||
res = person.SerializeToString() | ||
type(res), res | ||
if use_protoc: | ||
res = person.SerializeToString() | ||
print(type(res), res) | ||
|
||
|
||
######################################## | ||
# | ||
|
||
timeit.timeit("person.SerializeToString()", globals=globals(), number=100) | ||
if use_protoc: | ||
print(timeit.timeit("person.SerializeToString()", globals=globals(), number=100)) | ||
|
||
|
||
######################################## | ||
# | ||
|
||
pers = schema_pb2.Person.FromString(res) | ||
pers | ||
if use_protoc: | ||
pers = schema_pb2.Person.FromString(res) | ||
print(pers) | ||
|
||
|
||
######################################## | ||
# | ||
|
||
pers = schema_pb2.Person() | ||
pers.ParseFromString(res) | ||
pers | ||
if use_protoc: | ||
pers = schema_pb2.Person() | ||
pers.ParseFromString(res) | ||
print(pers) | ||
|
||
|
||
######################################## | ||
# | ||
|
||
timeit.timeit("schema_pb2.Person.FromString(res)", globals=globals(), number=100) | ||
if use_protoc: | ||
print( | ||
timeit.timeit( | ||
"schema_pb2.Person.FromString(res)", globals=globals(), number=100 | ||
) | ||
) | ||
|
||
|
||
######################################## | ||
# | ||
|
||
timeit.timeit("pers.ParseFromString(res)", globals=globals(), number=100) | ||
if use_protoc: | ||
print(timeit.timeit("pers.ParseFromString(res)", globals=globals(), number=100)) | ||
|
||
|
||
######################################## | ||
|
@@ -187,24 +209,24 @@ | |
|
||
|
||
db = [] | ||
|
||
person = schema_pb2.Person() | ||
person.id = 1234 | ||
person.name = "John Doe" | ||
person.email = "[email protected]" | ||
phone = person.phones.add() | ||
phone.number = "555-4321" | ||
phone.type = schema_pb2.Person.HOME | ||
db.append(person) | ||
|
||
person = schema_pb2.Person() | ||
person.id = 5678 | ||
person.name = "Johnette Doette" | ||
person.email = "[email protected]" | ||
phone = person.phones.add() | ||
phone.number = "777-1234" | ||
phone.type = schema_pb2.Person.MOBILE | ||
db.append(person) | ||
if use_protoc: | ||
person = schema_pb2.Person() | ||
person.id = 1234 | ||
person.name = "John Doe" | ||
person.email = "[email protected]" | ||
phone = person.phones.add() | ||
phone.number = "555-4321" | ||
phone.type = schema_pb2.Person.HOME | ||
db.append(person) | ||
|
||
person = schema_pb2.Person() | ||
person.id = 5678 | ||
person.name = "Johnette Doette" | ||
person.email = "[email protected]" | ||
phone = person.phones.add() | ||
phone.number = "777-1234" | ||
phone.type = schema_pb2.Person.MOBILE | ||
db.append(person) | ||
|
||
|
||
######################################## | ||
|
@@ -241,35 +263,41 @@ | |
######################################## | ||
# | ||
|
||
db2[0], db2[1] | ||
if db2: | ||
print(db2[0], db2[1]) | ||
|
||
|
||
######################################## | ||
# Sérialisation JSON | ||
# ================== | ||
|
||
|
||
print(MessageToJson(pers)) | ||
if use_protoc: | ||
print(MessageToJson(pers)) | ||
|
||
|
||
######################################## | ||
# | ||
|
||
timeit.timeit("MessageToJson(pers)", globals=globals(), number=100) | ||
if use_protoc: | ||
print(timeit.timeit("MessageToJson(pers)", globals=globals(), number=100)) | ||
|
||
|
||
######################################## | ||
# | ||
|
||
|
||
js = MessageToJson(pers) | ||
res = ParseJson(js, message=schema_pb2.Person()) | ||
res | ||
if use_protoc: | ||
js = MessageToJson(pers) | ||
res = ParseJson(js, message=schema_pb2.Person()) | ||
print(res) | ||
|
||
|
||
######################################## | ||
# | ||
|
||
timeit.timeit( | ||
"ParseJson(js, message=schema_pb2.Person())", globals=globals(), number=100 | ||
) | ||
if use_protoc: | ||
print( | ||
timeit.timeit( | ||
"ParseJson(js, message=schema_pb2.Person())", globals=globals(), number=100 | ||
) | ||
) |