ricetypes


Namericetypes JSON
Version 0.1.5 PyPI version JSON
download
home_pageNone
SummarySome nice type like things
upload_time2024-11-28 18:03:38
maintainerNone
docs_urlNone
authorNone
requires_python>=3.9
licenseMIT License
keywords typing result option enum
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # ricetypes

This is a little library that defines some nice type constructs that I like to have.

## Install

```sh
pip install ricetypes
```

### Enums (Discriminated Unions)

I have implemented something approximating rust enums.

``` python
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:
``` python
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
``` python
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')

```



            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "ricetypes",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "typing, result, option, enum",
    "author": null,
    "author_email": "Maxwell Gisborne <maxwell.gisborne@plymouth.ac.uk>",
    "download_url": "https://files.pythonhosted.org/packages/b4/75/3bd6dab55658ef04c4a46b211b9d51c44baa602b92d621800b7cd31c518a/ricetypes-0.1.5.tar.gz",
    "platform": null,
    "description": "# ricetypes\n\nThis is a little library that defines some nice type constructs that I like to have.\n\n## Install\n\n```sh\npip install ricetypes\n```\n\n### Enums (Discriminated Unions)\n\nI have implemented something approximating rust enums.\n\n``` python\nfrom ricetypes import Enum, Scalar_Variant, Struct_Variant\n\n@Enum\nclass Color:\n    Red:   Scalar_Variant\n    Blue:  Scalar_Variant\n    Green: Scalar_Variant\n\n    RGB: Struct_Variant(int, int, int, alpha=float)\n\n\nr = Color.Red\nb = Color.Blue\ng = Color.Green\n\n\nwhilte = Color.RGB(100,100,100, alpha=.1)\n\nprint(white.alpha)\nprint(white._0, white._1, white_2)\n\nprint(white.tuple)\nprint(white.dict)\n\nmatch r:\n    case Color.Red:\n        print('red')\n    case Color.Blue:\n        print('blue')\n    case Color.Green:\n        print('green')\n\n# unfortunatly you cant use Struct_Variants in a match statment\n\n```\n\n### Result \n\nIt includes the Result type:\n``` python\nfrom ricetypes import Result\n\nr = Result.Ok(10)\n\nfrom math import sqrt\nr.map(sqrt).unwrap()\n\nr = Result.Error('sad').maperr(str.upper)\n\nif r.error:\n    print(r._error)\n\ntry:\n    r.with_exception(KeyError).unwrap()\nexcept KeyError as e:\n    print(e)\n\ntry:\n    r.unwrap()\nexcept Exception as e:\n    print(e)\n\n# We can chain functions together using map\ninc = lambda n: n+1\nResult.Ok(0).map(inc).map(inc).map(sqrt)\n\n# And we can chain functions that return an Option together using a bind\ndef foo(x: int, y:int):\n    if x < y:\n        return Result.Ok(y)\n    return Result.Error(f'y must be bigger then x but x={x}, y={y}')\n\nResult.Ok(0).bind(foo, 10).bind(foo,30).bind(foo,20).or_else(-1)\n```\n\n\n### Option\n``` python\nfrom ricetypes import Option\nop = Option.Some('value')\n\nif op.something:\n    print(op.value)\n\nop.map(str.upper)\n\nop = Option.Nothing\n\nop.or_else('hi')\n\n```\n\n\n",
    "bugtrack_url": null,
    "license": "MIT License",
    "summary": "Some nice type like things",
    "version": "0.1.5",
    "project_urls": {
        "Homepage": "https://github.com/maxwell-gisborne/nicetypes"
    },
    "split_keywords": [
        "typing",
        " result",
        " option",
        " enum"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ecb1c0502c10248799456cf1ccc37db748528159d18c5c2ec8976cf5730c30e6",
                "md5": "a2aef7a1b2c91f630a5164021f6c4a36",
                "sha256": "879e370b259cff962732199fe9beb64d6422c9e50d29c6c75824a7029f740a2d"
            },
            "downloads": -1,
            "filename": "ricetypes-0.1.5-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "a2aef7a1b2c91f630a5164021f6c4a36",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 3817,
            "upload_time": "2024-11-28T18:03:37",
            "upload_time_iso_8601": "2024-11-28T18:03:37.423105Z",
            "url": "https://files.pythonhosted.org/packages/ec/b1/c0502c10248799456cf1ccc37db748528159d18c5c2ec8976cf5730c30e6/ricetypes-0.1.5-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b4753bd6dab55658ef04c4a46b211b9d51c44baa602b92d621800b7cd31c518a",
                "md5": "ff503db0532330f42e1563e76076bf41",
                "sha256": "a6316a104d46a679cd4bb32d7589e7d146887d47f6e69198a6d70899e5f69cd2"
            },
            "downloads": -1,
            "filename": "ricetypes-0.1.5.tar.gz",
            "has_sig": false,
            "md5_digest": "ff503db0532330f42e1563e76076bf41",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 3739,
            "upload_time": "2024-11-28T18:03:38",
            "upload_time_iso_8601": "2024-11-28T18:03:38.526564Z",
            "url": "https://files.pythonhosted.org/packages/b4/75/3bd6dab55658ef04c4a46b211b9d51c44baa602b92d621800b7cd31c518a/ricetypes-0.1.5.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-28 18:03:38",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "maxwell-gisborne",
    "github_project": "nicetypes",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "ricetypes"
}
        
Elapsed time: 0.41054s