Skip to content

Commit 93926b4

Browse files
authored
Add ok_or and ok_or_else methods. (#15)
Fixes #9
1 parent 87a13c6 commit 93926b4

File tree

9 files changed

+257
-49
lines changed

9 files changed

+257
-49
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838

3939
# Install library
4040
- name: Install maybe
41-
run: pip install --root-user-action=ignore --editable .
41+
run: pip install --root-user-action=ignore --editable .[result]
4242

4343
# Tests
4444
- name: Run tests (excluding pattern matching)

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ PYTHON_PRE_310 := $(shell python -c "import sys; print(sys.version_info < (3, 10
77
install: phony
88
@echo Installing dependencies...
99
python -m pip install --require-virtualenv -r requirements-dev.txt
10-
python -m pip install --require-virtualenv -e .
10+
python -m pip install --require-virtualenv -e .[result]
1111

1212
lint: phony lint-flake lint-mypy
1313

@@ -23,7 +23,7 @@ lint-mypy: phony
2323
mypy
2424

2525
test: phony
26-
pytest
26+
pytest -vv
2727

2828
docs: phony
2929
lazydocs \

README.md

+41-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Maybe
22

3+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
4+
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/rustedpy-maybe?logo=python&logoColor=white)](https://pypi.org/project/rustedpy-maybe/)
5+
[![PyPI](https://img.shields.io/pypi/v/rustedpy-maybe)](https://pypi.org/project/rustedpy-maybe/)
36
[![GitHub Workflow Status (branch)](https://img.shields.io/github/actions/workflow/status/rustedpy/maybe/ci.yml?branch=master)](https://github.com/rustedpy/maybe/actions/workflows/ci.yml?query=branch%3Amaster)
7+
[![Checked with mypy](https://www.mypy-lang.org/static/mypy_badge.svg)](http://mypy-lang.org/)
48
[![Coverage](https://codecov.io/gh/rustedpy/maybe/branch/master/graph/badge.svg)](https://codecov.io/gh/rustedpy/maybe)
59

610
A simple Maybe (Option) type for Python 3 [inspired by Rust](
@@ -20,6 +24,23 @@ Latest GitHub `master` branch version:
2024
pip install git+https://github.com/rustedpy/maybe
2125
```
2226

27+
There are no dependencies outside of the Python standard library. However, if
28+
you wish to use the `Result` conversion methods (see examples in the next
29+
section), you will need to install the `result` extra.
30+
31+
In this case, rather than installing via one of the commands above, you can
32+
install the package with the `result` extra either from the latest release:
33+
34+
```sh
35+
pip install rustedpy-maybe[result]
36+
```
37+
38+
or from the GitHub `master` branch:
39+
40+
```sh
41+
pip install git+https://github.com/rustedpy/maybe[result]
42+
```
43+
2344
## Summary
2445

2546
**Experimental. API subject to change.**
@@ -28,7 +49,7 @@ The idea is that a possible value can be either `Some(value)` or `Nothing()`,
2849
with a way to differentiate between the two. `Some` and `Nothing` are both
2950
classes encapsulating a possible value.
3051

31-
Example usage,
52+
Example usage:
3253

3354
```python
3455
from maybe import Nothing, Some
@@ -39,6 +60,25 @@ assert o.unwrap_or_else(str.upper) == 'yay'
3960
assert n.unwrap_or_else(lambda: 'default') == 'default'
4061
```
4162

63+
There are some methods that support conversion from a `Maybe` to a `Result` type
64+
in the [result library](https://github.com/rustedpy/result/). If you wish to
65+
leverage these methods, you must install the `result` extra as described in the
66+
installation section.
67+
68+
Example usage:
69+
70+
```python
71+
from maybe import Nothing, Some
72+
from result import Ok, Err
73+
74+
o = Some('yay')
75+
n = Nothing()
76+
assert o.ok_or('error') == Ok('yay')
77+
assert o.ok_or_else(lambda: 'error') == Ok('yay')
78+
assert n.ok_or('error') == Err('error')
79+
assert n.ok_or_else(lambda: 'error') == Err('error')
80+
```
81+
4282
## Contributing
4383

4484
These steps should work on any Unix-based system (Linux, macOS, etc) with Python

0 commit comments

Comments
 (0)