Skip to main content

Some nice type like things

Project description

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')

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

ricetypes-0.1.7.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ricetypes-0.1.7-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file ricetypes-0.1.7.tar.gz.

File metadata

  • Download URL: ricetypes-0.1.7.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.1

File hashes

Hashes for ricetypes-0.1.7.tar.gz
Algorithm Hash digest
SHA256 9d6b179aa29cc94cac3a4010e411856a972019400afe813317f1f8b0effd5337
MD5 d69aa1dee9410c9d3accb7fdb300bdc4
BLAKE2b-256 725c8c845191fff7708d071de1ade71aac6408fbf0a87f91fe4654736b5e53dd

See more details on using hashes here.

File details

Details for the file ricetypes-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: ricetypes-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.1

File hashes

Hashes for ricetypes-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 37f2f4bbcc968759b314a703aa989b5d0ec50cd84127e37d54002bda4904f109
MD5 0ff206629e47828f51ab520db3a3ad82
BLAKE2b-256 83de283c482f1bb4bd1e52350b34879c5af6b35264bb81c95581ff92e2a84e0e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page