{ "info": { "author": "Donal Mee", "author_email": "mee.donal@gmail.com", "bugtrack_url": null, "classifiers": [ "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python :: 2", "Programming Language :: Python :: 3" ], "description": "[![Build Status](https://travis-ci.org/ddmee/polling2.svg?branch=master)](https://travis-ci.org/ddmee/polling2)\r\n[![PyPI](https://img.shields.io/pypi/dm/polling2.svg)]()\r\n[![PyPI](https://img.shields.io/pypi/v/polling2.svg)]()\r\n[![HitCount](http://hits.dwyl.io/ddmee/polling2.svg)](http://hits.dwyl.io/ddmee/polling2)\r\n\r\npolling2\r\n=============\r\n\r\n_Never write another polling function again!_\r\n\r\nPolling2 is a powerful python utility used to wait for a function to return a certain expected condition.\r\n\r\nSome possible uses cases include:\r\n\r\n- Wait for API response to return with code 200\r\n- Wait for a file to exist (or not exist)\r\n- Wait for a thread lock on a resource to expire\r\n\r\nPolling2 is handy for getting rid of all that duplicated polling-code. Often, applications require retrying until the correct response is returned. Why re-implement the ability to poll again and again? Use Polling2!\r\n\r\nPolling2 is a fork of the original [polling](https://github.com/justiniso/polling). It was forked when the original maintainer failed to respond to issues or PRs. \r\n\r\nPolling2 is ++under active development++. Would you like to see a particular feature? Ask and thou shall recieve.\r\n\r\n# Installation\r\n\r\n```\r\npip install polling2\r\n```\r\n\r\n# Development installation\r\n\r\n```shell\r\n# install lib, but use system links from the repo into sitepackages.\r\npython setup.py develop\r\n# install test dependenices.\r\npython setup.py test\r\n# run the tests\r\npytest tests\r\n```\r\n\r\n# Examples\r\n\r\n### Example: Poll every minute until a url returns 200 status code\r\n\r\n```python\r\nimport requests\r\npolling2.poll(\r\n lambda: requests.get('http://google.com').status_code == 200,\r\n step=60,\r\n poll_forever=True)\r\n```\r\n\r\nIf you are creating a new cloud provider instance (e.g. waiting for an EC2 instance to come online), you can continue to poll despite getting ConnectionErrors:\r\n\r\n```python\r\nimport requests\r\npolling2.poll(\r\n lambda: requests.get('your.instance.ip').status_code == 200,\r\n step=60,\r\n ignore_exceptions=(requests.exceptions.ConnectionError,),\r\n poll_forever=True)\r\n```\r\n\r\n### Example: Poll for a file to exist\r\n\r\n```python\r\n# This call will wait until the file exists, checking every 0.1 seconds and stopping after 3 seconds have elapsed\r\nfile_handle = polling2.poll(\r\n lambda: open('/tmp/myfile.txt'),\r\n ignore_exceptions=(IOError,),\r\n timeout=3,\r\n step=0.1)\r\n\r\n# Polling will return the value of your polling function, so you can now interact with it\r\nfile_handle.close()\r\n```\r\n\r\n### Example: Polling for Selenium WebDriver elements\r\n\r\n```python\r\nfrom selenium import webdriver\r\ndriver = webdriver.Firefox()\r\n\r\ndriver.get('http://google.com')\r\nsearch_box = polling2.poll(\r\n lambda: driver.find_element_by_id('search'),\r\n step=0.5,\r\n timeout=7)\r\n\r\nsearch_box.send_keys('python polling')\r\n```\r\n\r\n### Example: Using the polling timeout exception\r\n\r\n```python\r\n# An exception will be raised by the polling function on timeout (or the maximum number of calls is exceeded).\r\n# This exception will have a 'values' attribute. This is a queue with all values that did not meet the condition.\r\n# You can access them in the except block.\r\n\r\nimport random\r\ntry:\r\n polling2.poll(lambda: random.choice([0, (), False]), step=0.5, timeout=1)\r\nexcept polling2.TimeoutException, te:\r\n while not te.values.empty():\r\n # Print all of the values that did not meet the exception\r\n print te.values.get()\r\n```\r\n\r\n### Example: Using a custom checker\r\n\r\n```python\r\n# is_truthy() is the default checker for the parameter check_success. But, it's easy to create a custom\r\n# checker function, that tests whether the value returned by the target is the expected value.\r\n\r\n# Here the target is going to return None, which the custom checker, created by is_value(None)\r\n# will return True for.\r\npolling2.poll(target=lambda: None, step=0.1, max_tries=1, check_success=polling2.is_value(None))\r\n# Or another example, where we can test that False is returned by the target.\r\npolling2.poll(target=lambda: False, step=0.1, max_tries=1, check_success=polling2.is_value(False))\r\n```\r\n\r\n### Example: Using a custom condition callback function\r\n\r\n```python\r\nimport requests\r\n\r\ndef is_correct_response(response):\r\n \"\"\"Check that the response returned 'success'\"\"\"\r\n return response == 'success'\r\n\r\npolling2.poll(\r\n lambda: requests.put('http://mysite.com/api/user', data={'username': 'Jill'},\r\n check_success=is_correct_response,\r\n step=1,\r\n timeout=10)\r\n```\r\n\r\n### Example: Logging the return values from the target function.\r\n\r\n```python\r\nimport logging\r\nimport requests\r\n\r\ndef is_correct_response(response):\r\n \"\"\"Check that the response returned 'success'\"\"\"\r\n return response == 'success'\r\n\r\npolling2.poll(\r\n lambda: requests.put('http://mysite.com/api/user', data={'username': 'Jill'},\r\n check_success=is_correct_response,\r\n step=1,\r\n timeout=10,\r\n log=logging.DEBUG)\r\n```\r\n\r\nThis will log the string representation of response object to python's logging module at the debug level.\r\nA message like this will be sent to the log for each return value. You can change the level by providing\r\na different value to the log parameter.\r\n\r\n```text\r\npoll() calls check_success()\r\n```\r\n\r\nThere is also an option to log the exceptions that are caught by ignore_exceptions. Note, the full-exception traceback\r\nwill not be printed in the logs. Instead, the error and it's message (using %r formatting) will appear. In the following\r\ncode snippet, the ValueError raised by the function `raises_error()` will be sent to the logger at the 'warning' level.\r\n\r\n```python\r\nimport polling2\r\nimport logging\r\nimport mock\r\n\r\n# basicConfig should sent warning level messages to the stdout.\r\nlogging.basicConfig()\r\n\r\n# Create a function that raises a ValueError, then a RuntimeError.\r\nraises_error = mock.Mock(side_effect=[ValueError('a message'), RuntimeError])\r\n\r\ntry:\r\n polling2.poll(\r\n target=raises_error,\r\n step=0.1,\r\n max_tries=3,\r\n ignore_exceptions=(ValueError), # Only ignore the ValueError.\r\n log_error=logging.WARNING # Ignored errors should be passed to the logger at warning level.\r\n )\r\nexcept RuntimeError as _e:\r\n print \"Un-ignored %r\" % _e``\r\n```\r\n\r\n# Future extensions\r\n\r\n- Add poll_killer(). Specify a hard timeout so that if the function being polled blocks and doesn't return, poll_killer() will raise a timeout.\r\n - Add an option to do via multiprocessing.\r\n - Add an option to do via threading - probably the default option.\r\n- Add poll_chain(). Have reason to poll a bunch of functions in a row? poll_chain() allows you to chain a bunch of polling functions together.\r\n- Allow step to be specificed as 0, so that we can poll continously. (Perhaps it's best to write a poll_continous() method.)\r\n\r\n# Release notes\r\n\r\n## 0.4.4\r\n- Add is_value() function. A function that allows a user to easily build a custom checker, like is_truthy(), but for any value.\r\n\r\n## 0.4.3\r\n- Add log_error parameter to the poll signature. Enables logging of ignored exceptions.\r\n\r\n## 0.4.2\r\n- Add log_value() decorator and log parameter to poll signature. Enables logging of return_values.\r\n\r\n## 0.4.0\r\n- Fixed polling function from waiting another sleep whenever the max_tries value has reached zero.\r\n- Remove test-only dependencies from requirements to install the package.\r\n- No longer testing on python 2.6. Add support for travis testing on python 3.6 and pypy 3.5.\r\n- Creation of polling2, forked from polling as previous maintainer seems to be ignoring issues and pull-requests.\r\n- Remove ```*a, **k``` from poll signature. This allows Type errors to be raised if caller spells arguments into correctly, making bugs easier to find.\r\n\r\n## 0.3.0\r\n\r\n- Support Python 3.4+\r\n\r\n## 0.2.0\r\n\r\n- Allow users to access a \"last\" attribute on the exceptions. This should hold the last evaluated value, which is the more common use case than getting the first value. \r\n- Fix a bug that actually ran 1 more time than value specified by max_tries\r\n\r\n## 0.1.0\r\n\r\n- First version\r\n\r\n# Contributors\r\n- Justin Iso (original creator)\r\n- Donal Mee\r\n\r\n\r\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "http://github.com/ddmee/polling2", "keywords": "", "license": "", "maintainer": "", "maintainer_email": "", "name": "polling2", "package_url": "https://pypi.org/project/polling2/", "platform": "", "project_url": "https://pypi.org/project/polling2/", "project_urls": { "Homepage": "http://github.com/ddmee/polling2" }, "release_url": "https://pypi.org/project/polling2/0.4.4/", "requires_dist": null, "requires_python": "", "summary": "Updated polling utility with many configurable options", "version": "0.4.4" }, "last_serial": 5677257, "releases": { "0.4.0": [ { "comment_text": "", "digests": { "md5": "ec384afa83b34a4bcdcaea6226809b1f", "sha256": "1b12e84406d42f631854c477e69856ee007c8fb87476ad15d7360d9dd57632c9" }, "downloads": -1, "filename": "polling2-0.4.0-py2-none-any.whl", "has_sig": false, "md5_digest": "ec384afa83b34a4bcdcaea6226809b1f", "packagetype": "bdist_wheel", "python_version": "py2", "requires_python": null, "size": 6025, "upload_time": "2019-06-04T17:41:56", "url": "https://files.pythonhosted.org/packages/c8/57/e876ddf1f98170c7d36ceddc14d1447293024efb72d6537ec83770c61865/polling2-0.4.0-py2-none-any.whl" }, { "comment_text": "", "digests": { "md5": "eb2ef7c6d1b00a7f89b35161b78b6c5a", "sha256": "04e31c838d038638b403965c5f7e74de95387fd8701fd9665dfcbb70fe7f3266" }, "downloads": -1, "filename": "polling2-0.4.0.tar.gz", "has_sig": false, "md5_digest": "eb2ef7c6d1b00a7f89b35161b78b6c5a", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6192, "upload_time": "2019-06-04T17:41:58", "url": "https://files.pythonhosted.org/packages/c5/02/47b89495000de32a627ceb5c4ada1bd097a0d1f2b1a0afe510a777d2b1e1/polling2-0.4.0.tar.gz" } ], "0.4.1": [ { "comment_text": "", "digests": { "md5": "836f3fe228df575268abf9d9a1d3095f", "sha256": "ff1ee8484cda9f7f91a7ad80b83094cffd87c706b94009d06b54ee5976c1e123" }, "downloads": -1, "filename": "polling2-0.4.1-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "836f3fe228df575268abf9d9a1d3095f", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 6110, "upload_time": "2019-06-04T18:20:36", "url": "https://files.pythonhosted.org/packages/e0/b0/8f2afc5e40b64c12dc9f92b5668811409c18ca102c7263b504388f2e0532/polling2-0.4.1-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "c7c669544dcb9aadc39717931294bddc", "sha256": "bf064bef90dcd3a9e6ff17e8a0a27bbd6aa7ee464d3e9df93ae3aac397efff55" }, "downloads": -1, "filename": "polling2-0.4.1.tar.gz", "has_sig": false, "md5_digest": "c7c669544dcb9aadc39717931294bddc", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6271, "upload_time": "2019-06-04T18:20:38", "url": "https://files.pythonhosted.org/packages/a6/90/22d34c9231b6d63445ad1c413f470a042f1306a17759bd40f1fe495d95a5/polling2-0.4.1.tar.gz" } ], "0.4.3": [ { "comment_text": "", "digests": { "md5": "6694c6694c6abe2580f8843356ad9efb", "sha256": "da5803ef73c020501b4a55e62044aed95cb92adf526c0e7eaeeea6ed96d048d1" }, "downloads": -1, "filename": "polling2-0.4.3-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "6694c6694c6abe2580f8843356ad9efb", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 7769, "upload_time": "2019-06-15T23:51:24", "url": "https://files.pythonhosted.org/packages/86/20/2ba93e67bceb8957c882d080839108b336fe006b9a340a9001d063c52d9c/polling2-0.4.3-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "b86dc6d3147f9de60b83c883b7148e51", "sha256": "b2ba8f58cc697220f84e238e58bbe7cabfd69b9869dd7ad6d202789baec207a3" }, "downloads": -1, "filename": "polling2-0.4.3.tar.gz", "has_sig": false, "md5_digest": "b86dc6d3147f9de60b83c883b7148e51", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8141, "upload_time": "2019-06-15T23:51:26", "url": "https://files.pythonhosted.org/packages/7b/1f/b69bd729b0ac6b48d826dd88c42daab28b7788734a03af9ac8f5c4e66316/polling2-0.4.3.tar.gz" } ], "0.4.4": [ { "comment_text": "", "digests": { "md5": "f534834025da8d5951757edbe85e6a84", "sha256": "39247a481941469e14df4373f573e944a1f2e14a1d1d77dc355fce3c3e077d1d" }, "downloads": -1, "filename": "polling2-0.4.4-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "f534834025da8d5951757edbe85e6a84", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 8067, "upload_time": "2019-08-14T14:08:37", "url": "https://files.pythonhosted.org/packages/90/67/468a1c7a72095856a4921b0a2d17f22dd383c24b1920fd2d8af7558bd63a/polling2-0.4.4-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "518c2dff6d6907fc55dd76623c3d9a7c", "sha256": "981d30eef4a99bba383a2494c5f994e7116105884f2e09a23bfdafee33ee86b9" }, "downloads": -1, "filename": "polling2-0.4.4.tar.gz", "has_sig": false, "md5_digest": "518c2dff6d6907fc55dd76623c3d9a7c", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8546, "upload_time": "2019-08-14T14:08:39", "url": "https://files.pythonhosted.org/packages/11/e2/21f68c0adecba32a255b72f4c38590c1dc32b7e195db4917c01fdb6392d1/polling2-0.4.4.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "f534834025da8d5951757edbe85e6a84", "sha256": "39247a481941469e14df4373f573e944a1f2e14a1d1d77dc355fce3c3e077d1d" }, "downloads": -1, "filename": "polling2-0.4.4-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "f534834025da8d5951757edbe85e6a84", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 8067, "upload_time": "2019-08-14T14:08:37", "url": "https://files.pythonhosted.org/packages/90/67/468a1c7a72095856a4921b0a2d17f22dd383c24b1920fd2d8af7558bd63a/polling2-0.4.4-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "518c2dff6d6907fc55dd76623c3d9a7c", "sha256": "981d30eef4a99bba383a2494c5f994e7116105884f2e09a23bfdafee33ee86b9" }, "downloads": -1, "filename": "polling2-0.4.4.tar.gz", "has_sig": false, "md5_digest": "518c2dff6d6907fc55dd76623c3d9a7c", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8546, "upload_time": "2019-08-14T14:08:39", "url": "https://files.pythonhosted.org/packages/11/e2/21f68c0adecba32a255b72f4c38590c1dc32b7e195db4917c01fdb6392d1/polling2-0.4.4.tar.gz" } ] }