protobuf消息的对象代理(包装器)

proxo的Python项目详细描述


Build Status

项目

使用自定义方法属性和其他 属性

tl;dr

fromproxoimportMessageProxy,encode,decodeclassPerson(MessageProxy):proto=addressbook_pb2.Person# it can be more complex, like pattern matching, see below@propertydeffirstname(self):returnself.name.split(' ')[0]p=Person(name='Test Me')assertp.firstname=='Test'assertdecode(encode(p))==p

使用量

给定地址簿协议定义
packagetutorial;messagePerson{requiredstringname=1;requiredint32id=2;optionalstringemail=3;enumPhoneType{MOBILE=0;HOME=1;WORK=2;}messagePhoneNumber{requiredstringnumber=1;optionalPhoneTypetype=2[default=HOME];}repeatedPhoneNumberphone=4;}messageAddressBook{repeatedPersonperson=1;}

传统方式
importaddressbook_pb2person=addressbook_pb2.Person()person.id=1234person.name="John Doe"person.email="jdoe@example.com"phone=person.phone.add()phone.number="555-4321"phone.type=addressbook_pb2.Person.HOME

通过proxo.dict_到protobuf
fromproxoimportdict_to_protobuf,protobuf_to_dictdata={'id':124,'name':'John Doe','email':'jdoe@example.com','phone':{'number':'555-4321','type':'HOME'}}proto=dict_to_protobuf(data,addressbook_pb2.Person)assertperson==proto# converting backmapping=protobuf_to_dict(proto)mapping['phone']['number']mapping.phone.number# using dot notationassertmapping==data

通过扩展proxo.MessageProxy
fromproxoimportMessageProxy,encode,decode# note that non defined types will be automatically proxied tooclassPerson(MessageProxy):proto=addressbook_pb2.Person# it can be more complex, like pattern matching, see below@propertydeffirstname(self):returnself.name.split(' ')[0]defcall(self):try:print('calling {}'.format(self.firstname))do_voip_call(self.phone.number)except:print('failed calling {} on his/her {} number'.format(self.firstname,self.phone.type.lower))obj=Person(id=124,name='John Doe',phone={'number':'555-4321','type':'HOME'})obj.phone.type='MOBILE'assertobj.firsname=='John'proto=encode(obj)john=decode(proto)# lets bother himjohn.call()

更复杂的例子

importoperatorfromuuidimportuuid4fromfunctoolsimportpartialfromproxoimportMessageProxyclassScalar(MessageProxy):proto=mesos_pb2.Value.ScalarclassResource(MessageProxy):proto=mesos_pb2.Resource# can be classclassScalarResource(Resource):proto=mesos_pb2.Resource(type=mesos_pb2.Value.SCALAR)# or partially set instancedef__init__(self,value=None,**kwargs):super(Resource,self).__init__(**kwargs)ifvalueisnotNone:self.scalar=Scalar(value=value)def__cmp__(self,other):first,second=float(self),float(other)iffirst<second:return-1eliffirst>second:return1else:return0def__repr__(self):return"<{}: {}>".format(self.__class__.__name__,self.scalar.value)def__float__(self):returnfloat(self.scalar.value)@classmethoddef_op(cls,op,first,second):value=op(float(first),float(second))returncls(value=value)def__add__(self,other):returnself._op(operator.add,self,other)def__radd__(self,other):returnself._op(operator.add,other,self)def__sub__(self,other):returnself._op(operator.sub,self,other)def__rsub__(self,other):returnself._op(operator.sub,other,self)def__mul__(self,other):returnself._op(operator.mul,self,other)def__rmul__(self,other):returnself._op(operator.mul,other,self)def__truediv__(self,other):returnself._op(operator.truediv,self,other)def__rtruediv__(self,other):returnself._op(operator.truediv,other,self)def__iadd__(self,other):self.scalar.value=float(self._op(operator.add,self,other))returnselfdef__isub__(self,other):self.scalar.value=float(self._op(operator.sub,self,other))returnselfclassCpus(ScalarResource):proto=mesos_pb2.Resource(name='cpus',type=mesos_pb2.Value.SCALAR)classMem(ScalarResource):proto=mesos_pb2.Resource(name='mem',type=mesos_pb2.Value.SCALAR)classDisk(ScalarResource):proto=mesos_pb2.Resource(name='disk',type=mesos_pb2.Value.SCALAR)

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java我能在这个程序中更好地使用内存吗?   为什么我的Java while循环迭代了一半   java IntelliJ IDEA不在构建时复制资源   socket仅在Java TCP服务器输出上检查客户端断开连接   java游戏物理摩擦   java片段onClick调用方法   symja数学分析器中无法识别java Abs[x]   java在使用泛型时创建二进制搜索树类的实例?   java在外键约束表上的添加和删除   语法java表达式的含义,如果有条件   java创建内联对象并作为参数传递   是否有相当于Redis排序集(zset)的Java数据结构   java找不到适合的方法(无参数)   音频文件操作给定字节帧的音量Java   Eclipse4不以JavaWebStart启动   java如何使用org在JSON对象中获取嵌套的键元素。json?   java与Jackson的反序列化:“org.codehaus.Jackson.map.JsonMappingException:无法反序列化[projectname]的实例。”   字符串的Java正则表达式   spring集成上的java检测缺火指令