{ "info": { "author": "Weston Renoud", "author_email": "wrenoud@gmail.com", "bugtrack_url": null, "classifiers": [], "description": "SerializableObject\r\n==================\r\n\r\nA pythonic semantic for describing binary data structures and associated\r\npython objects.\r\n\r\nInstallation\r\n------------\r\n\r\n::\r\n\r\n pip install cypyserialize\r\n\r\nBasic Usage\r\n-----------\r\n\r\nFor this first example we're going to define a simple 2D point with the\r\nattributes ``x`` and ``y`` representing binary doubles.\r\n\r\n.. code:: python\r\n\r\n class Point(cypyserialize.SerializableObject):\r\n \"Basic point class\"\r\n x = cypyserialize.double()\r\n y = cypyserialize.double()\r\n\r\nObject instances of the ``Point`` class, which extends the\r\n``SerializableObject``, can be initialized in several different ways.\r\nFirst, omitting any parameters the instance will be populated with\r\ndefault values (here we haven't specified defaults so 0.0 is assumed).\r\nThe attributes can be assigned values after initialization.\r\n\r\n.. code:: python\r\n\r\n >>> p = Point() # create default instance\r\n >>> list(p.items())\r\n [('x', None), ('y', None)]\r\n >>> p.x = 5000.0 # set x\r\n >>> p.y = 300.5 # set y\r\n >>> list(p.items())\r\n [('x', 5000.0), ('y', 300.5)]\r\n\r\nAlternately, the object can be initialized with our values. The\r\nparameter order should match the order specified in the class attribute\r\n``_field_order``.\r\n\r\n.. code:: python\r\n\r\n >>> p = Point(5000.0, 300.5)\r\n >>> list(p.items())\r\n [('x', 5000.0), ('y', 300.5)]\r\n\r\nOr we can use the attribute names if we forgot the order.\r\n\r\n.. code:: python\r\n\r\n >>> p = Point(y=300.5, x=5000.0)\r\n >>> list(p.items())\r\n [('x', 5000.0), ('y', 300.5)]\r\n\r\nOr mix the two (just remeber that after the first named parameter\r\nsubsequent parameters will have to be named as well).\r\n\r\n.. code:: python\r\n\r\n >>> p = Point(5000.0, y=300.5)\r\n >>> list(p.items())\r\n [('x', 5000.0), ('y', 300.5)]\r\n\r\nTo get the binary representation just call the class method ``pack``\r\n\r\n.. code:: python\r\n\r\n >>> p.pack()\r\n bytearray(b'\\x00\\x00\\x00\\x00\\x00\\x88\\xb3@\\x00\\x00\\x00\\x00\\x00\\xc8r@')\r\n\r\nLastly, we can initialize with a binary string.\r\n\r\n.. code:: python\r\n\r\n >>> p = Point(b'\\x00\\x00\\x00\\x00\\x00\\x88\\xb3@\\x00\\x00\\x00\\x00\\x00\\xc8r@')\r\n >>> list(p.items())\r\n [('x', 5000.0), ('y', 300.5)]\r\n\r\nUsing Substructures\r\n-------------------\r\n\r\nWe're going to reuse the ``Point`` class to describe a rectangular\r\nbounding box with two attributes, the northwest and southeast corners.\r\n\r\n.. code:: python\r\n\r\n class BoundingBox(cypyserialize.SerializableObject):\r\n northwest = Point()\r\n southeast = Point()\r\n\r\nSeriously, it's that easy. Let's initialize one of these.\r\n\r\n.. code:: python\r\n\r\n >>> bb = BoundingBox()\r\n >>> list(bb.items())\r\n [('northwest', ), ('southeast', )]\r\n\r\nLet's try that again but with some points\r\n\r\n.. code:: python\r\n\r\n >>> bb = BoundingBox(Point(0.0, 10.0), Point(15.0, 0.0))\r\n >>> bb.northwest.y\r\n 10.0\r\n >>> bb.southeast.x\r\n 15.0\r\n\r\nOverloading\r\n-----------\r\n\r\nSubclasses of SerializableObject can be extended and overloaded. This\r\ncan be especially handy if you have a standard structure that you don't\r\nwant to redefine.\r\n\r\nHere we're going to make a simple datagram structure with a start and\r\nend flag, a timestamp and some arbitrary body that we'll overload in a\r\nsecond.\r\n\r\n.. code:: python\r\n\r\n class GenericDatagram(cypyserialize.SerializableObject):\r\n STX = cypyserialize.uchar(value=0x02)\r\n timestamp = cypyserialize.uint()\r\n body = cypyserialize.none()\r\n ETX = cypyserialize.uchar(value=0x03)\r\n\r\nNow that we have generic datagram lets make it a wrapper on the\r\nBoundingBox structure we defined earlier as an extension of the\r\nGenericDatagram structure.\r\n\r\n.. code:: python\r\n\r\n class BoundingBoxDatagram(GenericDatagram):\r\n body = BoundingBox()\r\n\r\nThat's it. Lets create one of this. We'll set the timestamp and get the\r\nbinary.\r\n\r\n.. code:: python\r\n\r\n >>> p = BoundingBoxDatagram()\r\n >>> p.timestamp = time.time()\r\n >>> p.body = BoundingBox(Point(0, 10), Point(10, 0))\r\n >>> p.items()\r\n [('STX', 2), ('timestamp', 1398373100.412985), ('body', <__main__.BoundingBox object at 0xb713f3c4>), ('ETX', 3)]\r\n >>> p.pack()\r\n '\\x02...'\r\n\r\nArrays of Substructures\r\n-----------------------\r\n\r\nNow we're going to reuse the point structure to describe a path as a\r\nseries of points with a description.\r\n\r\n.. code:: python\r\n\r\n class Path(cypyserialize.SerializableObject):\r\n # the points\r\n points = cypyserialize.SerializableArray(\r\n Point(),\r\n count=cypyserialize.uint()\r\n )\r\n\r\n``count`` with be the field type that is used to read and write the\r\nnumber of ``Point()`` objects in the structure.\r\n\r\nByte Order\r\n----------\r\n\r\n***Currently Unsupported***\r\n\r\nByte order is always little endian at present.\r\n\r\nCustom Computed Attributes\r\n--------------------------\r\n\r\n.. code:: python\r\n\r\n class BetterBoundingBox(BoundingBox):\r\n def __init__(self):\r\n self.area = (self.southeast.x - self.northwest.x) * \\\r\n (self.northwest.y - self.southeast.y)\r\n\r\nNote that we need to accept ``args`` and ``kargs``, we don't need to\r\ncall the superclass **init** though as it wasn't defined on BoundingBox.\r\n\r\nLets try it out.\r\n\r\n.. code:: python\r\n\r\n >>> bb = BetterBoundingBox(Point(0,10),Point(10,0))\r\n >>> print bb.area\r\n 100", "description_content_type": null, "docs_url": null, "download_url": "https://github.com/wrenoud/cypyserialize/tarball/1.0.4", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/wrenoud/cypyserialize", "keywords": "testing,logging,example", "license": "Apache 2.0", "maintainer": null, "maintainer_email": null, "name": "cypyserialize", "package_url": "https://pypi.org/project/cypyserialize/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/cypyserialize/", "project_urls": { "Download": "https://github.com/wrenoud/cypyserialize/tarball/1.0.4", "Homepage": "https://github.com/wrenoud/cypyserialize" }, "release_url": "https://pypi.org/project/cypyserialize/1.0.4/", "requires_dist": null, "requires_python": null, "summary": "Really easy, really quick, binary parser framework for Python", "version": "1.0.4" }, "last_serial": 2197078, "releases": { "1.0.4": [ { "comment_text": "", "digests": { "md5": "8a8391b959f14b44825084717408d062", "sha256": "14f1ec6102e5996fcf4ff8c98b49abd132fcf29cc0ce77479648fe195d42832f" }, "downloads": -1, "filename": "cypyserialize-1.0.4.zip", "has_sig": false, "md5_digest": "8a8391b959f14b44825084717408d062", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 174526, "upload_time": "2016-07-01T00:18:57", "url": "https://files.pythonhosted.org/packages/8d/5c/3fc5e9245a386c896af6bee6aeb4b76156afc51be0fe3bb98d8d16c1764c/cypyserialize-1.0.4.zip" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "8a8391b959f14b44825084717408d062", "sha256": "14f1ec6102e5996fcf4ff8c98b49abd132fcf29cc0ce77479648fe195d42832f" }, "downloads": -1, "filename": "cypyserialize-1.0.4.zip", "has_sig": false, "md5_digest": "8a8391b959f14b44825084717408d062", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 174526, "upload_time": "2016-07-01T00:18:57", "url": "https://files.pythonhosted.org/packages/8d/5c/3fc5e9245a386c896af6bee6aeb4b76156afc51be0fe3bb98d8d16c1764c/cypyserialize-1.0.4.zip" } ] }