Skip to content

Commit ba9d275

Browse files
committed
doc: update readme examples
1 parent 373541c commit ba9d275

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

README.md

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ The two Result states are:
2929
- All attributes and methods not associated with `Result` are redirected to `value`.
3030
- `Ok(value).method()` is equivalent to `Ok(value.method())` and
3131
`Ok(value).attrib` is equivalent to `Ok(value.attrib)`.
32-
- `Ok(value).raised()` does NOT become `Ok(value.raised())` because `Result.raised()` exists.
32+
- `Ok(value).raises()` does NOT become `Ok(value.raises())` because `Result.raises()` exists.
3333
- Comparisons redirect to comparing the wrapped `value` if `Ok`. But mixed comparisons assume:
3434
`Err(e) < Ok(value)` and `Err(e1) == Err(e2)` for any `value`, `e`, `e1`, and `e2`.
3535
- `Ok(value1) < Ok(value2) `&nbsp; &nbsp;&nbsp; `value1 < value`
@@ -54,7 +54,7 @@ There are methods built into `Result` to check if an error has been raised, or t
5454

5555
- **Variants for Success and Failure**: Two variants, `Ok(value)` for successful outcomes, and `Err(e)` for errors that have resulted. Provides a flexible mechanism for chaining operations on the `Ok` value while propagating errors through `Err`.
5656
- **Attribute and Method Transparency**: Automatically passes attributes, methods, and math operations to the value contained within an `Ok`, otherwise propagates the `Err(e)`.
57-
- **Utility Methods**: Implements helper methods for error propagation, transformation, and querying (e.g., `.map()`, `.and_then()`, `.unwrap_or()`, `.expect()`) for concise and readable handling of success and error cases.
57+
- **Utility Methods**: Implements helper methods for error propagation, transformation, and querying (e.g., `.map()`, `.apply()`, `.unwrap_or()`, `.expect()`, `.raises()`) for concise and readable handling of success and error cases.
5858

5959
## Installation
6060
To install the module
@@ -134,51 +134,52 @@ new_dt_sub = dt + timedelta(days=-5) # new_dt = Ok(2024-12-14 12:00:00)
134134
dt_large = Ok(datetime(9999, 12, 31)) # dt_large = Ok(9999-12-31 00:00:00)
135135
bad_dt = dt + timedelta(days=10000) # bad_dt = Err("a + b resulted in an Exception. | OverflowError: date value out of range")
136136

137-
bad_dt.raised() # raises a ResultErr exception
137+
bad_dt.raises() # raises a ResultErr exception
138138
```
139139

140140
### Raising Errors
141141

142142
```python
143143
from ResultContainer import Result, Ok, Err
144144

145-
x = Result(10) # Ok(10)
146-
x /= 0 # Err("a /= b resulted in an Exception. | ZeroDivisionError: division by zero")
145+
# raises() is a powerful check when chaining methods.
146+
# It raises an exception if Err, otherwise returns the original Ok(value)
147+
x = Result(10) # x = Ok(10)
148+
y = x.raises() # y = Ok(10)
149+
x /= 0 # x = Err("a /= b resulted in an Exception. | ZeroDivisionError: division by zero")
147150

148-
x.raised()
149-
150-
# The exception raised is
151-
# (note that math operations do not log line numbers that the error occurs):
151+
y = x.raises() # Raises the following exception:
152152

153153
# Traceback (most recent call last):
154154
# File "example.py", line 7, in <module>
155-
# x.raised()
155+
# x.raises()
156156
# ^^^^^^^^^^
157-
# File "ResultContainer/ResultContainer.py", line 957, in raised
157+
# File "ResultContainer/ResultContainer.py", line 957, in raises
158158
# raise self._Err
159159
# ResultErr:
160160
# [1] a /= b resulted in an Exception.
161161
# [12] ZeroDivisionError: division by zero
162162
```
163163

164+
164165

165-
```python
166-
from ResultContainer import Result, Ok, Err
167-
from datetime import datetime, timedelta
168-
169-
dt = Result(datetime(9999, 12, 31))
170-
171-
bad_dt = dt + timedelta(days=10000)
172166

173-
bad_dt.raised()
174-
175-
# Not the exception says it occured on `line 6`
176-
# despite being called on `line 8`
167+
```python
168+
1 | from ResultContainer import Result, Ok, Err
169+
2 | from datetime import datetime, timedelta
170+
3 |
171+
4 | dt = Result(datetime(9999, 12, 31))
172+
5 |
173+
6 | bad_dt = dt + timedelta(days=10000)
174+
7 |
175+
8 | bad_dt.raises()
176+
# Raises the following exception.
177+
# Note the exception says it occured on `line 6` despite being called on `line 8`
177178

178179
# Traceback (most recent call last):
179180
# File "example.py", line 8, in <module>
180-
# bad_dt.raised()
181-
# File "ResultContainer/ResultContainer.py", line 957, in raised
181+
# bad_dt.raises()
182+
# File "ResultContainer/ResultContainer.py", line 957, in raises
182183
# raise self._Err
183184
# ResultErr:
184185
# File "ResultContainer/example.py", line 6, in <module>
@@ -194,10 +195,11 @@ bad_dt.raised()
194195
from math import sqrt
195196
# to use an external function, like sqrt
196197
# It must be passed to either apply or map or extracted with expect.
197-
a = Ok(9) # Ok(9)
198-
b = a.map(sqrt) # Ok(3.0)
199-
c = Ok(-9) # Ok(-9)
200-
d = c.map(sqrt) # Err("Result.apply exception. | ValueError: math domain error")
198+
# apply converts Ok to Err if the func fails, while map raises an exception.
199+
a = Ok(9) # Ok(9)
200+
b = a.apply(sqrt) # Ok(3.0)
201+
c = Ok(-9) # Ok(-9)
202+
d = c.apply(sqrt) # Err("Result.apply exception. | ValueError: math domain error")
201203
e = sqrt(c.expect()) # raises an error
202204

203205
plus1 = lambda x: x + 1

0 commit comments

Comments
 (0)