Rust-style enums
Project description
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
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
rust_enum-1.1.5.tar.gz
(2.5 kB
view details)
Built Distribution
File details
Details for the file rust_enum-1.1.5.tar.gz
.
File metadata
- Download URL: rust_enum-1.1.5.tar.gz
- Upload date:
- Size: 2.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 24459f7f70bc463a125f6b8dc203a3ad741c9f1c28a7306e046d741c869095cb |
|
MD5 | 6d15a88fd63a3f334176123dd7b81469 |
|
BLAKE2b-256 | ebe6c4bbf184660f962ce0670715678e42ad57cf24ac577e83d9ff8563a72af7 |
File details
Details for the file rust_enum-1.1.5-py3-none-any.whl
.
File metadata
- Download URL: rust_enum-1.1.5-py3-none-any.whl
- Upload date:
- Size: 2.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | aa10e1a82f23c7f32348aed3c1d90cbd02c7bcfa8b4022634d36a5008677ae93 |
|
MD5 | 1dcef6acb9d319a8300c88bf122dd311 |
|
BLAKE2b-256 | 9c4af46290cd3b5f0d3cd7ef79b843ce3c56f48166d8396ed30221470bd9e95c |