Skip to main content
██╗    ██╗ █████╗ ██╗███╗   ██╗ ██████╗ 
██║    ██║██╔══██╗██║████╗  ██║██╔════╝ 
██║ █╗ ██║╚█████╔╝██║██╔██╗ ██║██║  ███╗
██║███╗██║██╔══██╗██║██║╚██╗██║██║   ██║
╚███╔███╔╝╚█████╔╝██║██║ ╚████║╚██████╔╝
 ╚══╝╚══╝  ╚════╝ ╚═╝╚═╝  ╚═══╝ ╚═════╝ 

Build Status codecov Code style: black License: MIT image image


W8ing is (as the great Kenneth Reitz would say) waiting and or polling for humans.

Get it? The 8 replaces the ait in waiting :wink: :woman_shrugging:.

Let W8ing help you nuke all your hard calls to time.sleep() and make your tests less flakey :metal:.


Install

Install via pip (highly recommend installing within a Pipenv):

pip3 install w8ing

Usages

Wait until some condition is true:

from w8ing import wait

# This example uses an imaginary function that doesn't immediately give us the value we want.
result = wait.until(lambda: get_cat_treats(8) == 8)

# By default, this wait will return whether or not the condition was true or not.
result
True

Wait until an http request is valid:

import requests
from w8ing import wait

# You can even specify the timeout and retry delay.
response = wait.until(lambda: requests.get("http://www.google.com"), retry_time=1, timeout=15)

# By default a successful response (codes 2XX) object is truthy.
response
<Response [200]>

Wait until a serial device becomes available and catch any associated exceptions (for you hardware people):

import serial
from w8ing import wait

# If successful, you'll get a pyserial object back, otherwise you'll get None!
serial_port = wait.until(
    lambda: serial.Serial('/dev/ttyUSB0'), catch_exceptions=(SerialException,), retry_time=1, timeout=30
)

# If it doesn't open you can make a nice assert so your co-workers love you.
assert serial_port, "Unable to open serial port! Did you even plug it in??"

# Otherwise, continue!
serial_port.read(10).decode()
"boots and cats"

You can also call another function each loop!

from w8ing import wait

cat_treats = []

# The call_each_try function gets called each time the condition gets evaluated, 
result = wait.until(lambda: len(cat_treats) > 8, call_each_try=lambda: cat_treats.append("treat"), retry_time=0.5)

result
True

# The cat will be pleased, very pleased.

But wait, there's more!

What if you need to ensure that some condition remains true?? Got you covered fam:

from w8ing import wait

cat_nip = ["cat nip"] * 10

# Set retry delay to 0 so we can evaluate the condition as fast as possible! 
result = wait.ensure(lambda: len(cat_nip) > 5, call_each_try=lambda: cat_nip.pop(), retry_time=0, timeout=2)

result
True

# The cat will get extra intoxicated by this high quality cat nip, good job.

Note: Due to computing power and other operating system processes, it is possible that the condition could flicker to false and back to true before being able to catch it!

Contribute

Feel free to open an issue and once you get a green light, submit a PR!

All PRs will receive respectful and constructive feedback.

Metadata

Release files for w8ing 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for w8ing 1.0.2
File Size Uploaded
w8ing-1.0.2.tar.gz 5.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for w8ing 1.0.2
File Interpreter ABI Platform
w8ing-1.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 13.9 kB

Release files / w8ing-1.0.2.tar.gz

Download URL w8ing-1.0.2.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
f0906f323ab4b188388ccc5eded0d30df863ca11478556eaf35fe926586b4c15
BLAKE2b-256 checksum
How to use checksums
5b7ba732f2d37e24ed679cb6f7f7b9193bc787bf100786b4ee685a467c424f26
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.7.0

Release files / w8ing-1.0.2-py3-none-any.whl

Download URL w8ing-1.0.2-py3-none-any.whl
Size 8.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e1622744148fb0cecda16e91787548c0a1000e02d8daef492ea1deef1f5817e2
BLAKE2b-256 checksum
How to use checksums
cf08c0449f394798b9e12bc3868b9af5685c576e62b1b4680a3cb161117ddc0f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.7.0

Release history Release notifications | RSS feed

This release

1.0.2 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page