{ "info": { "author": "Chris Spencer", "author_email": "chrisspen@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 6 - Mature", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6" ], "description": "Simple Proxy Types\n==================\n\n[![](https://img.shields.io/pypi/v/proxytypes3.svg)](https://pypi.python.org/pypi/proxytypes3) [![Build Status](https://img.shields.io/travis/chrisspen/proxytypes3.svg?branch=master)](https://travis-ci.org/chrisspen/proxytypes3)\n\nThe `proxytypes3` package 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\nThis package is a fork of the [peak.util.proxies](https://pypi.org/project/ProxyTypes/) module,\nextended to include support for Python 3.* as well as better testing and continuous integration.\n\nProxy Basics\n------------\n\nHere's a quick demo of the `ObjectProxy` type::\n\n >>> from proxytypes3 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 proxytypes3 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 proxytypes3 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 proxytypes3 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 proxytypes3 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 proxytypes3 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 proxytypes3 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 proxytypes3 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\nTests\n-----\n\nTo run unittests across multiple Python versions, first install the necessary Python versions. On Ubuntu, run:\n\n sudo add-apt-repository ppa:deadsnakes/ppa\n sudo apt-get update\n sudo apt-get install python-dev python3.4-minimal python3.4-dev python3.5-minimal python3.5-dev python3.6 python3.6-dev\n\nThen to run all [tests](http://tox.readthedocs.org/en/latest/):\n\n tox\n\nTo run tests for a specific environment (e.g. Python 2.7):\n \n tox -e py27\n\nTo run a specific test:\n \n export TESTNAME=.additional_tests; tox -e py34\n \n export TESTNAME=.TestObjectProxy.testNumbers; tox -e py36", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/chrisspen/ProxyTypes3", "keywords": "", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "proxytypes3", "package_url": "https://pypi.org/project/proxytypes3/", "platform": "", "project_url": "https://pypi.org/project/proxytypes3/", "project_urls": { "Homepage": "https://github.com/chrisspen/ProxyTypes3" }, "release_url": "https://pypi.org/project/proxytypes3/1.0.1/", "requires_dist": null, "requires_python": "", "summary": "General purpose proxy and wrapper types.", "version": "1.0.1" }, "last_serial": 4055144, "releases": { "1.0.0": [ { "comment_text": "", "digests": { "md5": "bfc6319142cf958ba98ec1c3e5c82775", "sha256": "3bb3ef0e504e4bfaf6e51b703ca89e46dd502c5e5922b6c8f07e875f36fae158" }, "downloads": -1, "filename": "proxytypes3-1.0.0.tar.gz", "has_sig": false, "md5_digest": "bfc6319142cf958ba98ec1c3e5c82775", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6939, "upload_time": "2018-07-11T19:01:02", "url": "https://files.pythonhosted.org/packages/d3/44/113af46c2454af9d046aee9cc5b2b53673d692828924a56e0773a9e5fea0/proxytypes3-1.0.0.tar.gz" } ], "1.0.1": [ { "comment_text": "", "digests": { "md5": "63cc66c331b13918e7d5073c97329761", "sha256": "c83083b02440c8934a9c05da6de299b8ae8386cdfe77ab99d490112048a2f3dc" }, "downloads": -1, "filename": "proxytypes3-1.0.1.tar.gz", "has_sig": false, "md5_digest": "63cc66c331b13918e7d5073c97329761", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6957, "upload_time": "2018-07-12T16:45:36", "url": "https://files.pythonhosted.org/packages/9e/47/480d0d0a78c2824e85db73fa317b89568bad5d4d73cbb016e5c68e2e4b87/proxytypes3-1.0.1.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "63cc66c331b13918e7d5073c97329761", "sha256": "c83083b02440c8934a9c05da6de299b8ae8386cdfe77ab99d490112048a2f3dc" }, "downloads": -1, "filename": "proxytypes3-1.0.1.tar.gz", "has_sig": false, "md5_digest": "63cc66c331b13918e7d5073c97329761", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6957, "upload_time": "2018-07-12T16:45:36", "url": "https://files.pythonhosted.org/packages/9e/47/480d0d0a78c2824e85db73fa317b89568bad5d4d73cbb016e5c68e2e4b87/proxytypes3-1.0.1.tar.gz" } ] }