is there a protocol buffers for python wrapper that would allow object annotation in python code not it proto files?

  • Last Update :
  • Techknowledgy :

1. Download a current Protobuf release from Git:

wget https: //github.com/protocolbuffers/protobuf/releases/download/v3.12.4/protobuf-all-3.12.4.tar.gz

2. Extract the archive

tar - xzf protobuf - all - 3.12 .4.tar.gz

3. Install:

cd protobuf - 3.12 .4 / && . / configure && make && sudo make install
5._
syntax = "proto3";

import "generated/person_info.proto";

package persons;

message Person {
   PersonInfo info = 1; // characteristics of the person
   repeated Friend friends = 2; // friends of the person
}

message Friend {
   float friendship_duration = 1; // duration of friendship in days
   repeated string shared_hobbies = 2; // shared interests
   Person person = 3; // identity of the friend
}
6._
syntax = "proto3";

package persons;

enum Sex {
   M = 0; // male 
   F = 1; // female
   O = 2; // other
}

message PersonInfo {
   int32 age = 1; // age in years
   Sex sex = 2;
   int32 height = 3; // height in cm
}
syntax = "proto3";

import "generated/person_info.proto";

package persons;

message Person {
   PersonInfo info = 1; // characteristics of the person
   repeated Friend friends = 2; // friends of the person
}

message Friend {
   float friendship_duration = 1; // duration of friendship in days
   repeated string shared_hobbies = 2; // shared interests
   Person person = 3; // identity of the friend
}
syntax = "proto3";

package persons;

enum Sex {
   M = 0; // male 
   F = 1; // female
   O = 2; // other
}

message PersonInfo {
   int32 age = 1; // age in years
   Sex sex = 2;
   int32 height = 3; // height in cm
}
mkdir src / generated
protoc src / interfaces / person_info.proto--python_out src / --proto_path generated = . / src / interfaces /
   protoc src / interfaces / person.proto--python_out src / --proto_path generated = . / src / interfaces /
_PERSON = _descriptor.Descriptor(
      name = 'Person',
      full_name = 'persons.Person',
      filename = None,
      file = DESCRIPTOR,
      containing_type = None,
      create_key = _descriptor._internal_create_key,
      fields = [
         ...
# fill protobuf objects
import generated.person_pb2 as person_pb2
import generated.person_info_pb2 as person_info_pb2
# # # # # # # # # # # #
# define friend
for person of interest
# # # # # # # # # # # # #
friend_info = person_info_pb2.PersonInfo()
friend_info.age = 40
friend_info.sex = person_info_pb2.Sex.M
friend_info.height = 165
friend_person = person_pb2.Person()
friend_person.info.CopyFrom(friend_info)
friend_person.friends.extend([]) # no friends: -(
      # # # # # # # # define friendship characteristics # # # # # # # # friendship = person_pb2.Friend() friendship.friendship_duration = 365.1 friendship.shared_hobbies.extend(["books", "daydreaming", "unicorns"]) friendship.person.CopyFrom(friend_person) # # # # # # # # assign the friend to the friend of interest # # # # # # # # # person_info = person_info_pb2.PersonInfo() person_info.age = 30 person_info.sex = person_info_pb2.Sex.M person_info.height = 184 person = person_pb2.Person() person.info.CopyFrom(person_info) person.friends.extend([friendship]) # person with a single friend

Suggestion : 2

Published: June 8, 2017

message Metric {
   required string name = 1;
   required string type = 2;
   required float value = 3;
   repeated string tags = 4;
}
import metric_pb2

my_metric = metric_pb2.Metric()
my_metric.name = 'sys.cpu'
my_metric.type = 'gauge'
my_metric.value = 99.9
my_metric.tags.extend([‘my_tag’, ‘foo: bar’])

with open('out.bin', 'wb') as f:
   f.write(my_metric.SerializeToString())
with open('out.bin', 'rb') as f:
   read_metric = metric_pb2.Metric()
read_metric.ParseFromString(f.read())
# do something with read_metric
from google.protobuf.internal.encoder
import _VarintBytes
from google.protobuf.internal.decoder
import _DecodeVarint32
with open('out.bin', 'wb') as f:
   my_tags = (“my_tag”, “foo: bar”)
for i in range(128):
   my_metric = metric_pb2.Metric()
my_metric.name = 'sys.cpu'
my_metric.type = 'gauge'
my_metric.value = round(random(), 2)
my_metric.tags.extend(my_tags)
size = my_metric.ByteSize()
f.write(_VarintBytes(size))
f.write(my_metric.SerializeToString())
with open('out.bin', 'rb') as f:
   buf = f.read()
n = 0
while n < len(buf):
   msg_len, new_pos = _DecodeVarint32(buf, n)
n = new_pos
msg_buf = buf[n: n + msg_len]
n += msg_len
read_metric = metric_pb2.Metric()
read_metric.ParseFromString(msg_buf)
# do something with read_metric

Suggestion : 3

Before creating the proto file, we need to install the proto compiler known as protoc. To install it, visit Github and download the python protoc compiler. We have installed the 64 bit version window.,Protobuf is developed for data sharing across the cross language applications. Let's have an example of how to create protobuf. First we need to specify our data structure in the .proto file. Let's take the example of following student.proto file.,Now we will generate the integration code using the protobuf compiler (protoc) into the language-specific integration. We use the below command.,Google came with the solution in the form of the Protobuf. We will use the protobuf and learn how it is different from the regular data encoding. Let's start with the introduction of Protobuf.

student_id: 101
student_name: "Megha"
student {
   total_marks: 500
   marks_obtain: 425
}

student_id: 102
student_name: "Peter"
student {
   result: FAIL
   total_marks: 500
   marks_obtain: 136
}
{
   student_id: 101
   student_name: "Megha" {
      student {
         total_marks: 500
         marks_obtain: 425
      }
   }
}
student_id: 101
student_name: "Mathew"
student {
   total_marks: 500
   marks_obtain: 136
}

Suggestion : 4

It assembles the Protobuf Compiler (protoc) command line and uses it to generate Java source files out of your proto files.,This plugin integrates with the idea plugin and automatically registers the proto files and generated Java code as sources.,The Gradle plugin that compiles Protocol Buffer (aka. Protobuf) definition files (*.proto) in your project. There are two pieces of its job:,The Protobuf plugin assumes Protobuf files (*.proto) are organized in the same way as Java source files, in sourceSets. The Protobuf files of a sourceSet (or variant in an Android project) are compiled in a single protoc run, and the generated files are added to the input of the Java compilation run of that sourceSet (or variant).

buildscript {
   repositories {
      gradlePluginPortal()
   }
   dependencies {
      classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.19'
   }
}
buildscript {
   repositories {
      mavenLocal()
   }
   dependencies {
      classpath 'com.google.protobuf:protobuf-gradle-plugin:0.9.0-SNAPSHOT'
   }
}
apply plugin: 'java'
apply plugin: 'com.google.protobuf'
apply plugin: 'com.android.application' // or 'com.android.library'
apply plugin: 'com.google.protobuf'
plugins {
   id "com.google.protobuf"
   version "0.8.19"
   id "java"
}
sourceSets {
   main {
      proto {
         // In addition to the default 'src/main/proto'
         srcDir 'src/main/protobuf'
         srcDir 'src/main/protocolbuffers'
         // In addition to the default '**/*.proto' (use with caution).
         // Using an extension other than 'proto' is NOT recommended,
         // because when proto files are published along with class files, we can
         // only tell the type of a file from its extension.
         include '**/*.protodevel'
      }
      java {
         ...
      }
   }
   test {
      proto {
         // In addition to the default 'src/test/proto'
         srcDir 'src/test/protocolbuffers'
      }
   }
}