Skip to main content

ResultWrapper

How to install it?

You can install ResultWrapper from this Github repository with python3 setup.py install, or just install it directly from pypi with pip3 install result-wrapper.

What is it?

ResultWrapper is a very small utility library that provides a class containing either an Error, or a set of Results. This can be used in APIs to quickly and easy communicate if the operation failed or not. A ResultWrapper instance provides access to its underlying results if it did not fail, otherwise all access to possible results are denied.

How to use it?

To enable the capabilities of ResultWrapper, decorate a function with the wrap_result decorator. This function then returns an instance of ResultWrapper. To check wether a ResultWrapper is failed or not, use the .failed property.

You can pass a tuple of exception classes that should be caught and transformed to a failed Result in the decorator parameter exceptions:

from result_wrapper import wrap_result

@wrap_result(exceptions=(ValueError,))
def function1():
    raise ValueError("This exception will be caught and transformed to a failed result.")

@wrap_result(exceptions=(ValueError,))
def function2():
    raise TypeError("This exception will not be caught. The exception is thrown normally.")

assert function1().failed is True
function2() # this will throw TypeError !!!

Another way of returning a failed result is to use the ResultWrapper.make_failed function:

from result_wrapper import wrap_result, ResultWrapper

@wrap_result
def function1():
    return ResultWrapper.make_failed()

assert function1().failed is True

Optionally you can pass any object to the make_failed function to pass some error information to the caller. In the case of a thrown exception, the error information is set to the arguments to the constructor of the exception:

from result_wrapper import wrap_result, ResultWrapper

@wrap_result
def function1():
    return ResultWrapper.make_failed({"ErrorID": 1})

@wrap_result(exceptions=(ValueError,))
def function2():
    raise ValueError({"ErrorID": 1})

assert function1().error_information.get("ErrorID") == 1
assert function2().error_information.get("ErrorID") == 1

To return a successful result, return an instance of Result or ResultWrapper.make_succeeded:

from result_wrapper import wrap_result, Result, ResultWrapper

@wrap_result
def function1():
    return Result(result1="value1", result2=2)

@wrap_result
def function2():
    return ResultWrapper.make_succeeded(result1="value1", result2=2)

result = function1()
assert result.result1 == "value1"
assert result.result2 == 2

result = function2()
assert result.result1 == "value1"
assert result.result2 == 2

Metadata

Release files for result-wrapper 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for result-wrapper 1.0.0
File Size Uploaded
result_wrapper-1.0.0.tar.gz 3.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for result-wrapper 1.0.0
File Interpreter ABI Platform
result_wrapper-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 8.2 kB

Release files / result_wrapper-1.0.0.tar.gz

Download URL result_wrapper-1.0.0.tar.gz
Size 3.9 kB
Tags Source
SHA-256 checksum
How to use checksums
32ba4da39555875b36a681e90a5471398016c9443ca6d99bc7ca4e9d12d75a74
BLAKE2b-256 checksum
How to use checksums
bc4b9ef60d55780085439fd4ff916a1fc20147a6b39778a9a9f32fbc8358e43e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.4

Release files / result_wrapper-1.0.0-py3-none-any.whl

Download URL result_wrapper-1.0.0-py3-none-any.whl
Size 4.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6a01d7bbb752938f57f0011c5316ee62f55a7011d93b45292be54966f612b519
BLAKE2b-256 checksum
How to use checksums
8efd84ecf3ae29d2cd7d3a34c6b2730192f6c12e8a1b1d371bc94e794b325754
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.4

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page