1
1
# Maybe
2
2
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/ )
3
6
[ ![ 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/ )
4
8
[ ![ Coverage] ( https://codecov.io/gh/rustedpy/maybe/branch/master/graph/badge.svg )] ( https://codecov.io/gh/rustedpy/maybe )
5
9
6
10
A simple Maybe (Option) type for Python 3 [ inspired by Rust] (
@@ -20,6 +24,23 @@ Latest GitHub `master` branch version:
20
24
pip install git+https://github.com/rustedpy/maybe
21
25
```
22
26
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
+
23
44
## Summary
24
45
25
46
** Experimental. API subject to change.**
@@ -28,7 +49,7 @@ The idea is that a possible value can be either `Some(value)` or `Nothing()`,
28
49
with a way to differentiate between the two. ` Some ` and ` Nothing ` are both
29
50
classes encapsulating a possible value.
30
51
31
- Example usage,
52
+ Example usage:
32
53
33
54
``` python
34
55
from maybe import Nothing, Some
@@ -39,6 +60,25 @@ assert o.unwrap_or_else(str.upper) == 'yay'
39
60
assert n.unwrap_or_else(lambda : ' default' ) == ' default'
40
61
```
41
62
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
+
42
82
## Contributing
43
83
44
84
These steps should work on any Unix-based system (Linux, macOS, etc) with Python
0 commit comments