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
Copy file name to clipboardExpand all lines: README.md
+80-24Lines changed: 80 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,31 +5,35 @@
5
5
</p>
6
6
7
7
8
-
ResultContainer is a python module that contains the `Result` class that mimics the Result Result Enum. The Result Enum is used for situations when errors are expected and are easily handled, or you do not want to have lots of try/except clauses. The 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.
8
+
ResultContainer is a python module with a `Result` class that mimics the [Result Result Enum](https://doc.rust-lang.org/std/result/enum.Result.html). The Result Enum is used for situations when errors are expected and are easily handled, or you do not want to have lots of try/except clauses. The 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 is a `ResultErr` object stored in the `Result` that contains the error messages and traceback information. The `Result` object includes similar methods to the Result Result Enum for inquiry about the state, mapping functions, and passing attributes/methods to the containing `value`.
9
9
10
-
The two Result states are:
10
+
#### `Result` Variants
11
11
12
-
-`Result.Ok(value)`
12
+
-`Ok(value)`
13
13
-`value` is wrapped within an `Ok`.
14
-
-`Result.Err(e)`
15
-
-`e` is an error message wrapped within an `Err`.
14
+
- Constructor: `Result.as_Ok(value)`
15
+
-`Err(e)`
16
+
-`e` is a `ResultErr` object wrapped within an `Err`.
17
+
- Constructor: `Result.as_Err(error_msg)`
16
18
17
19
18
20
#### Properties of the `Result` Variants
19
21
20
-
##### `Result.Ok(value)`
22
+
##### `Ok(value)`
21
23
22
-
- Represents success (non-error state). The `value` is wrapped within the `Ok()`.
24
+
- Represents success (non-error state).
25
+
The `value` is wrapped within the `Ok()`.
23
26
- Can be initialized with `Ok(value)`
24
-
-`Ok(value)` ➥ syntactic-sugar for ➥ `Result.Ok(value)`
27
+
-`Ok(value)` ➥ syntactic-sugar for ➥ `Result.as_Ok(value)`
25
28
- Math operations are redirected to `value` and rewrap the solution or concatenate the errors.
and then move the file `ResultContainer/ResultContainer.py` to wherever you want to use it.
74
+
then rename the file `ResultContainer/__init__.py` to `ResultContainer/ResultContainer.py` and move `ResultContainer.py` to wherever you want to use it.
75
+
76
+
## Result Initialization
77
+
78
+
```python
79
+
# Only the first argument is required for all constructors
80
+
81
+
# Main object signature:
82
+
res = Result(value, success, error_msg, error_code, error_code_group, add_traceback, deepcopy) # Construct either Ok or Er
83
+
84
+
# Classmethod signatures:
85
+
res = Result.as_Ok(value, deepcopy, error_code_group) # Construct Ok variant
86
+
87
+
res = Result.as_Err(error_msg, error_code, error_code_group, add_traceback) # Construct Err variant
88
+
89
+
# Syntact Sugar Constructors:
90
+
res = Ok(value, deepcopy, error_code_group) # Construct Ok variant
91
+
92
+
res = Err(error_msg, error_code, error_code_group, add_traceback) # Construct Err variant
93
+
94
+
# Arguments:
95
+
#
96
+
# value (Any): The value to wrap in the Ok(value).
97
+
# If value is a Result object, then returns value; ignores other args.
98
+
# If value is a ResultErr object, then returns Err(value); ignores other args.
99
+
# success (bool, optional): True if success, False for error. Default is True.
100
+
# error_msg (Any, optional): If success is False:
101
+
# a) and error_msg="", return Err( str(value) )
102
+
# b) otherwise, return Err( str(error_msg) ),
103
+
# if error_msg is listlike, then each item is appended as a separate message.
104
+
# Default is "".
105
+
# error_code (int, optional): Error code associated with the error.
106
+
# Default is `1` for `Unspecified`.
107
+
# A dict of the currently assigned error codes are returned with `Result.error_code()`
108
+
# Note, the code description does not have to match the error_msg.
109
+
# error_code_group (int, optional): Specify the error_codes group to use for code and message flags.
110
+
# Default is 1. Error codes are stored as a class variable,
111
+
# so this is useful if you need different sets of error codes within a program.
112
+
# Most of the time you will never need to use this feature!
113
+
# add_traceback (bool, optional): If True and constructing Err variant, adds traceback information to Err.
114
+
# Default is True.
115
+
# deepcopy (bool, optional): If True, then deepcopy value before wrapping in Ok. Default is False.
116
+
#
117
+
```
118
+
119
+
71
120
72
121
73
122
## Usage
@@ -80,38 +129,45 @@ Below are examples showcasing how to create and interact with a `ResultContainer
80
129
from ResultContainer import Result, Ok, Err
81
130
82
131
# Wrap values in Ok state:
83
-
a = Result(5) # Default is to store as Ok.
132
+
a = Result(5) # Default is to store as Ok (success=True).
84
133
85
134
# Explicitly wrap values in Ok state:
86
-
a = Result.Ok(5)
135
+
a = Result.as_Ok(5)
87
136
88
-
# Wrap values in Ok state, Ok(value) is equalivent to Result.Ok()
137
+
# Wrap values in Ok state, Ok(value) is equalivent to Result.as_Ok()
89
138
a = Ok(5)
90
139
91
-
# Wrap values as an error
92
-
a = Result(5, False) # Flag says it is an error, so stored as Err("5")
93
-
# Note "5" is the error message and NOT the value.
140
+
# Wrap values as an error -------------------------------------------------------------------------------
141
+
a = Result(5, success=False) # Flag says it is an error, so stored as Err("5")
142
+
# Note "5" becomes the error message because error_msg was not provided.
94
143
# Explicitly wrap values in Err state:
95
-
a = Result.Err(5)
144
+
a = Result.as_Err(5)
96
145
97
-
# Wrap values in Err state, Err(value) is equalivent to Result.Err()
146
+
# Wrap values in Err state, Err(value) is equalivent to Result.as_Err()
98
147
a = Err(5)
99
148
100
149
```
101
150
102
151
### Math Operations with a Result
103
152
104
153
```python
154
+
from ResultContainer import Ok
105
155
# Addition, Subtraction, Multiplication and Division
106
-
a =Result.Ok(5)
107
-
b =Result.Ok(50)
156
+
a = Ok(5)
157
+
b = Ok(50)
108
158
c = a + b # c = Ok(55)
109
159
d = c -20# d = Ok(35)
110
160
e = d *2# e = Ok(70)
111
161
e /=10# e = Ok(7)
112
162
f = e /0# f = Err("a / b resulted in an Exception. | ZeroDivisionError: division by zero")
113
163
g = f +1# g = Err("a / b resulted in an Exception. | ZeroDivisionError: division by zero | a + b with a as Err.")
114
164
165
+
# Interally unwraps the value to use its operation, then rewraps the result.
0 commit comments