Name | ricetypes JSON |
Version |
0.1.7
JSON |
| download |
home_page | None |
Summary | Some nice type like things |
upload_time | 2025-02-19 11:53:49 |
maintainer | None |
docs_url | None |
author | None |
requires_python | >=3.9 |
license | MIT 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
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:
``` 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/72/5c/8c845191fff7708d071de1ade71aac6408fbf0a87f91fe4654736b5e53dd/ricetypes-0.1.7.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\nwhite = 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.7",
"project_urls": {
"Homepage": "https://github.com/maxwell-gisborne/nicetypes"
},
"split_keywords": [
"typing",
" result",
" option",
" enum"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "83de283c482f1bb4bd1e52350b34879c5af6b35264bb81c95581ff92e2a84e0e",
"md5": "0ff206629e47828f51ab520db3a3ad82",
"sha256": "37f2f4bbcc968759b314a703aa989b5d0ec50cd84127e37d54002bda4904f109"
},
"downloads": -1,
"filename": "ricetypes-0.1.7-py3-none-any.whl",
"has_sig": false,
"md5_digest": "0ff206629e47828f51ab520db3a3ad82",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 4600,
"upload_time": "2025-02-19T11:53:48",
"upload_time_iso_8601": "2025-02-19T11:53:48.400912Z",
"url": "https://files.pythonhosted.org/packages/83/de/283c482f1bb4bd1e52350b34879c5af6b35264bb81c95581ff92e2a84e0e/ricetypes-0.1.7-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "725c8c845191fff7708d071de1ade71aac6408fbf0a87f91fe4654736b5e53dd",
"md5": "d69aa1dee9410c9d3accb7fdb300bdc4",
"sha256": "9d6b179aa29cc94cac3a4010e411856a972019400afe813317f1f8b0effd5337"
},
"downloads": -1,
"filename": "ricetypes-0.1.7.tar.gz",
"has_sig": false,
"md5_digest": "d69aa1dee9410c9d3accb7fdb300bdc4",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 4386,
"upload_time": "2025-02-19T11:53:49",
"upload_time_iso_8601": "2025-02-19T11:53:49.639400Z",
"url": "https://files.pythonhosted.org/packages/72/5c/8c845191fff7708d071de1ade71aac6408fbf0a87f91fe4654736b5e53dd/ricetypes-0.1.7.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-02-19 11:53:49",
"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"
}