Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ok_or and ok_or_else methods. #15

Merged
merged 6 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]
chuckwondo marked this conversation as resolved.
Show resolved Hide resolved

# 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