exctrap: Trap exceptions for retry
It is not uncommon to have Python code that requires exceptions to be trapped, with the code triggering it retried, e.g., to handle temporary network or host failures when fetching web resources. There are quite a few Python packages that provide this functionality. Or actually, two functionalities: (1) trapping the exception, and (2) retrying the triggering code upon failure.
Typically, they work in the level of function: the code that needs to be retried is written as a function, and it is either wrapped within a context manager or passed to a retry function so that the function is invoked repeatedly until it succeeds or fail sufficiently many times. In this way, the two functionalities are packed into one function or context manager for the user.
We take a slightly different approach: the two functionalities are separated into two entities: a context manager that traps exception, and a function that returns the context manager. We argue that this leads to neater code.
Recipe
This provides most of what we normally need.
for etrapper in exctrap.trial():
with etrapper:
# Do whatever that may fail.
# Will reach here whether or not an exception is raised.
# If you want to reraise the exception here, use etrapper.reraise().
# If retry fails, the last exception is reraised so this is not reached.
Options
The trial() function provides all the options. These include:
num_tries: Maximum number of trials, if the code keeps raising exceptions for this many tries the exception is reraised without further retry.retry_period: Number of seconds to wait between tries (adjusted byperiod_noise).period_noise: Add or subtract at most this fraction of theretry_periodto get the actual amount of seconds to sleep.etypes: Exception types to trap. By default, trap all exceptions derived fromException. This is passed to the constructor ofExcTrapperwhen creating exception trappers.
Note that this does not require any function to be created for the code needs to be exception-proof. Experience shows that code can be a lot more neat as a result: the code can access the required variables much more easily when it does not sit within a separate global function or an inner function.
Implementation
The "etrapper" is the context manager which traps exceptions when
running under the "with" statement. This is done simply by recording
exceptions in the trapper object, and returning True to swallow it, in
the __exit__ function. If needed, the exception can be asked to be
reraised().
The trial() function provides the retry logic and create exception
trappers. Because the exception would be trapped in the trapper, all
trial needs to do is to check whether an exception is trapped, and
decide whether to retry or reraise.
Metadata
Release files for exctrap 0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| exctrap-0.3.tar.gz | 5.7 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| exctrap-0.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.7 kB
Release files / exctrap-0.3.tar.gz
| Download URL | exctrap-0.3.tar.gz |
|---|---|
| Size | 5.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5d5ba685adf60dc6394e1831a1cc175a6adbd329eb8a0a1aba7b364ad2c54e17
|
|
BLAKE2b-256 checksum How to use checksums |
42686e6232aadd7d63b9782b8a1b46fe0a68ccc30b63f3f603ff707fe6d2e900
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Release files / exctrap-0.3-py3-none-any.whl
| Download URL | exctrap-0.3-py3-none-any.whl |
|---|---|
| Size | 5.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7b96f376e6c8145db5f9a66929cd866a57a3a884330706855af0cf95d82cb433
|
|
BLAKE2b-256 checksum How to use checksums |
4a4bd4e126659f09ff178d69384edf7ffe8419432f18d2f2f9bea5af95d760b7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Security for exctrap 0.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.