{ "info": { "author": "Jack Maney", "author_email": "jackmaney@gmail.com", "bugtrack_url": null, "classifiers": [], "description": "lazy_property - A package for making properties lazy\n====================================================\n\n`Properties `_ are a very useful feature of Python, effectively allowing an attribute to masquerade as a method (with no arguments other than ``self``). However, sometimes we want to store the results of an expensive computation in an attribute. The straightforward way to do it would be something like the following:\n\n::\n\n import time\n\n def do_some_big_calculation():\n \"\"\"Simulating some ginormous calculation going on somewhere...\"\"\"\n print(\"{}: Calculations started...\".format(time.time))\n time.sleep(5)\n print(\"{}: Calculations complete!\".format(time.time))\n return 42\n\n class SomeClass(object):\n\n def __init__(self):\n self.calculation_value = do_some_big_calculation()\n\nAnd that certainly works. However, this means that whenever you instantiate an object from ``SomeClass``, you'll perform this calculation each time. That could be problematic...\n\nSo, what we really want is for this calculation to only be done when it's needed. In other words, what we want is for this attribute to be **lazy**.\n\nTo do that, you can create a property, and a \"private\" attribute, named ``_calculation_value``, used to cache the result, like so:\n\n::\n\n class SomeOtherClass(object):\n\n @property\n def calculation_value(self):\n\n if not hasattr(self, \"_calculation_value\"):\n self._calculation_value = do_some_big_calculation()\n\n return self._calculation_value\n\nThis package essentially reduces this down to a decorator:\n\n::\n\n import lazy_property\n\n class YetAnotherClass(object):\n\n @lazy_property.LazyProperty\n def calculation_value(self):\n\n return do_some_big_calculation()\n\nAnd when called, the \"calculation\" is only done once:\n\n::\n\n In [5]:yac = YetAnotherClass()\n\n In [6]: yac.calculation_value\n 1440798443.228256: Calculations started...\n 1440798448.229179: Calculations complete!\n Out[6]: 42\n\n In [7]: yac.calculation_value\n Out[7]: 42\n\nNote, however, that this property is not writable:\n\n::\n\n In [8]: yac.calculation_value = \"something_else\"\n ---------------------------------------------------------------------------\n AttributeError Traceback (most recent call last)\n in ()\n ----> 1 yac.calculation_value = \"something_else\"\n\n AttributeError: can't set attribute\n\nThat's what the ``LazyWritableProperty`` is for.\n\n::\n\n class SomethingElse(object):\n\n @lazy_property.LazyWritableProperty\n def overwritable_calculation_value(self):\n\n return do_some_big_calculation()\n\n::\n\n In [9]: se = SomethingElse()\n\n In [10]: se.overwritable_calculation_value\n 1440798779.27305: Calculations started...\n 1440798784.274711: Calculations complete!\n Out[10]: 42\n\n In [11]: se.overwritable_calculation_value\n Out[11]: 42\n\n In [12]: se.overwritable_calculation_value = \"foo\"\n\n In [13]: se.overwritable_calculation_value\n Out[13]: 'foo'\n\nInstallation\n------------\n\nIt's up on PyPI:\n\n::\n\n pip install lazy-property\n\nOr, to do it the hard way, clone this repo, enter the directory into which you cloned the repo, and do a\n\n::\n\n python setup.py install\n\n\nWait...isn't this a solved problem?\n-----------------------------------\n\nWell, yes, but I couldn't find a lazy attribute implementation that clearly implemented laziness in the (fairly simple) way discussed above.", "description_content_type": null, "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/jackmaney/lazy-property.git", "keywords": "", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "lazy-property", "package_url": "https://pypi.org/project/lazy-property/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/lazy-property/", "project_urls": { "Homepage": "https://github.com/jackmaney/lazy-property.git" }, "release_url": "https://pypi.org/project/lazy-property/0.0.1/", "requires_dist": null, "requires_python": "", "summary": "Makes properties lazy (ie evaluated only when called)", "version": "0.0.1" }, "last_serial": 2058799, "releases": { "0.0.0": [ { "comment_text": "", "digests": { "md5": "9bc7a1ee7310e3622dfc37f24735a513", "sha256": "75b10b4961908535ee0f511c674f08a9adb2eaeaa5b95ef2a1fc15f3234daf4b" }, "downloads": -1, "filename": "lazy-property-0.0.0.tar.gz", "has_sig": false, "md5_digest": "9bc7a1ee7310e3622dfc37f24735a513", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3204, "upload_time": "2015-08-28T22:01:20", "url": "https://files.pythonhosted.org/packages/e5/25/9b6d70eb0e9836fdf4900129c3affc87438b8ac7d21055bdfe6791de522c/lazy-property-0.0.0.tar.gz" } ], "0.0.1": [ { "comment_text": "", "digests": { "md5": "b82df387778069ddf108c2e9e0db86d1", "sha256": "907b3a9e653771f4d0bcafd0fbfcdac8aaade85b31ed7eccd1cbfe59c59b149f" }, "downloads": -1, "filename": "lazy_property-0.0.1-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "b82df387778069ddf108c2e9e0db86d1", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 2445, "upload_time": "2016-04-11T21:35:40", "url": "https://files.pythonhosted.org/packages/4f/87/871a1522823d2bba3e8fd8cdea0f5eb7fa7596889f1ed73167d895127046/lazy_property-0.0.1-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "c77b36ddcdc4f5cca622da4d2a8caba0", "sha256": "3a1cd36949b323468731ba569e169415a609d5904d53fffe982055fbe67b1180" }, "downloads": -1, "filename": "lazy-property-0.0.1.tar.gz", "has_sig": false, "md5_digest": "c77b36ddcdc4f5cca622da4d2a8caba0", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3250, "upload_time": "2016-04-11T21:35:46", "url": "https://files.pythonhosted.org/packages/1c/97/55bf318faf296b254a20e80657b439b019fd809d4aabc1e55d7788f75401/lazy-property-0.0.1.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "b82df387778069ddf108c2e9e0db86d1", "sha256": "907b3a9e653771f4d0bcafd0fbfcdac8aaade85b31ed7eccd1cbfe59c59b149f" }, "downloads": -1, "filename": "lazy_property-0.0.1-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "b82df387778069ddf108c2e9e0db86d1", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 2445, "upload_time": "2016-04-11T21:35:40", "url": "https://files.pythonhosted.org/packages/4f/87/871a1522823d2bba3e8fd8cdea0f5eb7fa7596889f1ed73167d895127046/lazy_property-0.0.1-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "c77b36ddcdc4f5cca622da4d2a8caba0", "sha256": "3a1cd36949b323468731ba569e169415a609d5904d53fffe982055fbe67b1180" }, "downloads": -1, "filename": "lazy-property-0.0.1.tar.gz", "has_sig": false, "md5_digest": "c77b36ddcdc4f5cca622da4d2a8caba0", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3250, "upload_time": "2016-04-11T21:35:46", "url": "https://files.pythonhosted.org/packages/1c/97/55bf318faf296b254a20e80657b439b019fd809d4aabc1e55d7788f75401/lazy-property-0.0.1.tar.gz" } ] }