{ "info": { "author": "Nicolas BESSOU", "author_email": "nicolas.bessou@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 3 - Alpha", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3.3", "Topic :: Software Development :: Libraries :: Python Modules" ], "description": "=============\nmutabletuple\n=============\n\nOverview\n========\n\nSimilar to namedlist, but with additional features:\n* Print like a native python dictionary\n* Improve support for nested mutabletuple\n* Conversion to dictionary is done recursively\n* Can iterate using iteritems like dictionary\n* Merge nested mutable tuple from dict or other mutabletuple\n* MtFactory support arguments\n* Nested pickle support\n\n**Warning** Pickling works, but only for mutabletuple with default values.\n\nTypical usage\n=============\n\nYou can use mutabletuple like a mutable namedtuple::\n\n >>> from mutabletuple import mutabletuple\n >>> from collections import OrderedDict\n\n >>> Point = mutabletuple('Point', 'x y')\n >>> p = Point(1, 3)\n >>> p.x = 2\n >>> assert p.x == 2\n >>> assert p.y == 3\n\nOr, you can specify a default value for all fields::\n\n >>> Point = mutabletuple('Point', ['x', 'y'], default=3)\n >>> p = Point(y=2)\n >>> assert p.x == 3\n >>> assert p.y == 2\n\nOr, you can specify per-field default values::\n\n >>> Point = mutabletuple('Point', [('x', 0), ('y', 100)])\n >>> p = Point()\n >>> assert p.x == 0\n >>> assert p.y == 100\n\nIf you use a mapping, the value MtNoDefault is convenient to specify\nthat a field uses the default value::\n\n >>> from mutabletuple import MtNoDefault\n >>> Point = mutabletuple('Point', OrderedDict((('y', MtNoDefault),\n ... ('x', 100))),\n ... default=5)\n >>> p = Point()\n >>> assert p.x == 100\n >>> assert p.y == 5\n\nNamedlist-like behavior\n=======================\n\nA mutabletuple behaves almost like a namedlist. Check the documentation of namedlist for more details: https://pypi.python.org/pypi/namedlist\n\nAdditional features\n===================\n\nAdditional class members\n------------------------\n\nmutabletuple class contain these members:\n\n* _asdict(): Returns a dict which maps field names to their\n corresponding values.\n\n* _fields: Tuple of strings listing the field names. Useful for introspection.\n\n* merge: Recursively merge with a dict or another mutabletuple.\n\n* orderedDict: Recursively convert a mutabletuple into an ordered dict.\n\n* iteritems: To iterate like a dict.\n\n\nMutable default values\n----------------------\n\nFor mutabletuple, be aware of specifying mutable default\nvalues. Due to the way Python handles default values, each instance of\na mutabletuple will share the default. This is especially problematic\nwith default values that are lists. For example::\n\n >>> A = mutabletuple('A', [('x', [])])\n >>> a = A()\n >>> a.x.append(4)\n >>> b = A()\n >>> assert b.x == [4]\n\nThis is probably not the desired behavior, so see the next section.\n\n\nSpecifying a factory function for default values\n------------------------------------------------\n\nFor mutabletuple, you can supply a zero-argument callable for a\ndefault, by wrapping it in a MtFactory call. The only change in this\nexample is to change the default from `[]` to `MtFactory(list)`. But\nnote that `b.x` is a new list object, not shared with `a.x`::\n\n >>> from mutabletuple import MtFactory\n >>> A = mutabletuple('A', [('x', MtFactory(list))])\n >>> a = A()\n >>> a.x.append(4)\n >>> b = A()\n >>> assert b.x == []\n\nEvery time a new instance is created, your callable (in this case,\n`list`), will be called to produce a new instance for the default\nvalue.\n\nSpecifying arguments to a factory function\n------------------------------------------\n\nWhen using nested mutabletuple, you might want to provide arguments\nfor the creation of nested mutabletuple. Here is an example of\nhow to do it::\n\n >>> Point = mutabletuple('Point', [('x', 0), ('y', 0)])\n >>> Vector = mutabletuple('Vector', [('p1', MtFactory(Point, 2)),\n ... ('p2', MtFactory(Point, 4, 8))])\n >>> v1 = Vector()\n >>> assert(v1 == Vector(Point(2, 0), Point(4, 8)))\n\nInitialized points are created every time a vector is created.\n\n\nMerging mutabletuple with mutabletuple or dict\n----------------------------------------------\n\nWhen working with nested mutabletuple, it might be useful to be able\nto merge recursively with a dictionary that represents only a subset\nof the mutabletuple::\n\n >>> Point = mutabletuple('Point', [('x', 0), ('y', 0)])\n >>> Vector = mutabletuple('Vector', [('p1', MtFactory(Point)),\n ... ('p2', MtFactory(Point))])\n >>> Shape = mutabletuple('Shape', [('v1', MtFactory(Vector)),\n ... ('v2', MtFactory(Vector))])\n >>> s = Shape()\n >>> d = {'v1': {'p1': {'x': 20}, 'p2': Point(30, 40)}}\n >>> s.merge(d)\n >>> assert(s._asdict() == {\n ... 'v1': {'p1': {'x': 20, 'y': 0}, 'p2': {'x': 30, 'y': 40}},\n ... 'v2': {'p1': {'x': 0, 'y': 0}, 'p2': {'x': 0, 'y': 0}}})\n\n\nIterating over instances\n------------------------\n\nBecause instances are iterable (like lists or tuples), iteration works\nthe same way. Values are returned in definition order::\n\n >>> Point = mutabletuple('Point', 'x y z t')\n >>> p = Point(1.0, 42.0, 3.14, 2.71828)\n >>> for value in p:\n ... print(value)\n 1.0\n 42.0\n 3.14\n 2.71828\n\nCreating and using instances\n============================\n\nBecause the type returned by mutabletuple is a normal\nPython class, you create instances as you would with any Python class.\n\n\nChange log\n==========\n\n0.1 2015-03-21 Nicolas Bessou\n-----------------------------\n\n* Initial release.\n* Brings some additional features to a namedlist in order to support nested mutabletuple and recursive merge of the class.", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/nicolasbessou/mutabletuple", "keywords": null, "license": "MIT", "maintainer": null, "maintainer_email": null, "name": "mutabletuple", "package_url": "https://pypi.org/project/mutabletuple/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/mutabletuple/", "project_urls": { "Download": "UNKNOWN", "Homepage": "https://github.com/nicolasbessou/mutabletuple" }, "release_url": "https://pypi.org/project/mutabletuple/0.2/", "requires_dist": null, "requires_python": null, "summary": "Similar to namedlist, but with additional features (nested class, recursive merge, etc).", "version": "0.2" }, "last_serial": 1471396, "releases": { "0.1.2": [ { "comment_text": "", "digests": { "md5": "8e6a75ff6e136f13b487040fc88853f6", "sha256": "a5cdf77e34a42a96a365aa322f8f555328867037012ff48cda580d3839ee9d8d" }, "downloads": -1, "filename": "mutabletuple-0.1.2.tar.gz", "has_sig": false, "md5_digest": "8e6a75ff6e136f13b487040fc88853f6", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6859, "upload_time": "2015-03-21T17:00:42", "url": "https://files.pythonhosted.org/packages/df/9b/eb24188378aae5b8ecda8cba10bd3a2889bb5d082cdf4dc53f52f5d45e34/mutabletuple-0.1.2.tar.gz" } ], "0.2": [ { "comment_text": "", "digests": { "md5": "69877ee5074618f32dd54a829bc44f8c", "sha256": "93f747ccd3a3c3bd0a50585231219b1e3bd3508efc5239883d034faa5ea98d35" }, "downloads": -1, "filename": "mutabletuple-0.2.tar.gz", "has_sig": false, "md5_digest": "69877ee5074618f32dd54a829bc44f8c", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6911, "upload_time": "2015-03-21T17:29:57", "url": "https://files.pythonhosted.org/packages/80/af/24cd2071eff9d049b83903be5a0b002c00b5b69c52965e2d410f8d6846e1/mutabletuple-0.2.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "69877ee5074618f32dd54a829bc44f8c", "sha256": "93f747ccd3a3c3bd0a50585231219b1e3bd3508efc5239883d034faa5ea98d35" }, "downloads": -1, "filename": "mutabletuple-0.2.tar.gz", "has_sig": false, "md5_digest": "69877ee5074618f32dd54a829bc44f8c", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6911, "upload_time": "2015-03-21T17:29:57", "url": "https://files.pythonhosted.org/packages/80/af/24cd2071eff9d049b83903be5a0b002c00b5b69c52965e2d410f8d6846e1/mutabletuple-0.2.tar.gz" } ] }