Decorator for simulating Golang's error handling in Python
Project description
goerrpy
Decorator for simulating Go's error handling in Python
A simple example:
from goerrpy import goerr
@goerr()
def hello_world():
raise Exception("WORLD SHALL NOT BE GREETED!")
value, err = hello_world()
if err is not None:
print(err)
Passing variables:
@goerr()
def get_numbers_larger_than_five(list: typing.List):
result = []
for num in list:
if type(num) is not int:
raise Exception("All values should be integers!")
if num > 5:
result.append(num)
return result
value, err = get_numbers_larger_than_five([4, 5, 6, 7])
if err is not None:
print(err)
# value is [6, 7]
How to unpack multiple values:
@goerr(unpack=True)
def get_numbers_smaller_than_five_but_unpack(list: typing.List):
result = []
for num in list:
if type(num) is not int:
raise Exception("All values should be integers!")
if num < 5:
result.append(num)
return result, result # so that it can be unpacked
first_list, second_list, err = get_numbers_smaller_than_five_but_unpack([3, 4, 5, 7])
if err is not None:
print(err)
# first_list is [3, 4] and second_list is [3, 4]
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
goerrpy-1.0.3.tar.gz
(1.8 kB
view details)
Built Distribution
File details
Details for the file goerrpy-1.0.3.tar.gz
.
File metadata
- Download URL: goerrpy-1.0.3.tar.gz
- Upload date:
- Size: 1.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7a656e1d08734e964f3787734a961c30c8a534768606c805a68ed2818250e335 |
|
MD5 | 6acc8ca311f1ded8cd24b3d40ec230a8 |
|
BLAKE2b-256 | 31e9757d684182ea2c2cf089d74cf3596569f3e258dc6ef9970dbe1365e7d42f |
File details
Details for the file goerrpy-1.0.3-py3-none-any.whl
.
File metadata
- Download URL: goerrpy-1.0.3-py3-none-any.whl
- Upload date:
- Size: 2.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ce1dac947b8deb762889451981b045f99694d319d7e40d79a1277108b6ba92a6 |
|
MD5 | 02b4bce7dbac8db0b7d9696ee42b3673 |
|
BLAKE2b-256 | c49c2924430940cedd26e1e9847a2b6d6e8b9cb9722aeba4f2df67a21ce03216 |