{ "info": { "author": "Carl Sverre", "author_email": "carl@carlsverre.com", "bugtrack_url": null, "classifiers": [], "description": "===========\nWraptor\n===========\n\n.. image:: https://github.com/carlsverre/wraptor/raw/master/docs/images/raptor.jpg\n\nTesting\n=======\n\n.. image:: https://travis-ci.org/carlsverre/wraptor.png\n :target: https://travis-ci.org/carlsverre/wraptor\n\nRun tests by executing :code:`make test`.\n\nDecorators\n==========\n\nMemoize\n-------\nAdd a cache to a function such that multiple calls with the same args\nwill return cached results. Supports an optional cache timeout which\nwill flush items from the cache after a set interval for\nrecomputation.\n\n.. code:: python\n\n from wraptor.decorators import memoize\n\n @memoize()\n def foo(bar, baz):\n print(bar, baz)\n\n foo(1, 2)\n # prints (1, 2)\n foo(3, 4)\n # prints (3, 4)\n foo(1, 2)\n # no-op\n\nSupports timeouts!\n\n.. code:: python\n\n @memoize(timeout=.5)\n def foo(bar, baz):\n print(bar, baz)\n\n foo(1, 2)\n # prints (1, 2)\n foo(1, 2)\n # no-op\n\n import time\n time.sleep(2)\n\n foo(1, 2)\n # prints (1, 2)\n\nSupports attaching to an instance method!\n\n.. code:: python\n\n class foo(object):\n @memoize(instance_method=True)\n def bar(self, a, b):\n return random()\n\n f = foo()\n f2 = foo()\n\n # they don't share a cache!\n f.bar(1,2) != f2.bar(1,2)\n\nThrottle\n--------\nThrottle a function to firing at most 1 time per interval. The function\nis fired on the forward edge (meaning it will fire the first time you\ncall it).\n\n.. code:: python\n\n from wraptor.decorators import throttle\n import time\n\n @throttle(.5)\n def foo(bar, baz):\n print(bar, baz)\n\n foo(1, 2)\n # prints (1, 2)\n foo(3, 4)\n # no-op\n time.sleep(1)\n foo(5, 6)\n # prints (1, 2)\n \nSupports attaching to an instance method!\n\n.. code:: python\n\n arr = []\n\n class foo(object):\n @throttle(1, instance_method=True)\n def bar(self):\n arr.append(1)\n\n x = foo()\n x2 = foo()\n\n x.bar()\n x2.bar()\n \n # they don't share the same throttle!\n assert arr == [1, 1]\n\nBy default throttle passes through the return value of the wrapped\nfunction if it was called, or None if the def was not called. Use\n`return_throttle_result=True` to instead return a `bool`:\n\n.. code:: python\n\n @throttle(1, return_throttle_result=True)\n def foo():\n pass\n\n assert foo() is True, 'True means \"called\"'\n assert foo() is False, 'False means \"not called\" (throttled)'\n\n\nTimeout\n-------\nTimeout uses signal under the hood to allow you to add timeouts to any\nfunction. The only caveat is that `signal.alarm` can only be used in the\nmain thread of execution (so multi-threading programs can't use this\ndecorator in sub-threads).\n\nThe timeout value must be a positive integer.\n\n.. code:: python\n\n from wraptor.decorators import timeout, TimeoutException\n import time\n\n @timeout(1)\n def heavy_workload():\n # simulate heavy work\n time.sleep(10)\n\n try:\n heavy_workload()\n except TimeoutException:\n print('workload timed out')\n\nYou can also catch the timeout exception from inside the function:\n\n.. code:: python\n\n @timeout(1)\n def heavy_workload():\n try:\n # simulate heavy work\n time.sleep(10)\n except TimeoutException:\n print('workload timed out')\n\nException Catcher\n-----------------\n`exception_catcher` is a helpful method for dealing with threads that\nmay raise an exception. It is especially useful for testing.\n\n.. code:: python\n\n from wraptor.decorators import exception_catcher\n\n @exception_catcher\n def work():\n raise Exception()\n\n t = threading.Thread(target=work)\n t.start()\n t.join()\n\n try:\n work.check()\n except Exception as e:\n print e\n\nContext Managers\n================\n\nThrottle\n--------\nThrottle a with statement to executing its body at most 1 time per\ninterval. The body is fired on the forward edge (meaning it will\nfire the first time you call it).\n\n.. code:: python\n\n from wraptor.context import throttle\n import time\n\n throttler = throttle(seconds=3)\n\n def foo():\n with throttler:\n print 'bar'\n\n foo()\n # prints bar\n sleep(2)\n foo()\n # does nothing\n sleep(2)\n foo()\n # prints bar\n\nMaybe\n-----\nExecute a with block based on the results of a predicate.\n\n.. code:: python\n\n from wraptor.context import maybe\n\n def foo(cond):\n with maybe(lambda: cond == 5):\n print 'bar'\n\n foo(5)\n # prints bar\n foo(3)\n # does nothing\n\nTimer\n-----\nTime a block of code.\n\n.. code:: python\n\n from wraptor.context import timer\n\n def foo(cond):\n with timer('my slow method') as t:\n expensive_stuff()\n print t\n\n foo()\n # prints \"my slow method took 435.694 ms\"", "description_content_type": "", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "http://github.com/carlsverre/wraptor", "keywords": "", "license": "LICENSE.txt", "maintainer": "", "maintainer_email": "", "name": "Wraptor", "package_url": "https://pypi.org/project/Wraptor/", "platform": "", "project_url": "https://pypi.org/project/Wraptor/", "project_urls": { "Homepage": "http://github.com/carlsverre/wraptor" }, "release_url": "https://pypi.org/project/Wraptor/0.7.0/", "requires_dist": null, "requires_python": "", "summary": "Useful decorators and other utility functions.", "version": "0.7.0" }, "last_serial": 3836166, "releases": { "0.1.0": [], "0.2.0": [ { "comment_text": "", "digests": { "md5": "a54e212bf956a4a81c54e96cd11c43b7", "sha256": "df03959ae6f37c63a6a07eed077e7114d58bd8d9c369bb1bf5f5db7423dd674a" }, "downloads": -1, "filename": "Wraptor-0.2.0.tar.gz", "has_sig": false, "md5_digest": "a54e212bf956a4a81c54e96cd11c43b7", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4785, "upload_time": "2013-06-02T19:02:50", "url": "https://files.pythonhosted.org/packages/70/9b/e5edcdaa8924f6c778b910efdfe82ca03562eade82cdade031473f7fed98/Wraptor-0.2.0.tar.gz" } ], "0.3.0": [ { "comment_text": "", "digests": { "md5": "beea22e2ee22525c203ee926ddbdcf10", "sha256": "7580f771c9795816d4819794144d83656beae2575c13b0922d34f2734c618a04" }, "downloads": -1, "filename": "Wraptor-0.3.0.tar.gz", "has_sig": false, "md5_digest": "beea22e2ee22525c203ee926ddbdcf10", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6771, "upload_time": "2013-06-13T06:40:59", "url": "https://files.pythonhosted.org/packages/17/ab/dc30b9dd3b41cee15d4b6e8ea145a17f0caa4b5f04556383a8152b5626d2/Wraptor-0.3.0.tar.gz" } ], "0.3.1": [ { "comment_text": "", "digests": { "md5": "35671bac48668efc95330a305b467400", "sha256": "1b6a865b804f493b1c50409990053b42510282605ae88c3b38c3d36bf556d1c2" }, "downloads": -1, "filename": "Wraptor-0.3.1.tar.gz", "has_sig": false, "md5_digest": "35671bac48668efc95330a305b467400", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6828, "upload_time": "2013-06-20T22:14:19", "url": "https://files.pythonhosted.org/packages/bc/e3/aefee4ecd7caf12c14fccd055ee1419c2b906a9e8fe701b077e9785e3619/Wraptor-0.3.1.tar.gz" } ], "0.4.0": [ { "comment_text": "", "digests": { "md5": "ea39e78a2a93df2bb0c44ccc98fdccd1", "sha256": "93c7f6d39693085b6261b202a921c323764446cff8e8fba3a572e8ad053840dc" }, "downloads": -1, "filename": "Wraptor-0.4.0.tar.gz", "has_sig": false, "md5_digest": "ea39e78a2a93df2bb0c44ccc98fdccd1", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8101, "upload_time": "2013-09-06T22:14:42", "url": "https://files.pythonhosted.org/packages/d2/ef/6771bda026a26766f90d09b618fb69b6d834dff1b743077ec3f139869117/Wraptor-0.4.0.tar.gz" } ], "0.4.1": [ { "comment_text": "", "digests": { "md5": "8c98fdd93a467363c809b149801084af", "sha256": "c5e0986d7210de0bda7f35168ed71cc10a6ac943cac0f332b6528ad5059dfee7" }, "downloads": -1, "filename": "Wraptor-0.4.1.tar.gz", "has_sig": false, "md5_digest": "8c98fdd93a467363c809b149801084af", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8239, "upload_time": "2013-09-06T23:31:24", "url": "https://files.pythonhosted.org/packages/f1/32/2e89c857e4795d4d35cadf64ac4d11fbefa5b513d4d17671c64a74af2d4a/Wraptor-0.4.1.tar.gz" } ], "0.5.0": [ { "comment_text": "", "digests": { "md5": "d67caae5a75bfd880516a339b8162262", "sha256": "284e230194e0707335629de6f81932bcf1f0677d66e41a975925489b400386c2" }, "downloads": -1, "filename": "Wraptor-0.5.0.tar.gz", "has_sig": false, "md5_digest": "d67caae5a75bfd880516a339b8162262", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8696, "upload_time": "2013-10-10T00:37:54", "url": "https://files.pythonhosted.org/packages/94/77/51b7e070cd5cf8576a5cb786f01536fa5c915c821e89003e94ae17817c96/Wraptor-0.5.0.tar.gz" } ], "0.6.0": [ { "comment_text": "", "digests": { "md5": "c6f91d8e650046a9b00742c830640885", "sha256": "fc0b27cc81d3c7b81db0aa324eeca7dbb8cec3ca77f8521b576584520d33e58e" }, "downloads": -1, "filename": "Wraptor-0.6.0.tar.gz", "has_sig": false, "md5_digest": "c6f91d8e650046a9b00742c830640885", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9752, "upload_time": "2014-03-09T22:11:49", "url": "https://files.pythonhosted.org/packages/f1/ae/4b9a3491077d24ba1399ac529b635539a2a660e1bbbda56adbf67eddf322/Wraptor-0.6.0.tar.gz" } ], "0.7.0": [ { "comment_text": "", "digests": { "md5": "cf8a1f1215db239fbff46fa99391669b", "sha256": "d61182866a061fb29b7ec426db281cc9a15540766885136f35809f079d9c1dec" }, "downloads": -1, "filename": "Wraptor-0.7.0.tar.gz", "has_sig": false, "md5_digest": "cf8a1f1215db239fbff46fa99391669b", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5991, "upload_time": "2018-05-05T02:21:28", "url": "https://files.pythonhosted.org/packages/50/8d/e3605669df8541f68a028dfd81db7f5b9b4d43bed133d070929f829eafce/Wraptor-0.7.0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "cf8a1f1215db239fbff46fa99391669b", "sha256": "d61182866a061fb29b7ec426db281cc9a15540766885136f35809f079d9c1dec" }, "downloads": -1, "filename": "Wraptor-0.7.0.tar.gz", "has_sig": false, "md5_digest": "cf8a1f1215db239fbff46fa99391669b", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5991, "upload_time": "2018-05-05T02:21:28", "url": "https://files.pythonhosted.org/packages/50/8d/e3605669df8541f68a028dfd81db7f5b9b4d43bed133d070929f829eafce/Wraptor-0.7.0.tar.gz" } ] }