Skip to content

Commit

Permalink
Add ok_or and ok_or_else methods. (#15)
Browse files Browse the repository at this point in the history
Fixes #9
  • Loading branch information
chuckwondo authored Aug 20, 2024
1 parent 87a13c6 commit 93926b4
Show file tree
Hide file tree
Showing 9 changed files with 257 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:

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

# Tests
- name: Run tests (excluding pattern matching)
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ PYTHON_PRE_310 := $(shell python -c "import sys; print(sys.version_info < (3, 10
install: phony
@echo Installing dependencies...
python -m pip install --require-virtualenv -r requirements-dev.txt
python -m pip install --require-virtualenv -e .
python -m pip install --require-virtualenv -e .[result]

lint: phony lint-flake lint-mypy

Expand All @@ -23,7 +23,7 @@ lint-mypy: phony
mypy

test: phony
pytest
pytest -vv

docs: phony
lazydocs \
Expand Down
42 changes: 41 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Maybe

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/rustedpy-maybe?logo=python&logoColor=white)](https://pypi.org/project/rustedpy-maybe/)
[![PyPI](https://img.shields.io/pypi/v/rustedpy-maybe)](https://pypi.org/project/rustedpy-maybe/)
[![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)
[![Checked with mypy](https://www.mypy-lang.org/static/mypy_badge.svg)](http://mypy-lang.org/)
[![Coverage](https://codecov.io/gh/rustedpy/maybe/branch/master/graph/badge.svg)](https://codecov.io/gh/rustedpy/maybe)

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

There are no dependencies outside of the Python standard library. However, if
you wish to use the `Result` conversion methods (see examples in the next
section), you will need to install the `result` extra.

In this case, rather than installing via one of the commands above, you can
install the package with the `result` extra either from the latest release:

```sh
pip install rustedpy-maybe[result]
```

or from the GitHub `master` branch:

```sh
pip install git+https://github.com/rustedpy/maybe[result]
```

## Summary

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

Example usage,
Example usage:

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

There are some methods that support conversion from a `Maybe` to a `Result` type
in the [result library](https://github.com/rustedpy/result/). If you wish to
leverage these methods, you must install the `result` extra as described in the
installation section.

Example usage:

```python
from maybe import Nothing, Some
from result import Ok, Err

o = Some('yay')
n = Nothing()
assert o.ok_or('error') == Ok('yay')
assert o.ok_or_else(lambda: 'error') == Ok('yay')
assert n.ok_or('error') == Err('error')
assert n.ok_or_else(lambda: 'error') == Err('error')
```

## Contributing

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

0 comments on commit 93926b4

Please sign in to comment.