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.2.tar.gz
(2.4 kB
view hashes)
Built Distribution
Close
Hashes for rust_enum-1.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bf5e9dd7041e5218c731dd346f2aa2e27ec4ddb8a8ee3003618cf6d5c32339ae |
|
MD5 | 7e720ad6fa384ccc87bd3e637f4f1ef3 |
|
BLAKE2b-256 | dbe5032c505511d73a8a37a1c3d95e2fa8d99fc5c326cfd20a6db5826474595b |