with-err
with-err is a python library that converts try-except pattern into Go-like result, err pattern. I feel result, err pattern easier to maintain in large projects.
Getting Started
Install
pip install with-err
(with uv)
uv pip install with-err
Use as Function
import json
from with_err import with_err
json_loads_e = with_err(json.loads)
data, err = json_loads_e('{"a": 1}')
assert err is None
assert data = {"a": 1}
data, err = json_loads_e('{"a": }')
assert isinstance(err, json.decoder.JSONDecodeError)
assert data is None
Use as Decorator
import json
from with_err import with_err
@with_err
def json_loads_e(a: str | bytes | bytearray):
return json.loads(a)
data, err = json_loads_e('{"a": 1}')
assert err is None
assert data = {"a": 1}
data, err = json_loads_e('{"a": }')
assert isinstance(err, json.decoder.JSONDecodeError)
assert data is None
with_err with Specified Exceptions
Return err only with specified exceptions and raise other exceptions.
import json
from with_err import with_err
@with_err(json.decoder.JSONDecodeError)
def json_loads_e(a: str | bytes | bytearray):
return json.loads(a)
data, err = json_loads_e('{"a": 1}')
assert err is None
assert data = {"a": 1}
data, err = json_loads_e('{"a": }')
assert isinstance(err, json.decoder.JSONDecodeError)
assert data is None
# raise json.decoder.JSONDecodeError
import json
import re
from with_err import with_err
@with_err(re.PatternError)
def json_loads_e(a: str | bytes | bytearray):
return json.loads(a)
data, err = json_loads_e('{"a": }')
# function
import json
from with_err import with_err
json_loads_e = with_err(json.decoder.JSONDecodeError)(json.loads)
data, err = json_loads_e('{"a": 1}')
assert err is None
assert data = {"a": 1}
data, err = json_loads_e('{"a": }')
assert isinstance(err, json.decoder.JSONDecodeError)
assert data is None
# empty: return all Exceptions
import json
from with_err import with_err
json_loads_e = with_err()(json.loads)
data, err = json_loads_e('{"a": 1}')
assert err is None
assert data = {"a": 1}
data, err = json_loads_e('{"a": }')
assert isinstance(err, json.decoder.JSONDecodeError)
assert data is None
Get err Traceback Stack
import json
from with_err import with_err, get_err_strs
def json_loads_e(a: str | bytes | bytearray):
return json.loads(a)
_data, err = json_loads_e('{"a": 1}')
err_stack = get_err_strs(err)
assert err is None
assert err_stack == []
import json
import re
from with_err import with_err, get_err_strs
def json_loads_e(a: str | bytes | bytearray):
return json.loads(a)
_data, err = json_loads_e('{"a": }')
err_stack = get_err_strs(err)
err_str = '\n'.join(err_stack)
assert isinstance(err, json.decoder.JSONDecodeError)
assert len(err_stack) > 0
assert 'json.decoder.JSONDecodeError: Expecting value:' in err_str
assert re.search(r'json/__init__.py", line \d+, in loads', err_str)
assert re.search(r', line \d+, in json_loads_e', err_str)
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
with_err-1.1.0.tar.gz
(2.8 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file with_err-1.1.0.tar.gz.
File metadata
- Download URL: with_err-1.1.0.tar.gz
- Upload date:
- Size: 2.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
495372737f98faeae95710b0776778c9b8dffcbb91d61d8fa86767aa3bd3a4be
|
|
| MD5 |
4dd4905a287945dbee214c645ac4f785
|
|
| BLAKE2b-256 |
b8cb30acb156bf9964e4739c0e8db1d1ed4ddab946fcecb8fd9e6cd8087f55b7
|
File details
Details for the file with_err-1.1.0-py3-none-any.whl.
File metadata
- Download URL: with_err-1.1.0-py3-none-any.whl
- Upload date:
- Size: 3.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ca1ffdc954fb6dedefc19761a5330bb4071da09b3f2fa4b9bf2020171ab6bc7f
|
|
| MD5 |
fbfcfdbda5d5a8f2de5f9845a77a4bf7
|
|
| BLAKE2b-256 |
58d58a707a17804392573b4560cda124edb8cb5dabb05f3d7fb9be233b3ce020
|