ricetypes
This is a little library that defines some nice type constructs that I like to have.
Install
pip install ricetypes
Enums (Discriminated Unions)
I have implemented something approximating rust enums.
from ricetypes import Enum, Scalar_Variant, Struct_Variant
@Enum
class Color:
Red: Scalar_Variant
Blue: Scalar_Variant
Green: Scalar_Variant
RGB: Struct_Variant(int, int, int, alpha=float)
r = Color.Red
b = Color.Blue
g = Color.Green
white = Color.RGB(100,100,100, alpha=.1)
print(white.alpha)
print(white._0, white._1, white_2)
print(white.tuple)
print(white.dict)
match r:
case Color.Red:
print('red')
case Color.Blue:
print('blue')
case Color.Green:
print('green')
# unfortunatly you cant use Struct_Variants in a match statment
Result
It includes the Result type:
from ricetypes import Result
r = Result.Ok(10)
from math import sqrt
r.map(sqrt).unwrap()
r = Result.Error('sad').maperr(str.upper)
if r.error:
print(r._error)
try:
r.with_exception(KeyError).unwrap()
except KeyError as e:
print(e)
try:
r.unwrap()
except Exception as e:
print(e)
# We can chain functions together using map
inc = lambda n: n+1
Result.Ok(0).map(inc).map(inc).map(sqrt)
# And we can chain functions that return an Option together using a bind
def foo(x: int, y:int):
if x < y:
return Result.Ok(y)
return Result.Error(f'y must be bigger then x but x={x}, y={y}')
Result.Ok(0).bind(foo, 10).bind(foo,30).bind(foo,20).or_else(-1)
Option
from ricetypes import Option
op = Option.Some('value')
if op.something:
print(op.value)
op.map(str.upper)
op = Option.Nothing
op.or_else('hi')
Metadata
Release files for ricetypes 0.1.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| ricetypes-0.1.7.tar.gz | 4.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ricetypes-0.1.7-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.0 kB
Release files / ricetypes-0.1.7.tar.gz
| Download URL | ricetypes-0.1.7.tar.gz |
|---|---|
| Size | 4.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9d6b179aa29cc94cac3a4010e411856a972019400afe813317f1f8b0effd5337
|
|
BLAKE2b-256 checksum How to use checksums |
725c8c845191fff7708d071de1ade71aac6408fbf0a87f91fe4654736b5e53dd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.13.1
|
Release files / ricetypes-0.1.7-py3-none-any.whl
| Download URL | ricetypes-0.1.7-py3-none-any.whl |
|---|---|
| Size | 4.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
37f2f4bbcc968759b314a703aa989b5d0ec50cd84127e37d54002bda4904f109
|
|
BLAKE2b-256 checksum How to use checksums |
83de283c482f1bb4bd1e52350b34879c5af6b35264bb81c95581ff92e2a84e0e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.13.1
|