Skip to content

Commit e136a05

Browse files
authored
Merge pull request #16 from ddmee/readthedocs
Readthedocs for polling2
2 parents ffaf1fc + dd5fddb commit e136a05

File tree

16 files changed

+645
-230
lines changed

16 files changed

+645
-230
lines changed

.readthedocs.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# .readthedocs.yml
2+
# Read the Docs configuration file
3+
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details
4+
5+
# Required
6+
version: 2
7+
8+
# Build documentation in the docs/ directory with Sphinx
9+
sphinx:
10+
configuration: docs/conf.py
11+
12+
# Build documentation with MkDocs
13+
#mkdocs:
14+
# configuration: mkdocs.yml
15+
16+
# Optionally build your docs in additional formats such as PDF
17+
# formats:
18+
# - pdf
19+
20+
# Optionally set the version of Python and requirements required to build your docs
21+
python:
22+
version: 3.7
23+
install:
24+
- requirements: docs/requirements.txt

LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2015 Justin Iso, 2019 Donal Mee
3+
Copyright (c) 2015 Justin Iso, 2019-2020 Donal Mee
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 11 additions & 187 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
[![Build Status](https://travis-ci.org/ddmee/polling2.svg?branch=master)](https://travis-ci.org/ddmee/polling2)
1+
[![Build Status](https://travis-ci.com/ddmee/polling2.svg?branch=master)](https://travis-ci.org/ddmee/polling2)
22
[![PyPI](https://img.shields.io/pypi/dm/polling2.svg)]()
33
[![PyPI](https://img.shields.io/pypi/v/polling2.svg)]()
44
[![HitCount](http://hits.dwyl.io/ddmee/polling2.svg)](http://hits.dwyl.io/ddmee/polling2)
55

6-
polling2
7-
=============
6+
# polling2
87

98
_Never write another polling function again!_
109

10+
Documentation available at [Read the Docs](https://polling2.readthedocs.io)
11+
1112
Polling2 is a powerful python utility used to wait for a function to return a certain expected condition.
1213

1314
Some possible uses cases include:
@@ -20,15 +21,15 @@ Polling2 is handy for getting rid of all that duplicated polling-code. Often, ap
2021

2122
Polling2 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.
2223

23-
Polling2 is ++under active development++. Would you like to see a particular feature? Ask and thou shall recieve.
24+
Polling2 is _under active development_. Would you like to see a particular feature? Ask and thou shall recieve.
2425

25-
# Installation
26+
## Installation
2627

2728
```
2829
pip install polling2
2930
```
3031

31-
# Development installation
32+
## Development installation
3233

3334
```shell
3435
# install lib, but use system links from the repo into sitepackages.
@@ -39,30 +40,7 @@ python setup.py test
3940
pytest tests
4041
```
4142

42-
# Examples
43-
44-
### Example: Poll every minute until a url returns 200 status code
45-
46-
```python
47-
import requests
48-
polling2.poll(
49-
lambda: requests.get('http://google.com').status_code == 200,
50-
step=60,
51-
poll_forever=True)
52-
```
53-
54-
If 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:
55-
56-
```python
57-
import requests
58-
polling2.poll(
59-
lambda: requests.get('your.instance.ip').status_code == 200,
60-
step=60,
61-
ignore_exceptions=(requests.exceptions.ConnectionError,),
62-
poll_forever=True)
63-
```
64-
65-
### Example: Poll for a file to exist
43+
## Example:
6644

6745
```python
6846
# This call will wait until the file exists, checking every 0.1 seconds and stopping after 3 seconds have elapsed
@@ -75,163 +53,9 @@ file_handle = polling2.poll(
7553
# Polling will return the value of your polling function, so you can now interact with it
7654
file_handle.close()
7755
```
78-
79-
### Example: Polling for Selenium WebDriver elements
80-
81-
```python
82-
from selenium import webdriver
83-
driver = webdriver.Firefox()
84-
85-
driver.get('http://google.com')
86-
search_box = polling2.poll(
87-
lambda: driver.find_element_by_id('search'),
88-
step=0.5,
89-
timeout=7)
90-
91-
search_box.send_keys('python polling')
92-
```
93-
94-
### Example: Using the polling timeout exception
95-
96-
```python
97-
# An exception will be raised by the polling function on timeout (or the maximum number of calls is exceeded).
98-
# This exception will have a 'values' attribute. This is a queue with all values that did not meet the condition.
99-
# You can access them in the except block.
100-
101-
import random
102-
try:
103-
polling2.poll(lambda: random.choice([0, (), False]), step=0.5, timeout=1)
104-
except polling2.TimeoutException, te:
105-
while not te.values.empty():
106-
# Print all of the values that did not meet the exception
107-
print te.values.get()
108-
```
109-
110-
### Example: Using a custom checker
111-
112-
```python
113-
# is_truthy() is the default checker for the parameter check_success. But, it's easy to create a custom
114-
# checker function, that tests whether the value returned by the target is the expected value.
115-
116-
# Here the target is going to return None, which the custom checker, created by is_value(None)
117-
# will return True for.
118-
polling2.poll(target=lambda: None, step=0.1, max_tries=1, check_success=polling2.is_value(None))
119-
# Or another example, where we can test that False is returned by the target.
120-
polling2.poll(target=lambda: False, step=0.1, max_tries=1, check_success=polling2.is_value(False))
121-
```
122-
123-
### Example: Using a custom condition callback function
124-
125-
```python
126-
import requests
127-
128-
def is_correct_response(response):
129-
"""Check that the response returned 'success'"""
130-
return response == 'success'
131-
132-
polling2.poll(
133-
lambda: requests.put('http://mysite.com/api/user', data={'username': 'Jill'},
134-
check_success=is_correct_response,
135-
step=1,
136-
timeout=10)
137-
```
138-
139-
### Example: Logging the return values from the target function.
140-
141-
```python
142-
import logging
143-
import requests
144-
145-
def is_correct_response(response):
146-
"""Check that the response returned 'success'"""
147-
return response == 'success'
148-
149-
polling2.poll(
150-
lambda: requests.put('http://mysite.com/api/user', data={'username': 'Jill'},
151-
check_success=is_correct_response,
152-
step=1,
153-
timeout=10,
154-
log=logging.DEBUG)
155-
```
156-
157-
This will log the string representation of response object to python's logging module at the debug level.
158-
A message like this will be sent to the log for each return value. You can change the level by providing
159-
a different value to the log parameter.
160-
161-
```text
162-
poll() calls check_success(<Response [200]>)
163-
```
164-
165-
There is also an option to log the exceptions that are caught by ignore_exceptions. Note, the full-exception traceback
166-
will not be printed in the logs. Instead, the error and it's message (using %r formatting) will appear. In the following
167-
code snippet, the ValueError raised by the function `raises_error()` will be sent to the logger at the 'warning' level.
168-
169-
```python
170-
import polling2
171-
import logging
172-
import mock
173-
174-
# basicConfig should sent warning level messages to the stdout.
175-
logging.basicConfig()
176-
177-
# Create a function that raises a ValueError, then a RuntimeError.
178-
raises_error = mock.Mock(side_effect=[ValueError('a message'), RuntimeError])
179-
180-
try:
181-
polling2.poll(
182-
target=raises_error,
183-
step=0.1,
184-
max_tries=3,
185-
ignore_exceptions=(ValueError), # Only ignore the ValueError.
186-
log_error=logging.WARNING # Ignored errors should be passed to the logger at warning level.
187-
)
188-
except RuntimeError as _e:
189-
print "Un-ignored %r" % _e``
190-
```
191-
192-
# Future extensions
193-
194-
- 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.
195-
- Add an option to do via multiprocessing.
196-
- Add an option to do via threading - probably the default option.
197-
- 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.
198-
- Allow step to be specificed as 0, so that we can poll continously. (Perhaps it's best to write a poll_continous() method.)
199-
200-
# Release notes
201-
202-
## 0.4.5
203-
- 'Begin poll(*)' message is logged when poll() is called. Hopefully this means the user doesn't feel the need to write a message before
204-
every call to poll() to indicate how long the poll() might take.
205-
206-
## 0.4.4
207-
- Add is_value() function. A function that allows a user to easily build a custom checker, like is_truthy(), but for any value.
208-
209-
## 0.4.3
210-
- Add log_error parameter to the poll signature. Enables logging of ignored exceptions.
211-
212-
## 0.4.2
213-
- Add log_value() decorator and log parameter to poll signature. Enables logging of return_values.
214-
215-
## 0.4.0
216-
- Fixed polling function from waiting another sleep whenever the max_tries value has reached zero.
217-
- Remove test-only dependencies from requirements to install the package.
218-
- No longer testing on python 2.6. Add support for travis testing on python 3.6 and pypy 3.5.
219-
- Creation of polling2, forked from polling as previous maintainer seems to be ignoring issues and pull-requests.
220-
- Remove ```*a, **k``` from poll signature. This allows Type errors to be raised if caller spells arguments into correctly, making bugs easier to find.
221-
222-
## 0.3.0
223-
224-
- Support Python 3.4+
225-
226-
## 0.2.0
227-
228-
- 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.
229-
- Fix a bug that actually ran 1 more time than value specified by max_tries
23056

231-
## 0.1.0
57+
There are [more examples](https://polling2.readthedocs.io/en/latest/examples) in the documentation.
23258

233-
- First version
59+
## API and user guide at [Read the Docs](https://polling2.readthedocs.io)
23460

235-
# Contributors
236-
- Justin Iso (original creator)
237-
- Donal Mee
61+
[![Read the Docs](https://raw.githubusercontent.com/ddmee/polling2/master/ext/read_the_docs.png)](https://polling2.readthedocs.io)

docs/Makefile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Minimal makefile for Sphinx documentation
2+
#
3+
4+
# You can set these variables from the command line, and also
5+
# from the environment for the first two.
6+
SPHINXOPTS ?=
7+
SPHINXBUILD ?= sphinx-build
8+
SOURCEDIR = .
9+
BUILDDIR = _build
10+
11+
# Put it first so that "make" without argument is like "make help".
12+
help:
13+
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
14+
15+
.PHONY: help Makefile
16+
17+
# Catch-all target: route all unknown targets to Sphinx using the new
18+
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
19+
%: Makefile
20+
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

docs/api.rst

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
The API
2+
=======
3+
4+
.. module:: polling2
5+
6+
This part of the documentation covers all the interfaces of Requests. For
7+
parts where Requests depends on external libraries, we document the most
8+
important right here and provide links to the canonical documentation.
9+
10+
11+
Poll Method
12+
-----------
13+
14+
The main method is the poll method.
15+
16+
.. autofunction:: poll
17+
18+
19+
Helper Methods
20+
--------------
21+
22+
There are some other supplemental methods that can help out.
23+
24+
.. autofunction:: step_constant
25+
.. autofunction:: step_linear_double
26+
.. autofunction:: is_truthy
27+
.. autofunction:: is_value
28+
.. autofunction:: log_value
29+
30+
Exceptions
31+
----------
32+
33+
.. autoexception:: PollingException
34+
.. autoexception:: TimeoutException
35+
.. autoexception:: MaxCallException

docs/conf.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Configuration file for the Sphinx documentation builder.
2+
#
3+
# This file only contains a selection of the most common options. For a full
4+
# list see the documentation:
5+
# https://www.sphinx-doc.org/en/master/usage/configuration.html
6+
7+
# -- Path setup --------------------------------------------------------------
8+
9+
# If extensions (or modules to document with autodoc) are in another directory,
10+
# add these directories to sys.path here. If the directory is relative to the
11+
# documentation root, use os.path.abspath to make it absolute, like shown here.
12+
#
13+
# import os
14+
import sys
15+
# sys.path.insert(0, os.path.abspath('.'))
16+
from pathlib import Path
17+
sys.path.append(str(Path('../').resolve()))
18+
import polling2
19+
20+
# -- Project information -----------------------------------------------------
21+
22+
project = 'Polling2'
23+
copyright = '2020, Donal Mee'
24+
author = 'Donal Mee'
25+
26+
# The full version, including alpha/beta/rc tags
27+
release = polling2.__version__
28+
version = release
29+
30+
31+
# -- General configuration ---------------------------------------------------
32+
33+
# Add any Sphinx extension module names here, as strings. They can be
34+
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
35+
# ones.
36+
extensions = [
37+
"sphinx.ext.autodoc",
38+
"sphinx.ext.viewcode",
39+
]
40+
41+
42+
# Add any paths that contain templates here, relative to this directory.
43+
templates_path = ['_templates']
44+
45+
# List of patterns, relative to source directory, that match files and
46+
# directories to ignore when looking for source files.
47+
# This pattern also affects html_static_path and html_extra_path.
48+
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
49+
50+
51+
# -- Options for HTML output -------------------------------------------------
52+
53+
# The theme to use for HTML and HTML Help pages. See the documentation for
54+
# a list of builtin themes.
55+
#
56+
html_theme = 'alabaster'
57+
58+
html_theme_options = {
59+
"show_powered_by": False,
60+
"github_user": "ddmee",
61+
"github_repo": "polling2",
62+
"github_banner": True,
63+
"show_related": False
64+
}
65+
66+
# Add any paths that contain custom static files (such as style sheets) here,
67+
# relative to this directory. They are copied after the builtin static files,
68+
# so a file named "default.css" will overwrite the builtin "default.css".
69+
html_static_path = ['_static']
70+
71+
# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.
72+
html_show_copyright = True

0 commit comments

Comments
 (0)