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
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.
10
10
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` 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`.
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`.
12
12
13
13
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
33
34
-
## `Result`Variants
34
+
## Variants
35
35
36
36
```python
37
37
# Result is the main class and Ok and Err are constructors.
38
38
from ResultContainer import Result, Ok, Err
39
39
```
40
40
41
41
-`Ok(value)`
42
-
-`value` is wrapped within an `Ok`.
42
+
-`value` is wrapped within an `Ok`.
43
43
- Constructor: `Result.as_Ok(value)`
44
44
-`Result.Ok` attribute returns the wrapped `value`
45
+
- Can never wrap a `ResultErr` instance (it will just be converted to an `Err(value)`).
46
+
45
47
-`Err(e)`
46
-
-`e` is a `ResultErr` object wrapped within an `Err`.
48
+
-`e` is wrapped within an `Err`, and `type(e) is ResultErr`.
47
49
- Constructor: `Result.as_Err(error_msg)`
48
50
-`Result.Err` attribute returns the wrapped `e`.
49
51
50
-
51
52
### Properties of the `Result` Variants
52
53
53
-
#### `Ok(value)`
54
-
55
-
- Represents success (non-error state).
56
-
The `value` is wrapped within the `Ok()`.
57
-
- Can be initialized with `Ok(value)`
58
-
-`Ok(value)` ➥ syntactic-sugar for ➥ `Result.as_Ok(value)`
59
-
- Math operations are redirected to `value` and rewrap the solution or concatenate the errors.
0 commit comments