问题描述:

In Python, is it possible at run time to convert a Google Protocol Buffers .proto file into a python class that reads that data? Python is a very dynamic language. When you use protoc to convert a .proto file to python source code, the generated code makes a lot of use of python metaclasses, so it's already very dynamic.

Ideally, I'm thinking of something like this:

import whatever

module = whatever.load_from_file("myfile.proto")

Is this possible?

(I am new to protocol buffers, please let me know if my question makes no sense)

网友答案:

In theory, all the pieces exist to make this work. The Python protobuf implementation could call the C++ .proto parser library (libprotoc) as a C extension to get Descriptors, and then could feed those into the metaclasses.

However, as far as I know, no one has quite tied it altogether. (Disclaimer: My knowledge is a few years old, I may have missed a new development, but I don't see anything in the docs.)

Incidentally, Cap'n Proto's Python implementation does do what you describe, proving it is possible. But that doesn't help you if you need to work with Protobuf format.

(Disclosure: I was the author of most of Google's open source Protobuf code, and I am also the author of Cap'n Proto.)

相关阅读:
Top