proto赋值、解析、转换为字典、获取字段类型,python

一、proto协议内容

test.proto
//个人信息
message PersonInfo
{
optional bytes gender=1;
optional uint32 age=2;
}
//课程信息
message CourseInfo
{
optional bytes coursename=1;
optional uint32 grade=3;
}

//我的信息
message MyInfo
{
optional string name=1;
optional PersonInfo person=2;
repeated CourseInfo course_list=3;
repeated bytes interest_list=4;
}

二、将proto转换为python类文件

安装protoc
执行指令:
protoc --python_out=. test.proto

生成 test_pb2.py

三、proto赋值

if __name__=="__main__":
    personinfo=test_pb2.PersonInfo()
    setattr(personinfo,"gender","female")
    setattr(personinfo,"age",1)

    course1=test_pb2.CourseInfo()
    setattr(course1,"coursename","高数")
    setattr(course1,"grade",87)

    course2 = test_pb2.CourseInfo()
    setattr(course1, "coursename", "英语")
    setattr(course1, "grade", 97)


    myinfo=test_pb2.MyInfo()
    #标准类型赋值
    setattr(myinfo,"name","lyyname")
    #对象类型赋值
    myinfo.person.CopyFrom(personinfo)
    #repeated对象类型赋值
    myinfo.course_list.extend([course1,course2])
    #repeated标准类型赋值
    myinfo.interest_list.extent(["跑步","游泳","羽毛球"])

    #序列化为字符串
    myinfo_string=myinfo.SerializeToString()

四、proto解析

#解析字符串
    myinfo_parse=test_pb2.MyInfo()
    myinfo_parse.ParseFromString(myinfo_string)
    print myinfo.name
    print myinfo.person
    print myinfo.course_list
    for course in myinfo.course_list:
        print course
    print myinfo.interest_list
    for interest in myinfo.interest_list
        print interest

五、proto对象转换为字典

def pb2dict(obj):
    """
    Takes a ProtoBuf Message obj and convertes it to a dict.
    """
    adict = {
     }
    if not obj.IsInitialized():
        return None

    for field in obj.DESCRIPTOR.fields:
        if not getattr(obj, field.name):
            continue
        from google.protobuf.descriptor import FieldDescriptor
        if not field.label == FieldDescriptor.LABEL_REPEATED:
            if not field.type == FieldDescriptor.TYPE_MESSAGE:
                adict[field.name] = getattr(obj, field.name)
            else:
                value = pb2dict(getattr(obj, field.name))
                if value:
                    adict[field.name] = value
        else:
            if field.type == FieldDescriptor.TYPE_MESSAGE:
                adict[field.name] = [pb2dict(v) for v in getattr(obj, field.name)]
            else:
                adict[field.name] = [v for v in getattr(obj, field.name)]
    return adict

五、根据字段名称,获取proto的字段类型

def get_F2F_field_type(field_name):
    type_dict={
     
        "1": "double",
        "2": "float",
        "3": "int64",
        "4": "uint64",
        "5": "int32",
        "6": "fixed64",
        "7": "fixed32",
        "8": "bool",
        "9": "string",
        "10": "group",
        "11": "message",
        "12": "bytes",
        "13": "uint32",
        "14": "enum",
        "15": "sfixed32",
        "16": "sfixed64",
        "17": "sint32",
        "18": "sint64"
    }
    obj=test_pb2.MyInfo()
    for field in obj.DESCRIPTOR.fields:
        if field.name==field_name:
            return type_dict[str(field.type)]

你可能感兴趣的:(Python,转换,转换dict,解析proto,proto赋值,repeated赋值)