safefunc
Important
You may want to use the newer library fnsafe.
Make sudden erros not stop your program
Installation
pip install safefunc
from safefunc import makesafe
Basic Example
@makesafe()
def divide(a, b):
return a / b
print("Let's calculate!")
result = divide(1, 0)
print(f"{result = }")
Output:
Let's calculate!
Ignoring exception in divide
Traceback (most recent call last):
File "<string>", line 40, in safe
File "<string>", line 75, in divide
ZeroDivisionError: division by zero
result = None
As you can see, the exception gets printed but the program continues.
More Examples
Let's try the same thing with divide(1, 1):
Output:
Let's calculate!
result = 1
Now everything worked fine.
Some functions may return None. If an error
happens, it would also return None. Luckily
we can differate between success and failure
with the return_entire_result paramter:
@makesafe(return_entire_result = True)
def divide(a, b):
return a / b
print("Let's calculate!")
result = divide(1, 0)
if result: # or result.success
print(result()) # or result.result
In this case our function returns a Result
object which allows us to check wether the
function succeeded or not. The result
attribute (which also can be accessed by
calling the object) returns depending on the
success, the function return value or the
exception.
Let's expect, we call divide("1", 1). Because
the first argument is not supported for
division, it would raise an error. If we only want
to ignore ZeroDivisionErrors, we can make use of
the ignore parameter. You can interpret this as
a whitelist:
@makesafe(return_entire_result = True, ignore = [ZeroDivisionError])
def divide(a, b):
return a / b
print("Let's calculate!")
result = divide("1", 1)
if result:
print(result())
This will raise any error that occures except
ZeroDivisionError.
We can also reverse the situation and ignore
any exception except ZeroDivisionError by
setting the raise_when parameter. You can
understand this as a blacklist:
@makesafe(return_entire_result = True, raise_when = [ZeroDivisionError])
def divide(a, b):
return a / b
print("Let's calculate!")
result = divide("1", 1)
if result:
print(result())
Keep in mind that you can not set both
ignore and raise_when. If you do
so, a TypeError gets raised.
Other Parameters
quiet
refrains displaying the exception
type: bool
default: False
title
text that gets displayed above the exception
type: str
default: "Ignoring exception in {function.__name__}"
colored
displays the exception in a darker color; may not work on every device / console; should not be used when using a file for the output
type: bool
default: False
file
a file or file-like object where the ouput gets written to
type: typing.TextIO
default: sys.stderr
Tips
In case you want to just run a function safely instead of directly making it safe, you can include this function:
from safefunc import makesafe
def runsafe(function, *args, **kwargs):
return makesafe(*args, **kwargs)(function)
Metadata
Release files for safefunc 0.0.5.post1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| safefunc-0.0.5.post1.tar.gz | 3.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| safefunc-0.0.5.post1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 7.4 kB
Release files / safefunc-0.0.5.post1.tar.gz
| Download URL | safefunc-0.0.5.post1.tar.gz |
|---|---|
| Size | 3.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
27af847c806c45fd52fb467148487f2e0c7355ba874de637f9f3fc49f651e7d3
|
|
BLAKE2b-256 checksum How to use checksums |
34f5e4d3a3e93fc2c924231aa7bd9fc418033e479ff1bb1a898be7fd82d3cdc8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.6
|
Release files / safefunc-0.0.5.post1-py3-none-any.whl
| Download URL | safefunc-0.0.5.post1-py3-none-any.whl |
|---|---|
| Size | 3.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bb34bbf64f89e2be6309d797d59ff3572aa25d3c4811ce84b5edb75dc9fdb0d3
|
|
BLAKE2b-256 checksum How to use checksums |
ca72836b8db1a3c55fabdbe5e8fbb172e8aca56cb963f0e3aa8c5646ac2e9f60
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.6
|