zerohedge
zerohedge

Reputation: 3725

Is there a Protostuff-equivalent Protobuff library for Python?

In Java, libraries like protostuff allow you to generate buffers from a Java POJO approximately like so:

Schema<Foo> schema = RuntimeSchema.getSchema(Foo.class);
...
protostuff = ProtostuffIOUtil.toByteArray(foo, schema, buffer);

I've been trying to find a similar solution for Python, but except for attempting to programmatically build Descriptors and FieldDescriptors (which come with their own challenges and problems since 4.x.x), I couldn't find anything. Is this simply impossible in Python, just isn't implemented anywhere, or am I missing something obvious here?

Upvotes: 1

Views: 881

Answers (1)

jpa
jpa

Reputation: 12176

The alternative Python protobuf libraries of pure-protobuf and python-betterproto both have their own syntax that can be used directly from Python, without a .proto file.

It however doesn't work for completely plain Python objects, as you still need to specify the field types and tag numbers (example from pure-protobuf):

@message
@dataclass
class SearchRequest:
    query: str = field(1, default='')
    page_number: int32 = field(2, default=int32(0))
    result_per_page: int32 = field(3, default=int32(0))

Upvotes: 1

Related Questions