{ "info": { "author": "Benjamin Bruno Meier", "author_email": "benjamin.meier70@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 2 - Pre-Alpha", "License :: OSI Approved :: MIT License", "Programming Language :: Python", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7" ], "description": "\u2112azy-\u2112oad\n=========\n\n.. image:: https://img.shields.io/pypi/v/lazy_load.svg\n :target: https://pypi.python.org/pypi/lazy_load\n :alt: Latest PyPI version\n\n.. image:: https://travis-ci.org/kutoga/lazy-load.png\n :target: https://travis-ci.org/kutoga/lazy-load\n :alt: Latest Travis CI build status\n\nA minimalistic interface that allows lazy evaluation of expressions and function calls.\n\nNote: This small library is highly based on `python-lazy-object-proxy`.\n\nWhy using \u2112azy-\u2112oad? Lazy loading in general may make some software implementations much more efficient.\nEspecially if it is not known if some data has to be loaded or not. Often the resulting code is less efficient,\nbecause eager loading is used or the code is not elegant, because one has to program (somehow) lazy loading.\n\nAdvantages of this library are that lazy-loading may be used quite elegant and effective.\n\nExamples\n^^^^^^^^\n\nIn a loop it might happen that a special condition appears once or even more often. If this is the case,\nan expensive function `expensive_function` is called and on the resulting object an operation has\nto be done. If the expensive function had to called more than once, than the result object may be reused.\n\nPossible implementation:\n\n\n.. code:: python\n\n def expensive_function():\n print(\"function evaluation\")\n ...\n return result\n\n obj = None\n for x, y, p in get_coordinates():\n if test_for_something(x, y, p):\n if obj is None:\n obj = expensive_function()\n obj.do_something(x, y)\n\nGiven this library, it might be done like this:\n\n\n.. code:: python\n\n from lazy_load import lazy\n\n def expensive_function():\n print(\"function evaluation\")\n ...\n return result\n\n obj = lazy(expensive_function)\n for x, y, p in get_coordinates():\n if test_for_something(x, y, p):\n obj.do_something(x, y)\n\nThere are similar situations outside of loops. The implementation without `lazy-load` might look like this:\n\n\n.. code:: python\n\n def expensive_function():\n print(\"function evaluation\")\n ...\n return result\n\n obj = None\n def get_obj():\n global obj\n if obj is None:\n obj = expensive_function()\n return obj\n\n if condition_a:\n get_obj().xyz()\n if condition_b:\n do_something()\n if condition_c:\n get_obj().abc()\n\nThis code can be realized much easier with `lazy-load`. Not only is the code shorter, but it is also more readable:\n\n\n.. code:: python\n\n from lazy_load import lazy\n\n def expensive_function():\n print(\"function evaluation\")\n ...\n return result\n\n obj = lazy(expensive_function)\n\n if condition_a:\n obj.xyz()\n if condition_b:\n do_something()\n if condition_c:\n obj.abc()\n\nIt might be the case that the expensive function is used more often and always a lazy evaluation is done.\nIn this case, a decorator might be used to indicate that all function calls to this function shall be lazily\nevaluated. This makes it possible to normally use the function. The behaviour is still the same like in the first example:\n\n\n.. code:: python\n\n from lazy_load import lazy_func\n\n @lazy_func\n def expensive_function():\n print(\"function evaluation\")\n ...\n return result\n\n obj = expensive_function()\n for x, y, p in get_coordinates():\n if test_for_something(x, y, p):\n obj.do_something(x, y)\n\nA lazy evaluation of functions / methods calls might be done with the `@lazy_func` decorator of with the `lazy`-call. This was already\nshown, therefore the following examples show how to do a one-shot lazy evaluation of a function call:\n\n\n.. code:: python\n\n from lazy_load import lazy, lz\n\n def expensive_func(x, y):\n print(f\"function evaluation with arguments x={x}, y={y}\")\n ...\n return result\n\n # Possibility 1: Use `lazy` with a callable\n obj = lazy(lambda: expensive_func(a, b))\n\n # Possibility 2: If it doesn't matter if the argument expressions for the expensive-function are eager evaluated, the call may be simplified:\n obj = lazy(expensive_func, a, b)\n\n # Possibility 3: `lazy` has a short version / alias: `lz`\n obj = lz(expensive_func, a, b)\n\nPython allows it to pass functions around: This is often used for callbacks, but also for other use cases.\nAssuming an expensive function is passed to an object which calls this function and stores the result of\nthe function call in an attribute. Later it might happen that this attribute is used. Depending on the\nprogram flow it also might happen that this attribute is not used. With a lazily evaluated function the\nexpensive function call is only executed if the result is really used. The lazily evaluated version of\na function has the exact same signature as the original function.\n\nOne might now like to have the possibility to on-the-fly convert a callable to a lazily evaluated callable.\nThis might be done in the following way:\n\n\n.. code:: python\n\n from lazy_load import lazy_func, lf\n\n def expensive_func(x):\n print(f\"function evaluation with argument x={x}\")\n ...\n return result\n\n # Possibility 1: Use `lazy_func`.\n my_obj.do_something(f=lazy_func(expensive_func))\n\n # Possibility 2: Use `lf` which is an alias of `lazy_func`\n my_obj.do_something(f=lf(expensive_func))\n\n # Possibility 3: Use the `\u2112`-\"operator\"\n my_obj.do_something(f=\u2112[expensive_func])\n\nActually, I want to go deeper into the `\u2112`azy- or `\u2112`-\"operator\". This operator converts on-the-fly a function\nto a lazily evaluated function. Another example:\n\n\n.. code:: python\n\n from lazy_load import \u2112\n\n def test(name):\n print(f\"hey {name}\")\n return True\n\n res = test(\"peter\")\n # prints \"hey peter\"\n\n test_l = \u2112[test]\n res = test_l(\"hans\")\n # prints nothing\n\n if res:\n print(\"res is True\")\n # prints \"hey hans\\nres is True\"\n\nIt is also possible to convert multiple functions to lazily evaluated functions using `\u2112`:\n\n\n.. code:: python\n\n from lazy_load import \u2112\n\n def f1(x):\n print(f\"f1 {x}\")\n return True\n\n def f2(x):\n print(f\"f1 {x}\")\n return True\n\n f1_l, f2_l, f3_l = \u2112[f1, f2, lambda x: x == 1]\n # This is equal to:\n f1_l = \u2112[f1]\n f2_l = \u2112[f2]\n f3_l = \u2112[lambda x: x == 1]\n\nFinally, one might like to decorate a class in a way that all its public methods which have a return\nvalue are lazily evaluated. Public methods are all methods that have a name not starting with `_`.\nMethods with a return value are identificated by the given return type hint which must not be `None`.\nThis behaviour might be done with the `@lazy_class`-decorator (alias: `lc`):\n\n\n.. code:: python\n\n from lazy_load import lazy_class\n\n @lazy_class\n class MyClass:\n def __init__(self):\n # Method name starts with \"_\" => is not public; therefore it is eager evaluated\n pass\n\n def setX(x) -> None:\n # Method does not return a value => therefore it is eager evaluated\n ...\n\n def do():\n # Method does not hav a return value type hint => therefore it is eager evaluated\n ...\n\n def compute() -> int:\n # Method will always be lazily evaluated\n ...\n return result\n\nFinally, it is also possible to force the evaluation of a lazy loading object by using `force_eval` (alias `fe`).\nThis function can safely to used to non-lazy loading objects: It is then just equal to the identity function.\n\n\n.. code:: python\n\n from lazy_load import lazy, force_eval\n\n def f1(x):\n print(f\"f1 {x}\")\n return True\n\n lazy_obj = lazy(f1, 1)\n\n # The following expression prints \"f1 1\" and returns \"True\"\n force_eval(lazy_obj)\n\nThe `force_eval` function may also be applied to lazy-functions (which are created with `lazy_func(x)`, `@lazy_func`\nor with `\u2112`). This restores the original non-lazy / eager function. For non-lazy functions this call has no effect:\n\n\n.. code:: python\n\n from lazy_load import lazy_func, force_eval\n\n @lazy_func\n def f(x):\n print(\"hey\")\n return x**2\n\n # The following line prints nothing\n obj = f(2)\n\n f_eager = force_eval(f)\n\n # The following line prints \"hey\" and \"obj\" has immediatly the value \"4\"\n obj = f_eager(2)\n\n\nInstallation\n------------\n\n`pip install lazy-load`\n\nRequirements\n^^^^^^^^^^^^\n\nPython 3.6 or Python 3.7.\n\nLicence\n-------\n\nMIT\n\nAuthors\n-------\n\n`lazy_load` was written by `Benjamin Bruno Meier `_.\n\n\n", "description_content_type": "", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/kutoga/lazy-load", "keywords": "", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "lazy-load", "package_url": "https://pypi.org/project/lazy-load/", "platform": "", "project_url": "https://pypi.org/project/lazy-load/", "project_urls": { "Homepage": "https://github.com/kutoga/lazy-load" }, "release_url": "https://pypi.org/project/lazy-load/0.8.2/", "requires_dist": [ "lazy-object-proxy (>=1.3.1)" ], "requires_python": "", "summary": "\u2112azy-\u2112oad - A minimalistic interface that allows the lazy evaluation of expressions. Additional functions and wrappers allow it to easily use the lazy evaluation for functions and classes.", "version": "0.8.2" }, "last_serial": 4382852, "releases": { "0.8.1": [ { "comment_text": "", "digests": { "md5": "4834d8865af77714433f7e9e70c38f0b", "sha256": "2b66de72a5e7efaaeb683883634a898d24613b039dca0c5f6f92eba4d8b99405" }, "downloads": -1, "filename": "lazy_load-0.8.1-py3-none-any.whl", "has_sig": false, "md5_digest": "4834d8865af77714433f7e9e70c38f0b", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 6921, "upload_time": "2018-10-15T20:59:22", "url": "https://files.pythonhosted.org/packages/1b/fd/32b453a93511a088e328dacad377df45318bea98cca244deb7c21a423bcd/lazy_load-0.8.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "cab0815b19311307b71e7a0b50bb415c", "sha256": "8f5100c3531776c5a620e36151e70bc8ba54f03903a7ed6970eb649865e8559e" }, "downloads": -1, "filename": "lazy_load-0.8.1.tar.gz", "has_sig": false, "md5_digest": "cab0815b19311307b71e7a0b50bb415c", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6315, "upload_time": "2018-10-15T20:59:23", "url": "https://files.pythonhosted.org/packages/bf/b2/cb0493a05981160b21ac946b284335ea6f60cbffbf525abd8abb9063e476/lazy_load-0.8.1.tar.gz" } ], "0.8.2": [ { "comment_text": "", "digests": { "md5": "fa2dbceee2f55056f73c69f3fe66d130", "sha256": "d58239a8c9632a75686b2a7f9ba9736d81510cfcb50bd36458e23c767e75e799" }, "downloads": -1, "filename": "lazy_load-0.8.2-py3-none-any.whl", "has_sig": false, "md5_digest": "fa2dbceee2f55056f73c69f3fe66d130", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 7126, "upload_time": "2018-10-16T19:04:47", "url": "https://files.pythonhosted.org/packages/09/46/7ee7b3e969f593cfa78e4ef89556fb99b7709f85556d4c092757f31e049f/lazy_load-0.8.2-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "53b8a26034bb99ad97ed7f9f241f9764", "sha256": "c2592d899748937edf84dbe107354dc21bef5c85b9e41b13f991a74e026655f9" }, "downloads": -1, "filename": "lazy_load-0.8.2.tar.gz", "has_sig": false, "md5_digest": "53b8a26034bb99ad97ed7f9f241f9764", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6526, "upload_time": "2018-10-16T19:04:48", "url": "https://files.pythonhosted.org/packages/c4/35/05688b9d1c74b3b49b91b315a2fc6c0aa9a13203f8ecefd521a36fe04b69/lazy_load-0.8.2.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "fa2dbceee2f55056f73c69f3fe66d130", "sha256": "d58239a8c9632a75686b2a7f9ba9736d81510cfcb50bd36458e23c767e75e799" }, "downloads": -1, "filename": "lazy_load-0.8.2-py3-none-any.whl", "has_sig": false, "md5_digest": "fa2dbceee2f55056f73c69f3fe66d130", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 7126, "upload_time": "2018-10-16T19:04:47", "url": "https://files.pythonhosted.org/packages/09/46/7ee7b3e969f593cfa78e4ef89556fb99b7709f85556d4c092757f31e049f/lazy_load-0.8.2-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "53b8a26034bb99ad97ed7f9f241f9764", "sha256": "c2592d899748937edf84dbe107354dc21bef5c85b9e41b13f991a74e026655f9" }, "downloads": -1, "filename": "lazy_load-0.8.2.tar.gz", "has_sig": false, "md5_digest": "53b8a26034bb99ad97ed7f9f241f9764", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6526, "upload_time": "2018-10-16T19:04:48", "url": "https://files.pythonhosted.org/packages/c4/35/05688b9d1c74b3b49b91b315a2fc6c0aa9a13203f8ecefd521a36fe04b69/lazy_load-0.8.2.tar.gz" } ] }