Skip to content

Commit

Permalink
added badges and code usage
Browse files Browse the repository at this point in the history
  • Loading branch information
henriquelino committed Dec 12, 2022
1 parent fad22bc commit a2c6027
Showing 1 changed file with 80 additions and 6 deletions.
86 changes: 80 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,55 @@
# retimer

A simple package to make retry loops easier
> A simple package to make retry loops easier
### Usage:
[![PyPI version][pypi-image]][pypi-url]
[![Build status][build-image]][build-url]
[![GitHub stars][stars-image]][stars-url]
[![Support Python versions][versions-image]][versions-url]



## Getting started

You can [get `retimer` from PyPI](https://pypi.org/project/retimer),
which means it's easily installable with `pip`:

```bash
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:

```python
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:
```python
from retimer import Timer
import time
Expand All @@ -12,17 +59,44 @@ while timer.not_expired:
# do something for 10 seconds

if retry_doing_something:
time.sleep(.5) # good if something is a request to a server or cpu intensive
time.sleep(.5)
continue

if something_bad:
timer.explode()

# all good so we break before timer expires
# all good
break

if timer.expired:
print("Could not do something after tried for 10 seconds")
print(f"Could not do something after {timer.duration} seconds")
else:
print("Successfully did something after 10 seconds")
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](https://github.com/henriquelino/retimer/blob/main/CHANGELOG.md) file.



<!-- Badges -->

[pypi-image]: https://img.shields.io/pypi/v/retimer
[pypi-url]: https://pypi.org/project/retimer/

[build-image]: https://github.com/henriquelino/retimer/actions/workflows/build.yaml/badge.svg
[build-url]: https://github.com/henriquelino/retimer/actions/workflows/build.yaml

[stars-image]: https://img.shields.io/github/stars/henriquelino/retimer
[stars-url]: https://github.com/henriquelino/retimer

[stars-image]: https://img.shields.io/github/stars/mathspp/extendedjson
[stars-url]: https://github.com/mathspp/extendedjson

[versions-image]: https://img.shields.io/pypi/pyversions/retimer
[versions-url]: https://pypi.org/project/retimer/

0 comments on commit a2c6027

Please sign in to comment.