{ "info": { "author": "Phillip J. Eby", "author_email": "peak@eby-sarna.com", "bugtrack_url": null, "classifiers": [], "description": "Simple Proxy Types\n==================\n\nThe ``peak.util.proxies`` module provides some useful base classes for creating\nproxies and wrappers for ordinary Python objects. Proxy objects automatically\ndelegate all attribute access and operations to the proxied object. Wrappers\nare similar, but can be subclassed to allow additional attributes and\noperations to be added to the wrapped object.\n\nNote that these proxy types are not intended to be tamper-proof; the unproxied\nform of an object can be readily accessed using a proxy's ``__subject__``\nattribute, and some proxy types even allow this attribute to be set. (This can\nbe handy for algorithms that lazily create circular structures and thus need to\nbe able to hand out \"forward reference\" proxies.)\n\n.. contents:: **Table of Contents**\n\nProxy Basics\n------------\n\nHere's a quick demo of the ``ObjectProxy`` type::\n\n >>> from peak.util.proxies import ObjectProxy\n >>> p = ObjectProxy(42)\n\n >>> p\n 42\n\n >>> isinstance(p, int)\n True\n\n >>> p.__class__\n <... 'int'>\n\n >>> p*2\n 84\n\n >>> 'X' * p\n 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'\n\n >>> hex(p)\n '0x2a'\n\n >>> chr(p)\n '*'\n\n >>> p ^ 1\n 43\n\n >>> p ** 2\n 1764\n\n\nAs you can see, a proxy is virtually indistinguishable from the object it\nproxies, except via its ``__subject__`` attribute, and its ``type()``::\n\n >>> p.__subject__\n 42\n\n >>> type(p)\n \n\nYou can change the ``__subject__`` of an ``ObjectProxy``, and it will then\nrefer to something else::\n\n >>> p.__subject__ = 99\n >>> p\n 99\n >>> p-33\n 66\n\n >>> p.__subject__ = \"foo\"\n >>> p\n 'foo'\n\nAll operations are delegated to the subject, including setattr and delattr::\n\n >>> class Dummy: pass\n >>> d = Dummy()\n >>> p = ObjectProxy(d)\n\n >>> p.foo = \"bar\"\n >>> d.foo\n 'bar'\n\n >>> del p.foo\n >>> hasattr(d,'foo')\n False\n\n\nCallback Proxies\n----------------\n\nSometimes, you may want a proxy's subject to be determined dynamically whenever\nthe proxy is used. For this purpose, you can use the ``CallbackProxy`` type,\nwhich accepts a callback function and creates a proxy that will invoke the\ncallback in order to get the target. Here's a quick example of a counter\nthat gets incremented each time it's used, from zero to three::\n\n >>> from peak.util.proxies import CallbackProxy\n\n >>> ct = -1\n >>> def callback():\n ... global ct\n ... if ct == 3: raise StopIteration\n ... ct += 1\n ... return ct\n\n >>> counter = CallbackProxy(callback)\n\n >>> counter\n 0\n >>> counter\n 1\n >>> str(counter)\n '2'\n >>> hex(counter)\n '0x3'\n\n >>> counter\n Traceback (most recent call last):\n ...\n StopIteration\n\nAs you can see, the callback is automatically invoked on any attempt to use the\nproxy. This is a somewhat silly example; a better one would be something like\na ``thread_id`` proxy that is always equal to the ID # of the thread it's\nrunning in.\n\nA callback proxy's callback can be obtained or changed via the ``get_callback``\nand ``set_callback`` functions::\n\n >>> from peak.util.proxies import get_callback, set_callback\n >>> set_callback(counter, lambda: 42)\n\n >>> counter\n 42\n\n >>> get_callback(counter)\n at ...>\n\n\nLazy Proxies\n------------\n\nA ``LazyProxy`` is similar to a ``CallbackProxy``, but its callback is called\nat most once, and then cached::\n\n >>> from peak.util.proxies import LazyProxy\n\n >>> def callback():\n ... print(\"called!\")\n ... return 42\n\n >>> lazy = LazyProxy(callback)\n >>> lazy\n called!\n 42\n >>> lazy\n 42\n\nYou can use the ``get_callback`` and ``set_callback`` functions on lazy\nproxies, but it has no effect if the callback was already called::\n\n >>> set_callback(lazy, lambda: 99)\n >>> lazy\n 42\n\nBut you can use the ``get_cache`` and ``set_cache`` functions to tamper with\nthe cached value::\n\n >>> from peak.util.proxies import get_cache, set_cache\n >>> get_cache(lazy)\n 42\n >>> set_cache(lazy, 99)\n >>> lazy\n 99\n\n\nWrappers\n--------\n\nThe ``ObjectWrapper``, ``CallbackWrapper`` and ``LazyWrapper`` classes are\nsimilar to their proxy counterparts, except that they are intended to be\nsubclassed in order to add custom extra attributes or methods. Any attribute\nthat exists in a subclass of these classes will be read or written from the\nwrapper instance, instead of the wrapped object. For example::\n\n >>> from peak.util.proxies import ObjectWrapper\n >>> class NameWrapper(ObjectWrapper):\n ... name = None\n ... def __init__(self, ob, name):\n ... ObjectWrapper.__init__(self, ob)\n ... self.name = name\n ... def __str__(self):\n ... return self.name\n\n >>> w = NameWrapper(42, \"The Ultimate Answer\")\n >>> w\n 42\n\n >>> print(w)\n The Ultimate Answer\n\n >>> w * 2\n 84\n\n >>> w.name\n 'The Ultimate Answer'\n\nNotice that any attributes you add must be defined *in the class*. You can't\nadd arbitary attributes at runtime, because they'll be set on the wrapped\nobject instead of the wrapper::\n\n >>> w.foo = 'bar'\n Traceback (most recent call last):\n ...\n AttributeError: 'int' object has no attribute 'foo'\n\nNote that this means that all instance attributes must be implemented as either\nslots, properties, or have a default value defined in the class body (like the\n``name = None`` shown in the example above).\n\nThe ``CallbackWrapper`` and ``LazyWrapper`` base classes are basically the same\nas ``ObjectWrapper``, except that they use a callback or cached lazy callback\ninstead of expecting an object as their subject.\n\n\nCreating Custom Subclasses and Mixins\n-------------------------------------\n\nIn addition to all the concrete classes described above, there are also two\nabstract base classes: ``AbstractProxy`` and ``AbstractWrapper``. If you want\nto create a mixin type that can be used with any of the concrete types, you\nshould subclass the abstract version and set ``__slots__`` to an empty list::\n\n >>> from peak.util.proxies import AbstractWrapper\n\n >>> class NamedMixin(AbstractWrapper):\n ... __slots__ = []\n ... name = None\n ... def __init__(self, ob, name):\n ... super(NamedMixin, self).__init__(ob)\n ... self.name = name\n ... def __str__(self):\n ... return self.name\n\nThen, when you mix it in with the respective base class, you can add back in\nany necessary slots, or leave off ``__slots__`` to give the subclass instances\na dictionary of their own::\n\n >>> from peak.util.proxies import CallbackWrapper, LazyWrapper\n\n >>> class NamedObject(NamedMixin, ObjectWrapper): pass\n >>> class NamedCallback(NamedMixin, CallbackWrapper): pass\n >>> class NamedLazy(NamedMixin, LazyWrapper): pass\n\n >>> print(NamedObject(42, \"The Answer\"))\n The Answer\n\n >>> n = NamedCallback(callback, \"Test\")\n >>> n\n called!\n 42\n >>> n\n called!\n 42\n\n >>> n = NamedLazy(callback, \"Once\")\n >>> n\n called!\n 42\n >>> n\n 42\n\nBoth the ``AbstractProxy`` and ``AbstractWrapper`` base classes work by\nassuming that ``self.__subject__`` will be the wrapped or proxied object. If\nyou don't want to use any of the standard three ways of defining\n``__subject__`` (i.e., as an object, callback, or lazy callback), you will need\nto subclass ``AbstractProxy`` or ``AbstractWrapper`` and provide your own\nway of defining ``__subject__``.\n\n\nMailing List\n------------\n\nPlease direct questions regarding this package to the PEAK mailing list; see\nhttp://www.eby-sarna.com/mailman/listinfo/PEAK/ for details.\n\n.. ex: set ft=rst :", "description_content_type": "", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/PEAK-Legacy/ProxyTypes#readme", "keywords": "", "license": "PSF or ZPL", "maintainer": "", "maintainer_email": "", "name": "ProxyTypes", "package_url": "https://pypi.org/project/ProxyTypes/", "platform": "", "project_url": "https://pypi.org/project/ProxyTypes/", "project_urls": { "Homepage": "https://github.com/PEAK-Legacy/ProxyTypes#readme" }, "release_url": "https://pypi.org/project/ProxyTypes/0.10.0/", "requires_dist": null, "requires_python": "", "summary": "General purpose proxy and wrapper types", "version": "0.10.0" }, "last_serial": 4037588, "releases": { "0.10.0": [ { "comment_text": "", "digests": { "md5": "849e48d1f09f78ba4d9637ade2cbb066", "sha256": "83e5bb089cccb23087c6e3c5d71dda9fca351b624ca5de709fddcf58dab84c84" }, "downloads": -1, "filename": "ProxyTypes-0.10.0-py2-none-any.whl", "has_sig": false, "md5_digest": "849e48d1f09f78ba4d9637ade2cbb066", "packagetype": "bdist_wheel", "python_version": "2.7", "requires_python": null, "size": 9658, "upload_time": "2018-07-06T20:24:22", "url": "https://files.pythonhosted.org/packages/d5/9c/e16097b84c103079db2ef8a3645b4816d7ef2313160e1e82eff9464b7e92/ProxyTypes-0.10.0-py2-none-any.whl" }, { "comment_text": "", "digests": { "md5": "e8d0ffcc92789cb61f4f59d39ffd444f", "sha256": "f3bf78debafe15d8ccb31a0c909f820408bb678532a8be8bf737ddd50c03f298" }, "downloads": -1, "filename": "ProxyTypes-0.10.0-py3-none-any.whl", "has_sig": false, "md5_digest": "e8d0ffcc92789cb61f4f59d39ffd444f", "packagetype": "bdist_wheel", "python_version": "3.6", "requires_python": null, "size": 9658, "upload_time": "2018-07-06T20:25:51", "url": "https://files.pythonhosted.org/packages/1e/d7/e682be643717d9400470f374cf53e1586a834913a79d96ea54d2323c711c/ProxyTypes-0.10.0-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "c0d878badb86f456dbe11a81dcd803d2", "sha256": "82cc41d5e7c913982b505241c7c87448ed26b6884a40c2889f2ee19d06339985" }, "downloads": -1, "filename": "ProxyTypes-0.10.0.tar.gz", "has_sig": false, "md5_digest": "c0d878badb86f456dbe11a81dcd803d2", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9243, "upload_time": "2018-07-06T20:24:21", "url": "https://files.pythonhosted.org/packages/fd/15/96033b47133203946d63aa2af276237f0f93d97de10422b9611a0e3c1c3b/ProxyTypes-0.10.0.tar.gz" } ], "0.9": [ { "comment_text": "", "digests": { "md5": "7d604937aad91bbbb74dd15fd54555cd", "sha256": "4be0b9e98b16deb003cdb67169d1d787613d3cf9a110de35d68d2b96b29fdc68" }, "downloads": -1, "filename": "ProxyTypes-0.9-py2.3.egg", "has_sig": false, "md5_digest": "7d604937aad91bbbb74dd15fd54555cd", "packagetype": "bdist_egg", "python_version": "2.3", "requires_python": null, "size": 27883, "upload_time": "2006-07-20T01:43:06", "url": "https://files.pythonhosted.org/packages/a0/20/dcaf9712c06ea9341f7c688a6a47a78fb3d9e4c3fe967532063a1a65ea41/ProxyTypes-0.9-py2.3.egg" }, { "comment_text": "", "digests": { "md5": "54105854c34938c295cef3e759bfc7b6", "sha256": "5c429ce64f03c4551d630ae8727d4e748c843f8f6c77ce87baaf792bf84367ac" }, "downloads": -1, "filename": "ProxyTypes-0.9-py2.4.egg", "has_sig": false, "md5_digest": "54105854c34938c295cef3e759bfc7b6", "packagetype": "bdist_egg", "python_version": "2.4", "requires_python": null, "size": 9051, "upload_time": "2006-07-20T01:43:14", "url": "https://files.pythonhosted.org/packages/11/10/226dfaae6e34f9dd4089068d2853887c60bc8c4659f4e61afc577315bf56/ProxyTypes-0.9-py2.4.egg" }, { "comment_text": "", "digests": { "md5": "73b65fbb9b3df07bfbe0742450acca23", "sha256": "20b35538c7addc2d5359ace9287d20c8c429621ec4a672704eac0db162c0ee0f" }, "downloads": -1, "filename": "ProxyTypes-0.9.zip", "has_sig": false, "md5_digest": "73b65fbb9b3df07bfbe0742450acca23", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 17975, "upload_time": "2006-07-20T01:45:09", "url": "https://files.pythonhosted.org/packages/72/bd/24f45710e7e6909b2129332363be2c981179ed2eda1166f18bc2baef98a1/ProxyTypes-0.9.zip" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "849e48d1f09f78ba4d9637ade2cbb066", "sha256": "83e5bb089cccb23087c6e3c5d71dda9fca351b624ca5de709fddcf58dab84c84" }, "downloads": -1, "filename": "ProxyTypes-0.10.0-py2-none-any.whl", "has_sig": false, "md5_digest": "849e48d1f09f78ba4d9637ade2cbb066", "packagetype": "bdist_wheel", "python_version": "2.7", "requires_python": null, "size": 9658, "upload_time": "2018-07-06T20:24:22", "url": "https://files.pythonhosted.org/packages/d5/9c/e16097b84c103079db2ef8a3645b4816d7ef2313160e1e82eff9464b7e92/ProxyTypes-0.10.0-py2-none-any.whl" }, { "comment_text": "", "digests": { "md5": "e8d0ffcc92789cb61f4f59d39ffd444f", "sha256": "f3bf78debafe15d8ccb31a0c909f820408bb678532a8be8bf737ddd50c03f298" }, "downloads": -1, "filename": "ProxyTypes-0.10.0-py3-none-any.whl", "has_sig": false, "md5_digest": "e8d0ffcc92789cb61f4f59d39ffd444f", "packagetype": "bdist_wheel", "python_version": "3.6", "requires_python": null, "size": 9658, "upload_time": "2018-07-06T20:25:51", "url": "https://files.pythonhosted.org/packages/1e/d7/e682be643717d9400470f374cf53e1586a834913a79d96ea54d2323c711c/ProxyTypes-0.10.0-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "c0d878badb86f456dbe11a81dcd803d2", "sha256": "82cc41d5e7c913982b505241c7c87448ed26b6884a40c2889f2ee19d06339985" }, "downloads": -1, "filename": "ProxyTypes-0.10.0.tar.gz", "has_sig": false, "md5_digest": "c0d878badb86f456dbe11a81dcd803d2", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9243, "upload_time": "2018-07-06T20:24:21", "url": "https://files.pythonhosted.org/packages/fd/15/96033b47133203946d63aa2af276237f0f93d97de10422b9611a0e3c1c3b/ProxyTypes-0.10.0.tar.gz" } ] }