Example #1
0
def main():
    person = Person()
    with open("person.bin", "rb") as f:
        person.ParseFromString(f.read())
        print(person.id)
        print(person.age)
        print(person.name)
Example #2
0
def read_person():
    conf = {
        'bootstrap.servers': "localhost:9092",
        'group.id': "pb_consumer",
        'auto.offset.reset': 'smallest'
    }

    consumer = Consumer(conf)
    consumer.subscribe(["pb"])
    msg = consumer.poll(5)
    # Show that message body is an array of bytes
    print("message body type is: %s", type(msg.value()))

    # Use message payload to initialize a Person instance
    p = Person()
    p.ParseFromString(msg.value())
    print(p.id, p.name, p.email)
Example #3
0
def main():
    person = Person()
    person.id = 1
    person.age = 18
    person.name = "Jerry"

    with open("person.bin", "wb") as f:
        print(person.SerializeToString())
        f.write(person.SerializeToString())
Example #4
0
def handler(event, context):
    print("writing a new record to kafka")
    for i in range(10):
        person = Person()
        person.id = i
        person.name = str(uuid.uuid4())
        person.email = "*****@*****.**"
        # Serialize person to protobuf bytes
        msg = person.SerializeToString()

        # Write bytes to topic
        conf = {
            "bootstrap.servers": os.getenv("BOOTSTRAP_SERVERS"),
            "client.id": "producer"
        }
        admin = AdminClient(conf)
        try:
            admin.create_topics([NewTopic("pb", 1, 1)])
        except Exception as e:
            print("could not create the topic", e)

        producer = Producer(conf)
        producer.produce("pb", msg)
        print(producer.flush())
        print("a new person added to stream")
Example #5
0
def write_person():
    person = Person()
    person.id = 1
    person.name = "Alice Smith"
    person.email = "*****@*****.**"
    # Serialize person to protobuf bytes
    msg = person.SerializeToString()

    # Write bytes to topic
    conf = {"bootstrap.servers": "localhost:9092", "client.id": "producer"}
    producer = Producer(conf)
    producer.produce("pb", msg)
    print(producer.flush())
    print("a new person added to stream")
Example #6
0
from person_pb2 import Person

person = Person()

person.firstName = "John"
person.lastName = "Doe"
person.age = 24

serialized = person.SerializeToString()

with open('out.bin', 'wb') as f:
    f.write(serialized)
Example #7
0
from person_pb2 import Person

person = Person()
person.id = 1234
person.name = "Jhon"
person.email = "*****@*****.**"
print(person.SerializeToString())
import sys
import socket
from person_pb2 import Person
sock = socket . socket ()
sock . connect (( 'localhost', 1337))
Man = Person()
man_id=input("Enter identifyer")
name=input("Enter name")
Man.id=man_id
Man.name=name
body=Man.SerializeToString()
sock.send(body.encode())
while True:
    data = sock.recv (1024)
    print(data.decode())
    if not data:
        break
sock . close ()
from person_pb2 import Person

person = Person()
with open('in.bin', 'rb') as f:
    data = f.read()
    person.ParseFromString(data)
from person_pb2 import Person

person = Person()
print('before', person)
person.ParseFromString(b'\n\x04Jhon\x10\xd2\t\x1a\[email protected]')
print('after', person)
syntax = "proto3";
message Person {
  string name = 1;
  int32 id = 2;
  repeated string email = 3;
}

And let's compile it (the `--descriptor_set_out` and `--include_imports` options are only required for the `tf.io.decode_proto()` example below):

!protoc person.proto --python_out=. --descriptor_set_out=person.desc --include_imports

!ls person*

from person_pb2 import Person

person = Person(name="Al", id=123, email=["*****@*****.**"])  # create a Person
print(person)  # display the Person

person.name  # read a field

person.name = "Alice"  # modify a field

person.email[0]  # repeated fields can be accessed like arrays

person.email.append("*****@*****.**")  # add an email address

s = person.SerializeToString()  # serialize to a byte string
s

person2 = Person()  # create a new Person
person2.ParseFromString(s)  # parse the byte string (27 bytes)
Example #12
0
from person_pb2 import Person

person = Person(name="A1", id=123, email=["*****@*****.**"])
print(person)

person.email.append("*****@*****.**")
s = person.SerializeToString()

person2 = Person()
person2.ParseFromString(s)

print(person == person2)

#from features_pb2 import BytesList, FloatList, Int64List
#from features_pb2 import Feature, Features, Example
import tensorflow as tf

person_example = tf.train.Example(features=tf.train.Features(
    feature={
        "name":
        tf.train.Feature(bytes_list=tf.train.BytesList(value=[b"Alice"])),
        "id":
        tf.train.Feature(int64_list=tf.train.Int64List(value=[123])),
        "emails":
        tf.train.Feature(bytes_list=tf.train.BytesList(
            value=[b"*****@*****.**", b"*****@*****.**"]))
    }))

with tf.io.TFRecordWriter("my_contracts.tfrecord") as f:
    f.write(person_example.SerializeToString())
Example #13
0
def main():
    """A pedantic docstring."""
    person: _message = Person()
    person.should_warn = 123
Example #14
0
from person_pb2 import Person

person = Person()
print(person.name)  # should not raise E1101
print(person.should_warn)  # should raise E5901


class Foo:
    pass


Person = Foo  # FIXME: should be renamed by class def
person = Person()
print(person.renamed_should_warn)  # should raise E1101