Skip to content

Commit

Permalink
avoid errors when protoc is not installed (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
xadupre authored Jan 10, 2024
1 parent 31cdb5b commit 655f70a
Showing 1 changed file with 83 additions and 55 deletions.
138 changes: 83 additions & 55 deletions _doc/examples/prog/plot_serialisation_protobuf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


########################################
Expand All @@ -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")


########################################
Expand All @@ -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))


########################################
Expand All @@ -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)


########################################
Expand Down Expand Up @@ -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
)
)

0 comments on commit 655f70a

Please sign in to comment.