Rustipy your python project
Project description
Rustipy
Introduction
This package introduces Option and Result, which are basic functional monad types, to your project!
Installation
pip install rustipy ## with pip
uv add rustipy ## with uv
Usage
Result example
check the test file for more examples
from rustipy.result import Result, Ok, Err
def square(x: int) -> int:
return x * x
def ok_if_positive(x: int) -> Result[int, str]:
if x > 0:
return Ok(x)
else:
return Err("Not positive")
def err_to_default_ok(e: str) -> Result[int, str]:
return Ok(DEFAULT_VALUE)
def len_str(s: str) -> int:
return len(s)
OK_VALUE = 100
DEFAULT_VALUE = 0
def test_flatten():
ok_ok: Result[Result[int, str], str] = Ok(Ok(OK_VALUE))
ok_err: Result[Result[int, str], str] = Ok(Err(ERR_VALUE))
err_outer: Result[Result[int, str], str] = Err(OTHER_ERR_VALUE)
ok_not_result: Result[int, str] = Ok(123)
assert ok_ok.flatten() == Ok(OK_VALUE)
assert ok_err.flatten() == Err(ERR_VALUE)
assert err_outer.flatten() == Err(OTHER_ERR_VALUE)
with pytest.raises(TypeError):
ok_not_result.flatten()
def test_chaining_err_path():
res = (
Ok(-5)
.map(square)
.and_then(ok_if_positive) # Ok(25)
.and_then(err_if_negative) # Ok(25)
.and_then(lambda x: Err("Force Err")) # Err("Force Err")
.or_else(err_to_default_ok) # Ok(DEFAULT_VALUE)
.unwrap()
)
assert res == DEFAULT_VALUE
Option example
check the test file for more examples
from rustipy.option import Option, Some, NONE
from tests.test_result import OK_VALUE
def int_to_some_str(x: int) -> Option[str]:
return Some(str(x))
def int_to_nothing_if_odd(x: int) -> Option[int]:
return NONE if x % 2 != 0 else Some(x)
def int_to_some_str(x: int) -> Option[str]:
return Some(str(x))
def int_to_nothing_if_odd(x: int) -> Option[int]:
return NONE if x % 2 != 0 else Some(x)
SOME_VALUE = 123
def test_and_then():
some_even: Option[int] = Some(10)
some_odd: Option[int] = Some(5)
nothing: Option[int] = NONE
assert some_even.and_then(int_to_some_str) == Some("10")
assert some_odd.and_then(int_to_some_str) == Some("5")
assert nothing.and_then(int_to_some_str) == NONE
assert some_even.and_then(int_to_nothing_if_odd) == Some(10)
assert some_odd.and_then(int_to_nothing_if_odd) == NONE
assert nothing.and_then(int_to_nothing_if_odd) == NONE
def test_inspect():
inspected_val = None
def inspector(x: int):
nonlocal inspected_val
inspected_val = x * 2
some: Option[int] = Some(SOME_VALUE)
nothing: Option[int] = NONE
assert some.inspect(inspector) is some # Returns self
assert inspected_val == SOME_VALUE * 2
inspected_val = None # Reset
assert nothing.inspect(inspector) is nothing # Returns self
assert inspected_val is None
def test_type_guards():
some: Option[int] = Some(OK_VALUE)
nothing: Option[int] = NONE
if is_some(some):
assert some.unwrap() == OK_VALUE
else:
pytest.fail("is_some failed for Some value")
if is_nothing(some):
pytest.fail("is_nothing succeeded for Some value")
if is_some(nothing):
pytest.fail("is_some succeeded for Nothing value")
if is_nothing(nothing):
# Can't unwrap Nothing, just check identity
assert nothing is NONE
else:
pytest.fail("is_nothing failed for Nothing value")
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file rustipy-0.2.2.tar.gz.
File metadata
- Download URL: rustipy-0.2.2.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0dc93685e545211b4ad2090e0fa5792afbdbf5fc70d225741ac4af0675e1c3f1
|
|
| MD5 |
743285766767c56ab6b94ef1f08c2500
|
|
| BLAKE2b-256 |
2763560a29af3aa5ad9dd9acf3ead12dff19abc04dabe85a759bdacbb0faf63f
|
Provenance
The following attestation bundles were made for rustipy-0.2.2.tar.gz:
Publisher:
release.yml on loldruger/Rustipy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rustipy-0.2.2.tar.gz -
Subject digest:
0dc93685e545211b4ad2090e0fa5792afbdbf5fc70d225741ac4af0675e1c3f1 - Sigstore transparency entry: 2050759385
- Sigstore integration time:
-
Permalink:
loldruger/Rustipy@1bb63ff90670095773715df340c0969967ebd7ff -
Branch / Tag:
refs/tags/v0.2.2 - Owner: https://github.com/loldruger
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@1bb63ff90670095773715df340c0969967ebd7ff -
Trigger Event:
push
-
Statement type:
File details
Details for the file rustipy-0.2.2-py3-none-any.whl.
File metadata
- Download URL: rustipy-0.2.2-py3-none-any.whl
- Upload date:
- Size: 8.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5f109ceae38f751c7233299335fede3353d4f4d41ca6d9f2356b824caf4e0b7a
|
|
| MD5 |
2439d7a427156a150b488aea5653491e
|
|
| BLAKE2b-256 |
e15f6a87a73d721b9083f39f1acce5f222839008d47faec1d1df8b6a17ede95c
|
Provenance
The following attestation bundles were made for rustipy-0.2.2-py3-none-any.whl:
Publisher:
release.yml on loldruger/Rustipy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rustipy-0.2.2-py3-none-any.whl -
Subject digest:
5f109ceae38f751c7233299335fede3353d4f4d41ca6d9f2356b824caf4e0b7a - Sigstore transparency entry: 2050759450
- Sigstore integration time:
-
Permalink:
loldruger/Rustipy@1bb63ff90670095773715df340c0969967ebd7ff -
Branch / Tag:
refs/tags/v0.2.2 - Owner: https://github.com/loldruger
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@1bb63ff90670095773715df340c0969967ebd7ff -
Trigger Event:
push
-
Statement type: