Rust-style enums for Python
Easily-defined enumerations that can contain data and be matched.
Here they are:
@enum
class DivisionResult:
Undefined = Case()
Some = Case(number=float)
def divide(a: float, b: float) -> DivisionResult:
if b == 0: return DivisionResult.Undefined()
return DivisionResult.Some(a / b)
match divide(3, 3):
case DivisionResult.Some(n): assert n == 1
case _: assert False
Also Option is implemented, so you can do it even faster in most cases:
def divide(a: float, b: float) -> Option[float]:
if b == 0: return Option.Nothing()
return Option.Some(a / b)
assert divide(6, 2).unwrap() == 3
assert divide(6, 2).unwrap_or(None) == 3
assert divide(6, 0).unwrap_or(None) is None
assert divide(6, 2).map(lambda v: v * 3) == Option.Some(9)
assert divide(6, 2).and_then(lambda v: divide(v, 3)) == Option.Some(1)
Installation
pip install rust_enum
Metadata
Release files for rust-enum 1.1.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| rust_enum-1.1.5.tar.gz | 2.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| rust_enum-1.1.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 4.9 kB
Release files / rust_enum-1.1.5.tar.gz
| Download URL | rust_enum-1.1.5.tar.gz |
|---|---|
| Size | 2.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
24459f7f70bc463a125f6b8dc203a3ad741c9f1c28a7306e046d741c869095cb
|
|
BLAKE2b-256 checksum How to use checksums |
ebe6c4bbf184660f962ce0670715678e42ad57cf24ac577e83d9ff8563a72af7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.0 CPython/3.9.12
|
Release files / rust_enum-1.1.5-py3-none-any.whl
| Download URL | rust_enum-1.1.5-py3-none-any.whl |
|---|---|
| Size | 2.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
aa10e1a82f23c7f32348aed3c1d90cbd02c7bcfa8b4022634d36a5008677ae93
|
|
BLAKE2b-256 checksum How to use checksums |
9c4af46290cd3b5f0d3cd7ef79b843ce3c56f48166d8396ed30221470bd9e95c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.0 CPython/3.9.12
|