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


whilte = 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.2.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

ricetypes-0.1.2-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ricetypes-0.1.2.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for ricetypes-0.1.2.tar.gz
Algorithm Hash digest
SHA256 45d1cfbe48ab0ac0a67e3f787c8b5937dd723eb1f7664c0767f4be357bf976ef
MD5 04e110b1d224123c2ba3a8cd1ce545c3
BLAKE2b-256 2424978bdb9a5d662a267b865919a9d6361a211c3f5653b942f209863720d8e6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ricetypes-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for ricetypes-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 696ae0ed6573c07ac4980d8dfea57cc242ea4ed8bce5b6dd170e72b22b12019a
MD5 6dba1f3b269b03919112fb10091f9768
BLAKE2b-256 61b0f59fc1cbb2b7994a00178c36926de91a261738d739e71468b70c91e7aa27

See more details on using hashes here.

Supported by

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