{ "info": { "author": "Kriszti\u00e1n Sz\u0171cs", "author_email": "szucs.krisztian@gmail.com", "bugtrack_url": null, "classifiers": [], "description": "|Build Status|\n\nProxo\n=====\n\nExtend protobuf message with custom methods properties and additional\nattributes\n\nTL;DR\n-----\n\n.. code:: python\n\n\n from proxo import MessageProxy, encode, decode\n\n class Person(MessageProxy):\n proto = addressbook_pb2.Person # it can be more complex, like pattern matching, see below\n\n @property\n def firstname(self):\n return self.name.split(' ')[0]\n\n\n p = Person(name='Test Me')\n assert p.firstname == 'Test'\n assert decode(encode(p)) == p\n\nUsage\n-----\n\nGiven the addressbook protobuf definition\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n.. code:: protobuf\n\n\n package tutorial;\n\n message Person {\n required string name = 1;\n required int32 id = 2;\n optional string email = 3;\n\n enum PhoneType {\n MOBILE = 0;\n HOME = 1;\n WORK = 2;\n }\n\n message PhoneNumber {\n required string number = 1;\n optional PhoneType type = 2 [default = HOME];\n }\n\n repeated PhoneNumber phone = 4;\n }\n\n message AddressBook {\n repeated Person person = 1;\n }\n\nThe traditional way\n~~~~~~~~~~~~~~~~~~~\n\n.. code:: python\n\n\n import addressbook_pb2\n\n person = addressbook_pb2.Person()\n person.id = 1234\n person.name = \"John Doe\"\n person.email = \"jdoe@example.com\"\n phone = person.phone.add()\n phone.number = \"555-4321\"\n phone.type = addressbook_pb2.Person.HOME\n\nvia Proxo.dict\\_to\\_protobuf\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n.. code:: python\n\n\n from proxo import dict_to_protobuf, protobuf_to_dict\n\n data = {'id': 124,\n 'name': 'John Doe',\n 'email': 'jdoe@example.com',\n 'phone': {'number': '555-4321',\n 'type': 'HOME'}}\n\n proto = dict_to_protobuf(data, addressbook_pb2.Person)\n\n assert person == proto\n\n # converting back\n mapping = protobuf_to_dict(proto)\n mapping['phone']['number']\n mapping.phone.number # using dot notation\n\n assert mapping == data\n\nvia extending Proxo.MessageProxy\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n.. code:: python\n\n\n from proxo import MessageProxy, encode, decode\n\n # note that non defined types will be automatically proxied too\n\n class Person(MessageProxy):\n proto = addressbook_pb2.Person # it can be more complex, like pattern matching, see below\n\n @property\n def firstname(self):\n return self.name.split(' ')[0]\n\n def call(self):\n try:\n print('calling {}'.format(self.firstname))\n do_voip_call(self.phone.number)\n except:\n print('failed calling {} on his/her {} number'.format(self.firstname,\n self.phone.type.lower))\n\n obj = Person(id=124, name='John Doe', phone={'number': '555-4321',\n 'type': 'HOME'})\n obj.phone.type = 'MOBILE'\n assert obj.firsname == 'John'\n\n proto = encode(obj)\n john = decode(proto)\n\n # lets bother him\n john.call()\n\nMore Complicated Example\n------------------------\n\n.. code:: python\n\n\n\n import operator\n\n from uuid import uuid4\n from functools import partial\n from proxo import MessageProxy\n\n\n class Scalar(MessageProxy):\n proto = mesos_pb2.Value.Scalar\n\n\n class Resource(MessageProxy):\n proto = mesos_pb2.Resource # can be class\n\n\n class ScalarResource(Resource):\n proto = mesos_pb2.Resource(type=mesos_pb2.Value.SCALAR) # or partially set instance\n\n def __init__(self, value=None, **kwargs):\n super(Resource, self).__init__(**kwargs)\n if value is not None:\n self.scalar = Scalar(value=value)\n\n def __cmp__(self, other):\n first, second = float(self), float(other)\n if first < second:\n return -1\n elif first > second:\n return 1\n else:\n return 0\n\n def __repr__(self):\n return \"<{}: {}>\".format(self.__class__.__name__, self.scalar.value)\n\n def __float__(self):\n return float(self.scalar.value)\n\n @classmethod\n def _op(cls, op, first, second):\n value = op(float(first), float(second))\n return cls(value=value)\n\n def __add__(self, other):\n return self._op(operator.add, self, other)\n\n def __radd__(self, other):\n return self._op(operator.add, other, self)\n\n def __sub__(self, other):\n return self._op(operator.sub, self, other)\n\n def __rsub__(self, other):\n return self._op(operator.sub, other, self)\n\n def __mul__(self, other):\n return self._op(operator.mul, self, other)\n\n def __rmul__(self, other):\n return self._op(operator.mul, other, self)\n\n def __truediv__(self, other):\n return self._op(operator.truediv, self, other)\n\n def __rtruediv__(self, other):\n return self._op(operator.truediv, other, self)\n\n def __iadd__(self, other):\n self.scalar.value = float(self._op(operator.add, self, other))\n return self\n\n def __isub__(self, other):\n self.scalar.value = float(self._op(operator.sub, self, other))\n return self\n\n\n class Cpus(ScalarResource):\n proto = mesos_pb2.Resource(name='cpus', type=mesos_pb2.Value.SCALAR)\n\n\n class Mem(ScalarResource):\n proto = mesos_pb2.Resource(name='mem', type=mesos_pb2.Value.SCALAR)\n\n\n class Disk(ScalarResource):\n proto = mesos_pb2.Resource(name='disk', type=mesos_pb2.Value.SCALAR)\n\n.. |Build Status| image:: http://drone.lensa.com:8000/api/badges/kszucs/proxo/status.svg\n :target: http://drone.lensa.com:8000/kszucs/proxo", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "http://github.com/kszucs/proxo", "keywords": "protobuf dict object wrapper proxy", "license": "Apache License, Version 2.0", "maintainer": null, "maintainer_email": null, "name": "proxo", "package_url": "https://pypi.org/project/proxo/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/proxo/", "project_urls": { "Download": "UNKNOWN", "Homepage": "http://github.com/kszucs/proxo" }, "release_url": "https://pypi.org/project/proxo/1.0.2/", "requires_dist": null, "requires_python": null, "summary": "Object proxies (wrappers) for protobuf messages", "version": "1.0.2" }, "last_serial": 2448488, "releases": { "1.0.2": [ { "comment_text": "", "digests": { "md5": "8cbbd6665be62136a26c1127241c21a3", "sha256": "10f42447526b3098e40e100b82206a5e1e451550ac07d157f7ca11e625994315" }, "downloads": -1, "filename": "proxo-1.0.2-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "8cbbd6665be62136a26c1127241c21a3", "packagetype": "bdist_wheel", "python_version": "3.5", "requires_python": null, "size": 7743, "upload_time": "2016-11-08T07:38:10", "url": "https://files.pythonhosted.org/packages/c4/c0/0fa608b5ff7d71d4b0455d079fe44599e0feaf711232a98a0648cb2e9d69/proxo-1.0.2-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "def6fe7e2c2f9e48ba493196002aefaf", "sha256": "bc8f41669c29eb03d0b36d73c9786f4dd7e21e7699ca39ecaf1f7f06550d5da7" }, "downloads": -1, "filename": "proxo-1.0.2.tar.gz", "has_sig": false, "md5_digest": "def6fe7e2c2f9e48ba493196002aefaf", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5334, "upload_time": "2016-11-08T07:38:07", "url": "https://files.pythonhosted.org/packages/0b/8e/76b635b583e3660602ffe68e29d6a3e0cf5a53678b57c29f827f96981088/proxo-1.0.2.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "8cbbd6665be62136a26c1127241c21a3", "sha256": "10f42447526b3098e40e100b82206a5e1e451550ac07d157f7ca11e625994315" }, "downloads": -1, "filename": "proxo-1.0.2-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "8cbbd6665be62136a26c1127241c21a3", "packagetype": "bdist_wheel", "python_version": "3.5", "requires_python": null, "size": 7743, "upload_time": "2016-11-08T07:38:10", "url": "https://files.pythonhosted.org/packages/c4/c0/0fa608b5ff7d71d4b0455d079fe44599e0feaf711232a98a0648cb2e9d69/proxo-1.0.2-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "def6fe7e2c2f9e48ba493196002aefaf", "sha256": "bc8f41669c29eb03d0b36d73c9786f4dd7e21e7699ca39ecaf1f7f06550d5da7" }, "downloads": -1, "filename": "proxo-1.0.2.tar.gz", "has_sig": false, "md5_digest": "def6fe7e2c2f9e48ba493196002aefaf", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5334, "upload_time": "2016-11-08T07:38:07", "url": "https://files.pythonhosted.org/packages/0b/8e/76b635b583e3660602ffe68e29d6a3e0cf5a53678b57c29f827f96981088/proxo-1.0.2.tar.gz" } ] }