Skip to content

Commit b717b3e

Browse files
committed
doc: readme.md examples and constructor discription
1 parent 3a16189 commit b717b3e

1 file changed

Lines changed: 80 additions & 24 deletions

File tree

README.md

Lines changed: 80 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,35 @@
55
</p>
66

77

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

10-
The two Result states are:
10+
#### `Result` Variants
1111

12-
- `Result.Ok(value)`
12+
- `Ok(value)`
1313
- `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)`
1618

1719

1820
#### Properties of the `Result` Variants
1921

20-
##### `Result.Ok(value)`
22+
##### `Ok(value)`
2123

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()`.
2326
- Can be initialized with `Ok(value)`
24-
- `Ok(value)` &nbsp;&nbsp; syntactic-sugar for &nbsp;&nbsp; `Result.Ok(value)`
27+
- `Ok(value)` &nbsp;&nbsp; syntactic-sugar for &nbsp;&nbsp; `Result.as_Ok(value)`
2528
- Math operations are redirected to `value` and rewrap the solution or concatenate the errors.
2629
- `Ok(value1) + Ok(value2) `&nbsp; &nbsp;&nbsp; `Ok(value1 + value2)`
2730
- `Ok(value1) + Err(e) `&nbsp; &nbsp;&nbsp; `Err(e + "a + b with b as Err")`
2831
- `Err(e1) + Err(e2) `&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; `Err(e1 + "a + b with a and b as Err.")`
2932
- All attributes and methods not associated with `Result` are redirected to `value`.
3033
- `Ok(value).method()` is equivalent to `Ok(value.method())` and
3134
`Ok(value).attrib` is equivalent to `Ok(value.attrib)`.
32-
- `Ok(value).raises()` does NOT become `Ok(value.raises())` because `Result.raises()` exists.
35+
- `Ok(value).raises()` does NOT become `Ok(value.raises())`
36+
because `Result.raises()` is a native `Result` method.
3337
- Comparisons redirect to comparing the wrapped `value` if `Ok`. But mixed comparisons assume:
3438
`Err(e) < Ok(value)` and `Err(e1) == Err(e2)` for any `value`, `e`, `e1`, and `e2`.
3539
- `Ok(value1) < Ok(value2) `&nbsp; &nbsp;&nbsp; `value1 < value`
@@ -38,14 +42,14 @@ The two Result states are:
3842
- `Err(e1) < Err(e2) ` &nbsp;&nbsp; `False`
3943
- `Err(e1) <= Err(e2) ` &nbsp;&nbsp; `True`
4044

41-
##### `Result.Err(e)`:
45+
##### `Err(e)`:
4246

43-
- Represents a failure (error-state) and contains one more more error messages.
47+
- Represents a failure (error-state) and contains `e` as a `ResultErr` object
48+
that stores error messages and traceback information.
4449
- Can be initialized with `Err(error_msg)`
45-
- `Err(e)` &nbsp;&nbsp; syntactic-sugar for &nbsp;&nbsp; `Result.Err(e)`
50+
- `Err(e)` &nbsp;&nbsp; syntactic-sugar for &nbsp;&nbsp; `Result.as_Err(e)`
4651

4752
- If an `Ok(value)` operation fails, then it is converted to an `Err(e)`, where `e` stores the error message.
48-
- `e` contains an error message, error code, and traceback information where the error occured.
4953
- Any operation on `Err(e)` results in another error message being appended to `e`.
5054

5155
There are methods built into `Result` to check if an error has been raised, or the unwrap the value/error to get its contents.
@@ -67,7 +71,52 @@ or you can clone the respository with
6771
```bash
6872
git clone https://github.com/ScottBoyce-Python/ResultContainer.git
6973
```
70-
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+
71120

72121

73122
## Usage
@@ -80,38 +129,45 @@ Below are examples showcasing how to create and interact with a `ResultContainer
80129
from ResultContainer import Result, Ok, Err
81130

82131
# 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).
84133

85134
# Explicitly wrap values in Ok state:
86-
a = Result.Ok(5)
135+
a = Result.as_Ok(5)
87136

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()
89138
a = Ok(5)
90139

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.
94143
# Explicitly wrap values in Err state:
95-
a = Result.Err(5)
144+
a = Result.as_Err(5)
96145

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()
98147
a = Err(5)
99148

100149
```
101150

102151
### Math Operations with a Result
103152

104153
```python
154+
from ResultContainer import Ok
105155
# Addition, Subtraction, Multiplication and Division
106-
a = Result.Ok(5)
107-
b = Result.Ok(50)
156+
a = Ok(5)
157+
b = Ok(50)
108158
c = a + b # c = Ok(55)
109159
d = c - 20 # d = Ok(35)
110160
e = d * 2 # e = Ok(70)
111161
e /= 10 # e = Ok(7)
112162
f = e / 0 # f = Err("a / b resulted in an Exception. | ZeroDivisionError: division by zero")
113163
g = f + 1 # g = Err("a / b resulted in an Exception. | ZeroDivisionError: division by zero | a + b with a as Err.")
114164

165+
# Interally unwraps the value to use its operation, then rewraps the result.
166+
# This results in the following behaivor:
167+
x = Ok([1, 2, 3])
168+
y = Ok([4, 5, 6, 7])
169+
z = x + y # z = Ok([1, 2, 3, 4, 5, 6, 7])
170+
115171
```
116172

117173
### Wrapping Objects

0 commit comments

Comments
 (0)