Skip to content

Commit 3c710e2

Browse files
committed
chore: readme.md markdown formatting
1 parent 4a0d559 commit 3c710e2

File tree

2 files changed

+170
-46
lines changed

2 files changed

+170
-46
lines changed

README.md

Lines changed: 153 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
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.
1010

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`.
1212

1313
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.
1414

@@ -31,61 +31,70 @@ git clone https://github.com/ScottBoyce-Python/ResultContainer.git
3131
```
3232
then rename the file `ResultContainer/__init__.py` to `ResultContainer/ResultContainer.py` and move `ResultContainer.py` to wherever you want to use it.
3333

34-
## `Result` Variants
34+
## Variants
3535

3636
```python
3737
# Result is the main class and Ok and Err are constructors.
3838
from ResultContainer import Result, Ok, Err
3939
```
4040

4141
- `Ok(value)`
42-
- `value` is wrapped within an `Ok`.
42+
- `value` is wrapped within an `Ok`.
4343
- Constructor: `Result.as_Ok(value)`
4444
- `Result.Ok` attribute returns the wrapped `value`
45+
- Can never wrap a `ResultErr` instance (it will just be converted to an `Err(value)`).
46+
4547
- `Err(e)`
46-
- `e` is a `ResultErr` object wrapped within an `Err`.
48+
- `e` is wrapped within an `Err`, and `type(e) is ResultErr`.
4749
- Constructor: `Result.as_Err(error_msg)`
4850
- `Result.Err` attribute returns the wrapped `e`.
4951

50-
5152
### Properties of the `Result` Variants
5253

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)` &nbsp;&nbsp; syntactic-sugar for &nbsp;&nbsp; `Result.as_Ok(value)`
59-
- Math operations are redirected to `value` and rewrap the solution or concatenate the errors.
60-
- `Ok(value1) + Ok(value2) `&nbsp; &nbsp;&nbsp; `Ok(value1 + value2)`
61-
- `Ok(value1) + Err(e) `&nbsp; &nbsp;&nbsp; `Err(e + "a + b with b as Err")`
62-
- `Err(e1) + Err(e2) `&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; `Err(e1 + "a + b with a and b as Err.")`
63-
- All attributes and methods not associated with `Result` are redirected to `value`.
64-
- `Ok(value).method()` is equivalent to `Ok(value.method())` and
65-
`Ok(value).attrib` is equivalent to `Ok(value.attrib)`.
66-
- `Ok(value).raises()` does NOT become `Ok(value.raises())`
67-
because `Result.raises()` is a native `Result` method.
68-
- Comparisons redirect to comparing the wrapped `value` if `Ok`. But mixed comparisons assume:
69-
`Err(e) < Ok(value)` and `Err(e1) == Err(e2)` for any `value`, `e`, `e1`, and `e2`.
70-
- `Ok(value1) < Ok(value2) `&nbsp; &nbsp;&nbsp; `value1 < value`
71-
- `Err(e) < Ok(value2) ` &nbsp;&nbsp; `True`
72-
- `Ok(value1) < Err(e) ` &nbsp;&nbsp; `False`
73-
- `Err(e1) < Err(e2) ` &nbsp;&nbsp; `False`
74-
- `Err(e1) <= Err(e2) ` &nbsp;&nbsp; `True`
75-
7654
#### `Err(e)`:
7755

78-
- Represents a failure (error-state) and contains `e` as a `ResultErr` object
79-
that stores error messages and traceback information.
56+
- Represents a failure (error-state) and contains `e` as a `ResultErr` object that stores error messages and traceback information.
57+
8058
- Can be initialized with `Err(error_msg)`
8159
- `Err(e)` &nbsp;&nbsp; syntactic-sugar for &nbsp;&nbsp; `Result.as_Err(e)`
8260

8361
- If an `Ok(value)` operation fails, then it is converted to an `Err(e)`, where `e` stores the error message.
62+
8463
- Any operation on `Err(e)` results in another error message being appended to `e`.
8564

86-
There are methods built into `Result` to check if an error has been raised, or the unwrap the value/error to get its contents.
65+
#### `Ok(value)`
66+
67+
- Represents success (non-error state). The `value` is wrapped within the `Ok()`.
68+
69+
- Can be initialized with `Ok(value)`
70+
- `Ok(value)` &nbsp;&nbsp; syntactic-sugar for &nbsp;&nbsp; `Result.as_Ok(value)`
71+
72+
- If `value` is an instance of `ResultErr`, then it is converted to `Err(e)`.
73+
- `e = ResultErr("error message")`
74+
`Ok(e)` &nbsp;&nbsp; becomes &nbsp;&nbsp; `Result.as_Err(e)`
75+
76+
- Math operations are redirected to `value` and rewrap the solution or concatenate the errors.
77+
- `Ok(value1) + Ok(value2) ``Ok(value1 + value2)`
78+
- `Ok(value1) + Err(e1) ``Err(e1 + "a + b with b as Err")`
79+
- `Err(e1) + Err(e2) ``Err(e1 + "a + b with a and b as Err.")`
80+
81+
- All methods and attributes not associated with `Result` are redirected to `value`.
82+
- `Ok(value).method()` is equivalent to `Ok(value.method())` and
83+
`Ok(value).attrib ` is equivalent to `Ok(value.attrib)`.
8784

88-
## Result Initialization
85+
- `Ok(value).raises()` does NOT become `Ok(value.raises())`
86+
because `Result.raises()` is a native `Result` method.
87+
88+
- Comparisons redirect to comparing the wrapped `value` if `Ok`. But mixed comparisons assume:
89+
`Err(e1) < Ok(value)` and `Err(e1) == Err(e2)` for any `value`, `e1`, and `e2`.
90+
- `Ok(value1) <= Ok(value2) ``value1 <= value2`
91+
- `Ok(value1) < Ok(value2) ``value1 < value2`
92+
- `Err(e1) < Ok(value2) ``True`
93+
- `Ok(value1) < Err(e1) ``False`
94+
- `Err(e1) < Err(e2) ``False`
95+
- `Err(e1) <= Err(e2) ``True`
96+
97+
## Initialization
8998

9099
```python
91100
# Only the first argument is required for all constructors
@@ -107,8 +116,8 @@ res = Err(error_msg, error_code, error_code_group, add_traceback)
107116
# Arguments:
108117
#
109118
# value (Any): The value to wrap in the Ok(value).
110-
# If value is a Result object, then returns value; ignores other args.
111-
# If value is a ResultErr object, then returns Err(value); ignores other args.
119+
# If value is a `Result` object, then returns value; ignores other args.
120+
# If value is a `ResultErr` object, then returns Err(value); ignores other args.
112121
# success (bool, optional): True if success, False for error. Default is True.
113122
# error_msg (Any, optional): If success is False:
114123
# a) and error_msg="", return Err( str(value) )
@@ -129,6 +138,116 @@ res = Err(error_msg, error_code, error_code_group, add_traceback)
129138
#
130139
```
131140

141+
## Attributes and Methods
142+
143+
There are select attributes and methods built into `Result` object. For a full listing please see the Result docstr.
144+
145+
### Attributes
146+
147+
```
148+
is_Ok (bool): True if the result is a success.
149+
is_Err (bool): True if the result is an error.
150+
151+
Ok (any):
152+
If Ok variant, then returns value in Ok(value);
153+
If Err variant, then raises a ResultErr exception.
154+
155+
Err (any):
156+
If Ok variant, then raises a ResultErr exception;
157+
If Err variant, then returns the wrapped ResultErr.
158+
159+
Err_msg (list[str]):
160+
For the Ok(value) variant, returns `[]`.
161+
For the Err(e) variant, returns list of error messages.
162+
163+
Err_traceback (list[list[str]]):
164+
For the Ok(value) variant, returns `[]`.
165+
For the Err(e) variant, returns list of traceback lines.
166+
```
167+
168+
### Methods:
169+
170+
```
171+
raises(add_traceback=False, error_msg="", error_code=1):
172+
If Ok variant, then returns Ok(value);
173+
If Err variant, then raises a ResultErr exception.
174+
175+
unwrap():
176+
Return the wrapped value in Ok(value) or e in Err(e).
177+
178+
unwrap_or(default):
179+
Return the wrapped value in Ok(value) or return default.
180+
181+
expect(error_msg=""):
182+
If Ok variant, then return the wrapped value in Ok(value);
183+
If Err variant, then raises a ResultErr exception and optionally append error_msg to it.
184+
185+
is_Ok_and(bool_ok_func, *args, **kwargs):
186+
True if Ok(value) variant and ok_func(value, *args, **kwargs) returns True, otherwise False.
187+
- If function call fails, then raises exception.
188+
189+
apply(ok_func, *args, **kwargs):
190+
Maps a function to the Result to return a new Result.
191+
For the Ok(value) variant, returns `Ok(ok_func(value, *args, **kwargs))`.
192+
For the Err(e) variant, returns `Err(e)`.
193+
- If ok_func fails, returns `Err("Result.apply exception.)`.
194+
195+
apply_or(default, ok_func, *args, **kwargs):
196+
Maps a function to the Result to return a new Result.
197+
For the Ok(value) variant, returns `Ok(ok_func(value, *args, **kwargs))`.
198+
For the Err(e) variant, returns `Ok(default)`.
199+
- If ok_func fails, returns `Ok(default)`.
200+
201+
apply_map(ok_func, unwrap=False):
202+
Maps a function to the elmenets in value from a Result to return
203+
a new Result containing a list of the function returns.
204+
For the Ok(value) variant, and
205+
value is iterable, returns `Ok(list(map(ok_func, value)))`.
206+
otherwise, returns `Ok([ok_func(value)])`.
207+
For the Err(e) variant, returns `Err(e)`.
208+
- If ok_func fails, returns `Err("Result.apply_map exception.)`.
209+
If unwrap is True, then returns a list or ResultErr.
210+
211+
map(ok_func):
212+
map_or(default, ok_func):
213+
Same functionality as apply and apply_or,
214+
except that if the function call fails, raises an exception.
215+
216+
iter(unwrap=True, expand=False):
217+
Returns an iterator of the value in Ok(value).
218+
if unwrap is False returns iter_wrap(expand)
219+
if unwrap is True returns iter_unwrap(expand)
220+
Always iterates at least once for Ok, and does not iterate for Err.
221+
222+
iter_unwrap(expand=False):
223+
Returns an iterator of the value in Ok(value).
224+
For the Ok(value) variant,
225+
if value is iterable: returns `iter(value)`
226+
else: returns `iter([value])` -> Only one iteration
227+
For the Err(e) variant, returns `iter([])`.
228+
Always iterates at least once for Ok, and does not iterate for Err.
229+
If expand is True, then returns `list(iter_unwrap())`.
230+
231+
iter_wrap(expand=False):
232+
Returns an iterator of the value in Ok(value) that wraps each iterated item in a Result. That is,
233+
`[item for item in Ok(value).iter_wrap()]` ➥ `[Result(item0), Result(item1), Result(item2), ...]`
234+
Always iterates at least once for Ok, and does not iterate for Err.
235+
If expand is True, then returns `list(iter_unwrap())`.
236+
237+
add_Err_msg(error_msg, error_code=1, add_traceback=True)):
238+
For the Ok(value) variant, converts to Err(error_msg).
239+
For the Err(e) variant, adds an error message.
240+
241+
update_result(value, create_new=False, deepcopy=False):
242+
Update Result to hold value. Either updates the current instance or creates a new one.
243+
Return the updated or new Result. If value is not a ResultErr type, then returns Ok(value);
244+
otherwise, returns Err(value).
245+
246+
copy(deepcopy=False):
247+
Create a copy of the Result. If deepcopy=True, the returns Result(deepcopy(value)).
248+
249+
```
250+
132251

133252

134253

ResultContainer/__init__.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -848,9 +848,14 @@ class Result:
848848
849849
Ok (any):
850850
If Ok variant, then returns value in Ok(value);
851-
If Err variant, then raise Err and optionally append error_msg to it.
851+
If Err variant, then raises a ResultErr exception.
852852
Equivalent to the expect() method.
853853
854+
Err (any):
855+
If Ok variant, then raises a ResultErr exception;
856+
If Err variant, then returns the wrapped ResultErr.
857+
Equivalent to the expect_Err() method.
858+
854859
Err_msg (list[str]):
855860
For the Ok(value) variant, returns `[]`.
856861
For the Err(error) variant, returns list of error messages.
@@ -868,25 +873,25 @@ class Result:
868873
869874
Methods:
870875
876+
raises(add_traceback=False, error_msg="", error_code=1):
877+
If Ok variant, then returns Ok(value);
878+
If Err variant, then raises a ResultErr exception`.
879+
Useful for check during chained operations
880+
871881
unwrap():
872-
Return the wrapped value in Ok(value) or error in Err(error).
882+
Return the wrapped value in Ok(value) or e in Err(e).
873883
874884
unwrap_or(default):
875885
Return the wrapped value in Ok(value) or return default.
876886
877887
expect(error_msg=""):
878-
If Ok variant, then returns value in Ok(value);
879-
If Err variant, then raise Err and optionally append error_msg to it.
888+
If Ok variant, then return the wrapped value in Ok(value);
889+
If Err variant, then raises a ResultErr exception and optionally append error_msg to it.
880890
Equivalent to the `Ok` attribute.
881891
882892
expect_Err(ok_msg=""):
883-
If Ok variant, then raise ResultErr(ok_msg);
884-
If Err variant, then returns error in Err(error), which is type ResultErr.
885-
886-
raises(add_traceback=False, error_msg="", error_code=1):
887-
If Ok variant, then returns Ok(value);
888-
If Err variant, then raise Err and optionally include `from exception`.
889-
Useful for check during chained operations
893+
If Ok variant, then raises ResultErr(ok_msg);
894+
If Err variant, then returns e in Err(e), which is type ResultErr.
890895
891896
is_Ok_and(bool_ok_func, *args, **kwargs):
892897
True if Ok(value) variant and ok_func(value, *args, **kwargs) returns True,
@@ -903,7 +908,7 @@ class Result:
903908
Maps a function to the Result to return a new Result.
904909
For the Ok(value) variant, returns `Ok(ok_func(value, *args, **kwargs))`.
905910
For the Err(error) variant, returns `Ok(default)`.
906-
- If ok_func fails, returns `Result(default)`.
911+
- If ok_func fails, returns `Ok(default)`.
907912
908913
apply_or_else(err_func, ok_func, *args, **kwargs):
909914
Maps a function to the Result to return a new Result.

0 commit comments

Comments
 (0)