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 details)
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 alg_types-0.1.2.tar.gz.
File metadata
- Download URL: alg_types-0.1.2.tar.gz
- Upload date:
- Size: 1.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.12.4 Linux/6.9.7-200.fc40.x86_64
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fe4c8963287d9eef3cda5107eed496c5c62a926a3ed3958a057f45198e1a9614
|
|
| MD5 |
ecae18729d188694c24bedf5d415f627
|
|
| BLAKE2b-256 |
89e7fce16624399971b5b54cdbfec7ace2b313c1d1e47febd42cfb6e068b404a
|
File details
Details for the file alg_types-0.1.2-py3-none-any.whl.
File metadata
- Download URL: alg_types-0.1.2-py3-none-any.whl
- Upload date:
- Size: 1.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.12.4 Linux/6.9.7-200.fc40.x86_64
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
29e0a6601e7f5dfc70110d1b2f41fe7e9837f21d8dfbc121f300764a49170f25
|
|
| MD5 |
a5520ec7ec70f30eefa09a24f0ecac2b
|
|
| BLAKE2b-256 |
1c1507fd60825833f62aa0acfa498e7ecef27a2def3ddc6b2b6cfa01fed938e0
|