Skip to content

Commit 655f70a

Browse files
authored
avoid errors when protoc is not installed (#61)
1 parent 31cdb5b commit 655f70a

File tree

1 file changed

+83
-55
lines changed

1 file changed

+83
-55
lines changed

_doc/examples/prog/plot_serialisation_protobuf.py

Lines changed: 83 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,16 @@
9090

9191

9292
cmd = "protoc --python_out=. schema.proto"
93-
out, err = run_cmd(cmd=cmd, wait=True)
94-
print(out)
95-
print(err)
93+
try:
94+
out, err = run_cmd(cmd=cmd, wait=True)
95+
use_protoc = True
96+
except FileNotFoundError as e:
97+
print(f"error: {e}")
98+
print("unable to use protoc")
99+
use_protoc = False
100+
if use_protoc:
101+
print(out)
102+
print(err)
96103

97104

98105
########################################
@@ -104,10 +111,12 @@
104111

105112
########################################
106113

107-
108-
with open("schema_pb2.py", "r") as f:
109-
content = f.read()
110-
print(content[:1000])
114+
if os.path.exists("schema_pb2.py"):
115+
with open("schema_pb2.py", "r") as f:
116+
content = f.read()
117+
print(content[:1000])
118+
else:
119+
print("schema_pb2.py missing")
111120

112121

113122
########################################
@@ -117,68 +126,81 @@
117126
# Pour utliser *protobuf*, il faut importer le module créé.
118127

119128

120-
sys.path.append(".")
121-
import schema_pb2 # noqa: E402
129+
if use_protoc:
130+
sys.path.append(".")
131+
import schema_pb2 # noqa: E402
122132

123133
########################################
124134
# On créé un enregistrement.
125135

126136

127-
person = schema_pb2.Person()
128-
person.id = 1234
129-
person.name = "John Doe"
130-
person.email = "[email protected]"
131-
phone = person.phones.add()
132-
phone.number = "555-4321"
133-
phone.type = schema_pb2.Person.HOME
137+
if use_protoc:
138+
person = schema_pb2.Person()
139+
person.id = 1234
140+
person.name = "John Doe"
141+
person.email = "[email protected]"
142+
phone = person.phones.add()
143+
phone.number = "555-4321"
144+
phone.type = schema_pb2.Person.HOME
134145

135146

136147
########################################
137148
#
138149

139-
person
150+
if use_protoc:
151+
person
140152

141153

142154
########################################
143155
# Sérialisation en chaîne de caractères
144156
# =====================================
145157

146158

147-
res = person.SerializeToString()
148-
type(res), res
159+
if use_protoc:
160+
res = person.SerializeToString()
161+
print(type(res), res)
149162

150163

151164
########################################
152165
#
153166

154-
timeit.timeit("person.SerializeToString()", globals=globals(), number=100)
167+
if use_protoc:
168+
print(timeit.timeit("person.SerializeToString()", globals=globals(), number=100))
155169

156170

157171
########################################
158172
#
159173

160-
pers = schema_pb2.Person.FromString(res)
161-
pers
174+
if use_protoc:
175+
pers = schema_pb2.Person.FromString(res)
176+
print(pers)
162177

163178

164179
########################################
165180
#
166181

167-
pers = schema_pb2.Person()
168-
pers.ParseFromString(res)
169-
pers
182+
if use_protoc:
183+
pers = schema_pb2.Person()
184+
pers.ParseFromString(res)
185+
print(pers)
170186

171187

172188
########################################
173189
#
174190

175-
timeit.timeit("schema_pb2.Person.FromString(res)", globals=globals(), number=100)
191+
if use_protoc:
192+
print(
193+
timeit.timeit(
194+
"schema_pb2.Person.FromString(res)", globals=globals(), number=100
195+
)
196+
)
176197

177198

178199
########################################
179200
#
180201

181-
timeit.timeit("pers.ParseFromString(res)", globals=globals(), number=100)
202+
if use_protoc:
203+
print(timeit.timeit("pers.ParseFromString(res)", globals=globals(), number=100))
182204

183205

184206
########################################
@@ -187,24 +209,24 @@
187209

188210

189211
db = []
190-
191-
person = schema_pb2.Person()
192-
person.id = 1234
193-
person.name = "John Doe"
194-
person.email = "[email protected]"
195-
phone = person.phones.add()
196-
phone.number = "555-4321"
197-
phone.type = schema_pb2.Person.HOME
198-
db.append(person)
199-
200-
person = schema_pb2.Person()
201-
person.id = 5678
202-
person.name = "Johnette Doette"
203-
person.email = "[email protected]"
204-
phone = person.phones.add()
205-
phone.number = "777-1234"
206-
phone.type = schema_pb2.Person.MOBILE
207-
db.append(person)
212+
if use_protoc:
213+
person = schema_pb2.Person()
214+
person.id = 1234
215+
person.name = "John Doe"
216+
person.email = "[email protected]"
217+
phone = person.phones.add()
218+
phone.number = "555-4321"
219+
phone.type = schema_pb2.Person.HOME
220+
db.append(person)
221+
222+
person = schema_pb2.Person()
223+
person.id = 5678
224+
person.name = "Johnette Doette"
225+
person.email = "[email protected]"
226+
phone = person.phones.add()
227+
phone.number = "777-1234"
228+
phone.type = schema_pb2.Person.MOBILE
229+
db.append(person)
208230

209231

210232
########################################
@@ -241,35 +263,41 @@
241263
########################################
242264
#
243265

244-
db2[0], db2[1]
266+
if db2:
267+
print(db2[0], db2[1])
245268

246269

247270
########################################
248271
# Sérialisation JSON
249272
# ==================
250273

251-
252-
print(MessageToJson(pers))
274+
if use_protoc:
275+
print(MessageToJson(pers))
253276

254277

255278
########################################
256279
#
257280

258-
timeit.timeit("MessageToJson(pers)", globals=globals(), number=100)
281+
if use_protoc:
282+
print(timeit.timeit("MessageToJson(pers)", globals=globals(), number=100))
259283

260284

261285
########################################
262286
#
263287

264288

265-
js = MessageToJson(pers)
266-
res = ParseJson(js, message=schema_pb2.Person())
267-
res
289+
if use_protoc:
290+
js = MessageToJson(pers)
291+
res = ParseJson(js, message=schema_pb2.Person())
292+
print(res)
268293

269294

270295
########################################
271296
#
272297

273-
timeit.timeit(
274-
"ParseJson(js, message=schema_pb2.Person())", globals=globals(), number=100
275-
)
298+
if use_protoc:
299+
print(
300+
timeit.timeit(
301+
"ParseJson(js, message=schema_pb2.Person())", globals=globals(), number=100
302+
)
303+
)

0 commit comments

Comments
 (0)