Skip to main content

Make sudden erros not stop your program

Project description

safefunc

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. If we only want to ignore ZeroDivisionErrors, we can make use of the ignore paramter:

@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 to True:

@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

inspired by dicsord.py

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

safefunc-0.0.2-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

Details for the file safefunc-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: safefunc-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 3.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for safefunc-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f6ab755d86b8ac927998fc3528070c7a73aff3a776a72d6cb6f5fbf6b43c7ddc
MD5 cfdb4025fa057bc55c1f3747389ec8ed
BLAKE2b-256 b2eeff64e5676d583fdc8398638581f7361d1c1c7788430d09f2adecc95f82d9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page