Python code snippets from popular open-source libraries delivered to your inbox.
See the latest issue here
Frequency: Maximum 3 emails per week

0 Spam, Only Python

Here is a sample:

Retrying HTTP Requests with Backoff

A technique for retrying failed HTTP requests.
From Google Maps Services Python

"""
first_request_time: The time of the first request (None if no retries have occurred).
retry_counter: The number of this retry, or zero for first attempt.
"""

if not first_request_time:
    first_request_time = datetime.now()

elapsed = datetime.now() - first_request_time
if elapsed > self.retry_timeout:
    raise googlemaps.exceptions.Timeout()

if retry_counter > 0:
    # 0.5 * (1.5 ^ i) is an increased sleep time of 1.5x per iteration,
    # starting at 0.5s when retry_counter=0. The first retry will occur
    # at 1, so subtract that first.
    delay_seconds = 0.5 * 1.5 ** (retry_counter - 1)

    # Jitter this value by 50% and pause.
    time.sleep(delay_seconds * (random.random() + 0.5))

"""
Source: google-maps-services-python
https://github.com/googlemaps/google-maps-services-python
"""   

You can also checkout a runnable demo on Repl.it by clicking the button below


Do reach out to aswin@pythonninja.xyz for any suggestion or queries