Python Rusty Result Library: 在Python中使用Rust风格的Result类型
Project description
PyRsult 开发文档
版本:0.1.0
作者:fexcode
仓库:https://github.com/fexcode/pyrsult
1. 快速开始
pip install git+https://github.com/fexcode/pyrsult.git
from pyrsult import Result, Option
# 1.1 Result:要么成功,要么失败
def div(a: int, b: int) -> Result[float, str]:
if b == 0:
return Result.Failure("divide by zero")
return Result.Success(a / b)
print(div(10, 2).unwrap()) # 5.0
print(div(10, 0).unwrap_or(-1)) # -1
# 1.2 Option:要么有值,要么空
def find(lst, target):
for v in lst:
if v == target:
return Option.Some(v)
return Option.Nothing()
find([1, 2, 3], 2).map(lambda x: x * 10).unwrap_or(0) # 20
2. Result 接口速查
所有方法均为 链式/懒求值 安全设计。
| 接口 | 说明 | 代码示例 |
|---|---|---|
is_ok() |
判别成功 | div(4,2).is_ok() # True |
is_err() |
判别失败 | div(4,0).is_err() # True |
unwrap() |
取成功值,失败则抛 | div(4,2).unwrap() # 2.0 |
unwrap_or(default) |
失败时给默认值 | div(4,0).unwrap_or(-1) # -1 |
unwrap_or_else(f: E->T) |
失败时懒求值 | div(4,0).unwrap_or_else(lambda e: 999) # 999 |
expect(msg) |
失败时自定义异常 | div(4,0).expect("bad") # ValueError: bad |
map(f: T->U) |
成功链式转换 | div(10,2).map(lambda x: x*2).unwrap() # 10.0 |
map_err(f: E->F) |
失败链式转换 | div(10,0).map_err(str.upper).unwrap_or(0) # 0 |
and_then(f: T->Result[U,E]) |
扁平链式(Rust flat_map) | div(10,2).and_then(lambda x: div(x,2)).unwrap() # 2.5 |
or_else(f: E->Result[T,E]) |
失败时补救 | div(10,0).or_else(lambda e: Result.Success(0)).unwrap() # 0 |
match |
模式匹配(3.10+) | 见下方示例 |
模式匹配示例:
match div(10, 0):
case Success(v):
print("ok", v)
case Failure(e):
print("err", e) # → err divide by zero
3. Option 接口速查
| 接口 | 说明 | 代码示例 |
|---|---|---|
is_some() / is_nothing() |
判别 | Option.Some(7).is_some() # True |
unwrap() |
取值,空抛 | Some(7).unwrap() # 7 |
unwrap_or(default) |
空给默认值 | Nothing().unwrap_or(99) # 99 |
unwrap_or_else(f:()->T) |
懒求值 | Nothing().unwrap_or_else(lambda: 99) # 99 |
map(f: T->U) |
有值则映射 | Some(7).map(str).unwrap() # '7' |
and_then(f: T->Option[U]) |
扁平链 | Some(5).and_then(lambda x: Some(x*2)) # Some(10) |
filter(pred) |
条件过滤 | Some(7).filter(lambda x: x>10) # Nothing |
or_(optb) / or_else(f) |
备选 | `Nothing() |
ok_or(err) |
转 Result | Some(5).ok_or("empty") # 5 |
iter() |
0/1 迭代器 | list(Some(3).iter()) # [3] |
__bool__ |
真值测试 | if Some(3): ... |
| ` | ` 运算符 | 或合并 |
构造快捷方式:
Option.Auto(None) # Nothing()
Option.Auto(42) # Some(42)
4. 组合实战:读取配置
import os
from pyrsult import Result, Option
def env_int(key: str) -> Result[int, str]:
val = os.getenv(key)
return (
Option.Auto(val)
.map(int)
.ok_or(f"{key} not a valid int")
)
port = env_int("PORT").unwrap_or(8080) # 配置缺失自动兜底
欢迎 PR & Issue!
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 pyrsult-0.2.2.tar.gz.
File metadata
- Download URL: pyrsult-0.2.2.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d69d9ae0ff7124aa84ddda6d25c55e49ffd8b8e35ee3e495f268a78d7cbf9687
|
|
| MD5 |
d3338e679b0d5f1b2844b4eccb892495
|
|
| BLAKE2b-256 |
83d1f3d4f718a196adf25f7b78e646b070f309536f25126891d9a4853c207ba2
|
Provenance
The following attestation bundles were made for pyrsult-0.2.2.tar.gz:
Publisher:
python-publish.yml on fexcode/pyrsult
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyrsult-0.2.2.tar.gz -
Subject digest:
d69d9ae0ff7124aa84ddda6d25c55e49ffd8b8e35ee3e495f268a78d7cbf9687 - Sigstore transparency entry: 714978657
- Sigstore integration time:
-
Permalink:
fexcode/pyrsult@1c117148127ef18dbbbcdff780c90515a4ca1fc7 -
Branch / Tag:
refs/tags/v0.2.2 - Owner: https://github.com/fexcode
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@1c117148127ef18dbbbcdff780c90515a4ca1fc7 -
Trigger Event:
release
-
Statement type:
File details
Details for the file pyrsult-0.2.2-py3-none-any.whl.
File metadata
- Download URL: pyrsult-0.2.2-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9c8dfedd837f3c9a42fb2df7de29a731b989d60b71c160ea3a6eaa2824ede22d
|
|
| MD5 |
227a20c337dac4a007b85d1cd17da9b3
|
|
| BLAKE2b-256 |
da5f6e2e87fd7447b68d7f22ecaf3d3a08b47b0a56f977f83f4720dcaf8d6cda
|
Provenance
The following attestation bundles were made for pyrsult-0.2.2-py3-none-any.whl:
Publisher:
python-publish.yml on fexcode/pyrsult
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyrsult-0.2.2-py3-none-any.whl -
Subject digest:
9c8dfedd837f3c9a42fb2df7de29a731b989d60b71c160ea3a6eaa2824ede22d - Sigstore transparency entry: 714978658
- Sigstore integration time:
-
Permalink:
fexcode/pyrsult@1c117148127ef18dbbbcdff780c90515a4ca1fc7 -
Branch / Tag:
refs/tags/v0.2.2 - Owner: https://github.com/fexcode
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@1c117148127ef18dbbbcdff780c90515a4ca1fc7 -
Trigger Event:
release
-
Statement type: