No project description provided
Project description
retimer
A simple package to make retry loops easier
Getting started
You can get retimer
from PyPI,
which means it's easily installable with pip
:
python -m pip install extendedjson
Example usage
Think of a scenario where you need to keep trying to do something for a range of time, usually you can write this:
from time import perfcounter
timeout = 10
begin = perfcounter()
while percounter() - begin < timeout:
# do something for 10 seconds
if retry_doing_something:
time.sleep(.5)
continue
if something_bad:
break
# all good
break
if perfcounter - begin >= timeout:
print(f"Could not do something after {timeout} seconds")
else:
print("Success!")
Rewriting using this package becomes:
from retimer import Timer
import time
timer = Timer(10)
while timer.not_expired:
# do something for 10 seconds
if retry_doing_something:
time.sleep(.5)
continue
if something_bad:
timer.explode()
# all good
break
if timer.expired:
print(f"Could not do something after {timer.duration} seconds")
else:
print("Success!")
Although both codes accomplish the same result, I personally find the second one more readable and shines when I need two or more chained loops
Changelog
Refer to the CHANGELOG.md file.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
retimer-0.1.0.6.tar.gz
(3.5 kB
view hashes)
Built Distribution
Close
Hashes for retimer-0.1.0.6-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fa74e9b000d3114418f99fb1774b18ebbc35c8971f51f9652a950b87de18c2b8 |
|
MD5 | 5de6de4cde9b1421d9904063ee0c9631 |
|
BLAKE2b-256 | f24e177aa7881d195e1e42b20e1e097f7ff6f03fbe513b8ceb0ebbeac4b831ff |