|
90 | 90 |
|
91 | 91 |
|
92 | 92 | 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) |
96 | 103 |
|
97 | 104 |
|
98 | 105 | ########################################
|
|
104 | 111 |
|
105 | 112 | ########################################
|
106 | 113 |
|
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") |
111 | 120 |
|
112 | 121 |
|
113 | 122 | ########################################
|
|
117 | 126 | # Pour utliser *protobuf*, il faut importer le module créé.
|
118 | 127 |
|
119 | 128 |
|
120 |
| -sys.path.append(".") |
121 |
| -import schema_pb2 # noqa: E402 |
| 129 | +if use_protoc: |
| 130 | + sys.path.append(".") |
| 131 | + import schema_pb2 # noqa: E402 |
122 | 132 |
|
123 | 133 | ########################################
|
124 | 134 | # On créé un enregistrement.
|
125 | 135 |
|
126 | 136 |
|
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 |
134 | 145 |
|
135 | 146 |
|
136 | 147 | ########################################
|
137 | 148 | #
|
138 | 149 |
|
139 |
| -person |
| 150 | +if use_protoc: |
| 151 | + person |
140 | 152 |
|
141 | 153 |
|
142 | 154 | ########################################
|
143 | 155 | # Sérialisation en chaîne de caractères
|
144 | 156 | # =====================================
|
145 | 157 |
|
146 | 158 |
|
147 |
| -res = person.SerializeToString() |
148 |
| -type(res), res |
| 159 | +if use_protoc: |
| 160 | + res = person.SerializeToString() |
| 161 | + print(type(res), res) |
149 | 162 |
|
150 | 163 |
|
151 | 164 | ########################################
|
152 | 165 | #
|
153 | 166 |
|
154 |
| -timeit.timeit("person.SerializeToString()", globals=globals(), number=100) |
| 167 | +if use_protoc: |
| 168 | + print(timeit.timeit("person.SerializeToString()", globals=globals(), number=100)) |
155 | 169 |
|
156 | 170 |
|
157 | 171 | ########################################
|
158 | 172 | #
|
159 | 173 |
|
160 |
| -pers = schema_pb2.Person.FromString(res) |
161 |
| -pers |
| 174 | +if use_protoc: |
| 175 | + pers = schema_pb2.Person.FromString(res) |
| 176 | + print(pers) |
162 | 177 |
|
163 | 178 |
|
164 | 179 | ########################################
|
165 | 180 | #
|
166 | 181 |
|
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) |
170 | 186 |
|
171 | 187 |
|
172 | 188 | ########################################
|
173 | 189 | #
|
174 | 190 |
|
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 | + ) |
176 | 197 |
|
177 | 198 |
|
178 | 199 | ########################################
|
179 | 200 | #
|
180 | 201 |
|
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)) |
182 | 204 |
|
183 | 205 |
|
184 | 206 | ########################################
|
|
187 | 209 |
|
188 | 210 |
|
189 | 211 | 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) |
208 | 230 |
|
209 | 231 |
|
210 | 232 | ########################################
|
|
241 | 263 | ########################################
|
242 | 264 | #
|
243 | 265 |
|
244 |
| -db2[0], db2[1] |
| 266 | +if db2: |
| 267 | + print(db2[0], db2[1]) |
245 | 268 |
|
246 | 269 |
|
247 | 270 | ########################################
|
248 | 271 | # Sérialisation JSON
|
249 | 272 | # ==================
|
250 | 273 |
|
251 |
| - |
252 |
| -print(MessageToJson(pers)) |
| 274 | +if use_protoc: |
| 275 | + print(MessageToJson(pers)) |
253 | 276 |
|
254 | 277 |
|
255 | 278 | ########################################
|
256 | 279 | #
|
257 | 280 |
|
258 |
| -timeit.timeit("MessageToJson(pers)", globals=globals(), number=100) |
| 281 | +if use_protoc: |
| 282 | + print(timeit.timeit("MessageToJson(pers)", globals=globals(), number=100)) |
259 | 283 |
|
260 | 284 |
|
261 | 285 | ########################################
|
262 | 286 | #
|
263 | 287 |
|
264 | 288 |
|
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) |
268 | 293 |
|
269 | 294 |
|
270 | 295 | ########################################
|
271 | 296 | #
|
272 | 297 |
|
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