a library to implement rust like enums
Project description
alg_types
alg_types
is my attempt, more of a 'hack' to implement rust like enums in python.
Examples
Here is an attempt to implement rust result using it
from typing import TypeVar, Generic, Callable, Self
from alg_types import alg, variant
T,U = TypeVar("T"), TypeVar("U")
@alg
class Result(Generic[T,U]):
@variant
def Ok(x:T): ...
@variant
def Err(st:U): ...
def is_ok(self):
return isinstance(self, Result.Ok)
def is_err(self):
return isinstance(self, Result.Err)
def map(self, f: Callable[[T], T]) -> Self:
match self:
case Result.Ok(x):
return Result.Ok(f(x))
case Result.Err(st):
return Result.Err(st)
def map_err(self, f: Callable[[U], U]) -> Self:
match self:
case Result.Ok(x):
return Result.Ok(x)
case Result.Err(st):
return Result.Err(f(st))
def unwrap(self) -> T:
match self:
case Result.Ok(x):
return x
case Result.Err(st):
raise Exception(st)
def unwrap_err(self) -> U:
match self:
case Result.Ok(x):
raise Exception(x)
case Result.Err(st):
return st
def unwrap_or(self, default: T) -> T:
match self:
case Result.Ok(x):
return x
case Result.Err(st):
return default
def unwrap_or_else(self, f: Callable[[], T]) -> T:
match self:
case Result.Ok(x):
return x
case Result.Err(st):
return f()
def expect(self, msg: str) -> T:
match self:
case Result.Ok(x):
return x
case Result.Err(_st):
raise Exception(msg)
x = Result.Err("1").map_err(lambda x: x + '1')
print(x)
outputs: Result.Err(st='11')
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
alg_types-0.1.2.tar.gz
(1.5 kB
view hashes)
Built Distribution
Close
Hashes for alg_types-0.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 29e0a6601e7f5dfc70110d1b2f41fe7e9837f21d8dfbc121f300764a49170f25 |
|
MD5 | a5520ec7ec70f30eefa09a24f0ecac2b |
|
BLAKE2b-256 | 1c1507fd60825833f62aa0acfa498e7ecef27a2def3ddc6b2b6cfa01fed938e0 |