You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ResultContainer is a Python library inspired by [Rust’s Result](https://doc.rust-lang.org/std/result/enum.Result.html) enum, designed for robust error handling. It seamlessly supports mathematical operations, attribute access, and method chaining on `Ok(value)`, while automatically transitioning to `Err(e)` upon encountering errors, ensuring continuous error tracking and logging. Ideal for developers seeking structured and functional error management in Python.
7
9
10
+
## Description
8
11
9
-
The `ResultContainer` module simplifies complex error handling into clean, readable, and maintainable code structures. Error handling in Python can often become unwieldy, with deeply nested `try/except` blocks and scattered error management. The `ResultContainer` is used for situations when errors are expected and are easily handled. Inspired by [Rust’s Result<Ok, Err>](https://doc.rust-lang.org/std/result/enum.Result.html) enum, `ResultContainer` introduces a clean and Pythonic way to encapsulate success (`Ok`) and failure (`Err`) outcomes.
12
+
The `ResultContainer` module simplifies complex error handling into clean, readable, and maintainable code structures. Error handling in Python can often become unwieldy, with deeply nested `try/except` blocks and scattered error management. The `ResultContainer` is used for situations when errors are expected and are easily handled. Inspired by Rust's Result<Ok, Err> enum, `ResultContainer` introduces a clean and Pythonic way to encapsulate a `Result` as a success (`Ok`) and failure (`Err`) outcomes.
10
13
11
-
The `ResultContainer.Result` enum wraps a value in an `Ok` variant, until there is an exception or error raised, and then it is converted to the `Err` variant. The `Err` variant wraps a `ResultContainer.ResultErr` exception object that contains the error messages and traceback information. The `Result` object includes similar methods to the Rust Result Enum for inquiry about the state, mapping functions, and passing attributes/methods to the containing `value`.
14
+
The `ResultContainer` module contains two classes, `ResultErr` and `Result`. The `ResultContainer.ResultErr` class extends the Exception class to collect and store error messages and traceback information. The `ResultContainer.Result` is the enum with two variants: `ResultContainer.Result.Ok(value)` and `ResultContainer.Result.Err(e)`. The `Ok(value)` variant wraps any `value` as long as no exceptions occur. The `Ok` variant cannot directly wrap another Result or ResultErr objects. However, `Ok` can wrap another object, such as a list, that contains Result and ResultErr objects. Methods and attributes that are not part of the Result class are automatically passed to the wrapped `value`. For example, `Ok(value).method()` becomes `Ok(value.method())`. If an `Ok` variant operation results in raising an exception, the exception and traceback info is stored in a `ResultErr` object (`e`) and the `Ok` is converted to the `Err(e)` variant. Subsequent errors are appended to `e`. `Result` contains status inquiry methods, unwrap methods to get the stored `value` or `e`, and the ability to raise a `ResultErr` exception for the `Err(e)` variant.
12
15
13
16
The `ResultContainer` is designed to streamline error propagation and improve code readability, `ResultContainer` is ideal for developers seeking a robust, maintainable approach to handling errors in data pipelines, API integrations, or asynchronous operations.
then rename the file `ResultContainer/__init__.py` to `ResultContainer/ResultContainer.py` and move `ResultContainer.py` to wherever you want to use it.
33
36
37
+
38
+
34
39
## Variants
35
40
36
41
```python
37
42
# Result is the main class and Ok and Err are constructors.
38
-
from ResultContainer import Result, Ok, Err
43
+
from ResultContainer import Result, Ok, Err, ResultErr
39
44
```
40
45
41
46
-`Ok(value)`
42
-
-`value` is wrapped within an `Ok`.
47
+
-`value` is any object to be wrapped within an `Ok`.
43
48
- Constructor: `Result.as_Ok(value)`
44
49
-`Result.Ok` attribute returns the wrapped `value`
45
50
- Can never wrap a `ResultErr` instance (it will just be converted to an `Err(value)`).
46
51
47
52
-`Err(e)`
48
-
-`e` is wrapped within an `Err`, and `type(e) is ResultErr`.
53
+
-`e` is any object to be wrapped within an `Err`.
54
+
-`e` is stored as `ResultErr` Exception object.
55
+
- If `not isinstance(e, ResultErr)`, then `e = ResultErr(e)`.
49
56
- Constructor: `Result.as_Err(error_msg)`
50
57
-`Result.Err` attribute returns the wrapped `e`.
51
58
52
59
### Properties of the `Result` Variants
53
60
54
61
#### `Err(e)`:
55
62
56
-
- Represents a failure (error-state) and contains `e` as a `ResultErr` object that stores error messages and traceback information.
63
+
- Represents a failure (error-state).
64
+
-`e` is a `ResultErr` object that stores error messages and traceback information.
57
65
58
-
- Can be initialized with `Err(error_msg)`
66
+
- If `e` is another type, it is converted to a `ResultErr`.
67
+
That is, given `Err(e)` and `not isinstance(e, ResultErr)` becomes `Err( ResultErr(e) )`.
68
+
69
+
- Can be initialized with `Err(error_msg)`, where `error_msg` can be any object (typically a str)
59
70
-`Err(e)` ➥ syntactic-sugar for ➥ `Result.as_Err(e)`
60
71
61
72
- If an `Ok(value)` operation fails, then it is converted to an `Err(e)`, where `e` stores the error message.
@@ -94,14 +105,79 @@ from ResultContainer import Result, Ok, Err
94
105
-`Err(e1) < Err(e2) ` ➣ `False`
95
106
-`Err(e1) <= Err(e2) ` ➣ `True`
96
107
97
-
## Initialization
108
+
109
+
110
+
## ResultErr Class
111
+
112
+
The `ResultErr` class is a custom exception class for error handling in the `Result` object. The `ResultErr` class captures error messages and optional traceback information. Its primary use is for identifying when a `Result` instance is an `Err` variant, which is handled automatically. It should not be necessary to use the ResultErr class directly, but select attributes and methods are presented here for background information.
113
+
114
+
### Initialization
115
+
116
+
```python
117
+
# All arguments are optional
118
+
from ResultContainer import ResultErr
119
+
120
+
# Main object signature:
121
+
e = ResultErr(msg="", add_traceback=True, max_messages=20)
122
+
# msg (Any, optional): Error message(s) to initialize with.
123
+
# `str(msg)` is the message that is stored.
124
+
# If msg is a Sequence, then each item in the Sequence is
125
+
# appended as str(item) to the error messages.
126
+
# Default is "", to disable error status.
127
+
# add_traceback (bool, optional): If True, then traceback information is added to the message.
128
+
# max_messages (int, optional): The maximum number of error messages to store.
129
+
# After this, all additional messages are ignored. Default is 20.
130
+
```
131
+
132
+
### Attributes and Methods
133
+
134
+
These are select attributes and methods built into `Result` object.
135
+
136
+
#### Attributes
137
+
138
+
```
139
+
size (int): Returns the number of error messages.
140
+
141
+
is_Ok (bool): Returns False if in error status (ie, size == 0).
142
+
is_Err (bool): Returns True if in error status (ie, size > 0).
143
+
144
+
Err_msg (list[str]): List of the error messages that have been added.
145
+
Err_traceback (list[list[str]]): List of lists that contains the traceback information for each error message.
146
+
```
147
+
148
+
#### Methods
149
+
150
+
```
151
+
append(msg, add_traceback=True):
152
+
Append an error message to the instance.
153
+
154
+
raises(add_traceback=False, error_msg=""):
155
+
Raise a ResultErr exception if `size > 0`.
156
+
`error_msg` is an optional note to append to the ResultErr.
0 commit comments