ricetypes


Namericetypes JSON
Version 0.1.4 PyPI version JSON
download
home_pageNone
SummarySome nice type like things
upload_time2024-11-08 17:42:41
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/30/de/93bf82179cbfa0c466cd5092ba0fde20fdf029e6065b2e7cb24372503aeb/ricetypes-0.1.4.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.4",
    "project_urls": {
        "Homepage": "https://github.com/maxwell-gisborne/nicetypes"
    },
    "split_keywords": [
        "typing",
        " result",
        " option",
        " enum"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2daa70d8b5391c2be0725e21f4df4ad4b5a8d20bc2042668787c316d3ffe95de",
                "md5": "a688a582b4c7b512623ac5b1a8081659",
                "sha256": "163ee427de565af15f744b78064465d7fa92dce5ceafeb2004e7ceecb213a88f"
            },
            "downloads": -1,
            "filename": "ricetypes-0.1.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "a688a582b4c7b512623ac5b1a8081659",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 3817,
            "upload_time": "2024-11-08T17:42:40",
            "upload_time_iso_8601": "2024-11-08T17:42:40.630081Z",
            "url": "https://files.pythonhosted.org/packages/2d/aa/70d8b5391c2be0725e21f4df4ad4b5a8d20bc2042668787c316d3ffe95de/ricetypes-0.1.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "30de93bf82179cbfa0c466cd5092ba0fde20fdf029e6065b2e7cb24372503aeb",
                "md5": "5c9c4f4a77e0db191ebcf085b85701c8",
                "sha256": "4ab8a1b8a16c3b064af0ee8430a75177d38e7f0cccbd4c1eef0bc368c94c2a4b"
            },
            "downloads": -1,
            "filename": "ricetypes-0.1.4.tar.gz",
            "has_sig": false,
            "md5_digest": "5c9c4f4a77e0db191ebcf085b85701c8",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 3750,
            "upload_time": "2024-11-08T17:42:41",
            "upload_time_iso_8601": "2024-11-08T17:42:41.513651Z",
            "url": "https://files.pythonhosted.org/packages/30/de/93bf82179cbfa0c466cd5092ba0fde20fdf029e6065b2e7cb24372503aeb/ricetypes-0.1.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-08 17:42:41",
    "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.38959s