{ "info": { "author": "Andrea Ratto", "author_email": "andrearatto_liste@yahoo.it", "bugtrack_url": null, "classifiers": [ "Development Status :: 5 - Production/Stable", "Intended Audience :: Developers", "License :: OSI Approved :: Python Software Foundation License", "License :: OSI Approved :: Zope Public License", "Operating System :: OS Independent", "Programming Language :: Python", "Topic :: Software Development :: Libraries" ], "description": "Simple Proxy Types\n==================\n\nThe ``objproxies`` 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\nDevelopment status\n******************\n\nThis is Python 3 port of `ProxyTypes\n`_ wrote by Phillip J. Eby as\npart of `PEAK `_ for Python 2.\n\nThe namespace was changed from ``peak.util.proxies`` to ``objproxies``. Other\nthan that it should be a compatible replacement.\n\nSo far the following was accomplished:\n\n* Streamlined files and setup\n* Ported unittests and doctests\n* Cleaned up syntax\n\nv1.0 TODO\n+++++++++\n\n* Turn the module in a package, separate functionalities in different modules\n* Simplify code wherever possible\n* Get positive feedback from a couple of users\n\nContributions and bug reports are welcome.\n\nTesting\n+++++++\n\nWhen nose is available all tests can be run using:\n\n.. code:: bash\n\n nosetests3 --with-doctest --doctest-extension=rst .\n\nOtherwise standard python will suffice:\n\n.. code:: bash\n\n python -m unittest objproxies_tests.py\n python -m doctest README.rst\n\nProxy Basics\n************\n\nHere's a quick demo of the ``ObjectProxy`` type::\n\n >>> from objproxies import ObjectProxy\n >>> p = ObjectProxy(42)\n\n >>> p\n 42\n\n >>> isinstance(p, int)\n True\n\n >>> p.__class__\n \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\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\n``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\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 that\ngets incremented each time it's used, from zero to three::\n\n >>> from objproxies import CallbackProxy\n\n >>> callback = iter(range(4)).__next__\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 objproxies import get_callback, set_callback\n >>> set_callback(counter, lambda: 42)\n\n >>> counter\n 42\n\n >>> type(get_callback(counter))\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 objproxies 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 objproxies import get_cache, set_cache\n >>> get_cache(lazy)\n 42\n >>> set_cache(lazy, 99)\n >>> lazy\n 99\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 objproxies 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 arbitrary 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``LazyWrapper`` objects are particularly useful when working with expensive\nresources, like connections or web browsers, to avoid their creation unless\nabsolutely needed. \n\nHowever resources usually must be released after use by calling a \"``close``\"\nmethod of some sort. In this case the lazy creation could be triggered just\nwhen the object is not needed anymore, by the call to ``close`` itself. For\nthis reason when extending ``LazyWrapper`` these methods can be overridden with\na ``@lazymethod`` replacement::\n\n >>> from objproxies import LazyWrapper, lazymethod\n\n >>> class LazyCloseable(LazyWrapper):\n ... @lazymethod\n ... def tell(self):\n ... return 0\n ... @lazymethod\n ... def close(self):\n ... print(\"bye\")\n ... @lazymethod\n ... def __bool__(self):\n ... return False\n\n >>> import tempfile\n\n >>> def openf():\n ... print(\"called\")\n ... return tempfile.TemporaryFile('w')\n\n >>> lazyfile = LazyCloseable(openf)\n >>> lazyfile.tell()\n 0\n >>> lazyfile.close()\n bye\n >>> bool(lazyfile)\n False\n\n >>> lazyfile = LazyCloseable(openf)\n >>> lazyfile.write('wake up')\n called\n 7\n >>> lazyfile.tell()\n 7\n >>> lazyfile.close() # close for real\n >>> bool(lazyfile)\n True\n\nAdvanced: 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 objproxies 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 objproxies 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 proxed 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 way\nof defining ``__subject__``.", "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/soulrebel/objproxies", "keywords": "proxy pattern lazy wrapper", "license": "PSF or ZPL", "maintainer": null, "maintainer_email": null, "name": "objproxies", "package_url": "https://pypi.org/project/objproxies/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/objproxies/", "project_urls": { "Download": "UNKNOWN", "Homepage": "http://github.com/soulrebel/objproxies" }, "release_url": "https://pypi.org/project/objproxies/0.9.4/", "requires_dist": null, "requires_python": null, "summary": "General purpose proxy and wrapper types", "version": "0.9.4" }, "last_serial": 1647611, "releases": { "0.9.1": [ { "comment_text": "built for Linux-3.14.6-1-ARCH-x86_64-with-glibc2.3.4", "digests": { "md5": "6aecb56bc9a7f8243d238bf87814f352", "sha256": "b8c4bb9bef3cf5a3ebefe7db9ddccdc5d3812c862b386cbea84aef93d2a44baa" }, "downloads": -1, "filename": "objproxies-0.9.1.linux-x86_64.tar.gz", "has_sig": false, "md5_digest": "6aecb56bc9a7f8243d238bf87814f352", "packagetype": "bdist_dumb", "python_version": "any", "requires_python": null, "size": 8042, "upload_time": "2014-06-14T09:01:18", "url": "https://files.pythonhosted.org/packages/cc/46/a1fd7a8815c12719cd586302fb689640f67826e49fc95b5681f45e788f3a/objproxies-0.9.1.linux-x86_64.tar.gz" }, { "comment_text": "", "digests": { "md5": "9633361b26f0b35a6784017941d03da6", "sha256": "3bb7c2807a21c3d635a8a1846587a2b6988441aa6e419a0e107f53f97e497af1" }, "downloads": -1, "filename": "objproxies-0.9.1.tar.gz", "has_sig": false, "md5_digest": "9633361b26f0b35a6784017941d03da6", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6208, "upload_time": "2014-06-14T09:01:15", "url": "https://files.pythonhosted.org/packages/4f/bf/cbb285e3522d336e84c79bb87a718b74d36565c47d1033a377b9ad9136f4/objproxies-0.9.1.tar.gz" } ], "0.9.2": [ { "comment_text": "built for Linux-3.16.2-1-ARCH-x86_64-with-glibc2.3.4", "digests": { "md5": "bf9d75969d2cfa9c5abd279d5db5c0ec", "sha256": "a7322abfaf5d67ecc593784586602c3f566dfbab3fb05787b48e1c1e69d0af1e" }, "downloads": -1, "filename": "objproxies-0.9.2.linux-x86_64.tar.gz", "has_sig": false, "md5_digest": "bf9d75969d2cfa9c5abd279d5db5c0ec", "packagetype": "bdist_dumb", "python_version": "any", "requires_python": null, "size": 8533, "upload_time": "2014-09-20T17:17:06", "url": "https://files.pythonhosted.org/packages/97/1f/2d46f1ff1509f7b0c47fc0c5b6f13ce3bbb303685fe7185b737327aa0b0b/objproxies-0.9.2.linux-x86_64.tar.gz" }, { "comment_text": "", "digests": { "md5": "28413e1125f4dedefe46fd15370289d9", "sha256": "4ced7a6923464945610c2f5ecabe3a1c8c3b388b76a4d552424b3e656f96bdb5" }, "downloads": -1, "filename": "objproxies-0.9.2.tar.gz", "has_sig": false, "md5_digest": "28413e1125f4dedefe46fd15370289d9", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6874, "upload_time": "2014-09-20T17:17:03", "url": "https://files.pythonhosted.org/packages/20/b0/2917ec12d533a65fc3f685b8c16a339b57d200e77e1a7021fff744dc0ba4/objproxies-0.9.2.tar.gz" } ], "0.9.3": [ { "comment_text": "built for Linux-3.16.2-1-ARCH-x86_64-with-glibc2.3.4", "digests": { "md5": "546e625efa93180886539a50a36d0772", "sha256": "375ba5c39bf6586c633ae76cffddf1b612731a0a7fef5a0eeefb6c4d717591c1" }, "downloads": -1, "filename": "objproxies-0.9.3.linux-x86_64.tar.gz", "has_sig": false, "md5_digest": "546e625efa93180886539a50a36d0772", "packagetype": "bdist_dumb", "python_version": "any", "requires_python": null, "size": 8606, "upload_time": "2014-09-21T01:41:02", "url": "https://files.pythonhosted.org/packages/3f/91/ffb80c42a42f817684a9c908a80415ca00bf2e5b1866df2ec66afe960333/objproxies-0.9.3.linux-x86_64.tar.gz" }, { "comment_text": "", "digests": { "md5": "3f6151bae18e2549ea0264e151b057e0", "sha256": "b7c3d942f549c2f7e9e3f7471e75e99fcf145b63ebd6b87d7d166319bde5bd3c" }, "downloads": -1, "filename": "objproxies-0.9.3.tar.gz", "has_sig": false, "md5_digest": "3f6151bae18e2549ea0264e151b057e0", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7005, "upload_time": "2014-09-21T01:40:59", "url": "https://files.pythonhosted.org/packages/5b/60/507d704c8828412a94b106009ce0dcbdbd34353b455c9d37f3e8eeb71bfa/objproxies-0.9.3.tar.gz" } ], "0.9.4": [ { "comment_text": "", "digests": { "md5": "cc6108a5a0b74fd89a90c87ee58849d3", "sha256": "6d68281b3b44dbda51ee11e460b50e9d0025ea68e16e3fb192fcf9250f229426" }, "downloads": -1, "filename": "objproxies-0.9.4.tar.gz", "has_sig": false, "md5_digest": "cc6108a5a0b74fd89a90c87ee58849d3", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7186, "upload_time": "2015-07-24T05:54:46", "url": "https://files.pythonhosted.org/packages/1d/6b/0434d100d411760d4d634e21dc256e259af720887b0c856c5a9b2c97ea30/objproxies-0.9.4.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "cc6108a5a0b74fd89a90c87ee58849d3", "sha256": "6d68281b3b44dbda51ee11e460b50e9d0025ea68e16e3fb192fcf9250f229426" }, "downloads": -1, "filename": "objproxies-0.9.4.tar.gz", "has_sig": false, "md5_digest": "cc6108a5a0b74fd89a90c87ee58849d3", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7186, "upload_time": "2015-07-24T05:54:46", "url": "https://files.pythonhosted.org/packages/1d/6b/0434d100d411760d4d634e21dc256e259af720887b0c856c5a9b2c97ea30/objproxies-0.9.4.tar.gz" } ] }