Name | unopt JSON |
Version |
0.2.0
JSON |
| download |
home_page | |
Summary | Utility functions to unwrap Optional values |
upload_time | 2023-10-28 18:45:15 |
maintainer | |
docs_url | None |
author | |
requires_python | <=3.13,>=3.8 |
license | Apache Software License 2.0 |
keywords |
none
optional
rust
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# unopt: Utility functions to unwrap `Optional[T]`
## Overview
*unopt* provides several utility functions to "unwrap" the `Optional[T]` (or `T | None`)
objects: removes the `Optional` type hint and obtains the underlying object.
*unopt* functions are inspired by the Rust's `Option<T>` functionality, but the behavior
is tuned to Python's convention. E.g., `unwrap()` raises an exception instead of
aborting.
## Install
```shell
pip install unopt
```
## Examples
```python
from unopt import *
foo: Optional[int] = 123
bar: Optional[int] = None
# unwrap() returns the given object if it is not None.
assert unwrap(foo) == 123
unwrap(bar) # Raises UnwrapError
# unwrap_or() returns the default value if the given object is None.
assert unwrap_or(foo, 456) == 123
assert unwrap_or(bar, 456) == 456
# unwrap_or_else() returns the default value obtained by invoking the given function.
assert unwrap_or_else(foo, lambda: 456) == 123
assert unwrap_or_else(bar, lambda: 456) == 456
# unwrap_unchecked() just casts the given object without value checking.
assert unwrap_unchecked(foo) == 123
assert unwrap_unchecked(bar) is None # Unsafe
```
Raw data
{
"_id": null,
"home_page": "",
"name": "unopt",
"maintainer": "",
"docs_url": null,
"requires_python": "<=3.13,>=3.8",
"maintainer_email": "",
"keywords": "none,optional,rust",
"author": "",
"author_email": "Yusuke Oda <odashi@predicate.jp>",
"download_url": "https://files.pythonhosted.org/packages/16/94/69faaf0460d3a97b89c22dc3aa8c4114134f798d155c69386021d9861070/unopt-0.2.0.tar.gz",
"platform": null,
"description": "# unopt: Utility functions to unwrap `Optional[T]`\n\n## Overview\n\n*unopt* provides several utility functions to \"unwrap\" the `Optional[T]` (or `T | None`)\nobjects: removes the `Optional` type hint and obtains the underlying object.\n\n*unopt* functions are inspired by the Rust's `Option<T>` functionality, but the behavior\nis tuned to Python's convention. E.g., `unwrap()` raises an exception instead of\naborting.\n\n## Install\n\n```shell\npip install unopt\n```\n\n## Examples\n\n```python\nfrom unopt import *\n\nfoo: Optional[int] = 123\nbar: Optional[int] = None\n\n# unwrap() returns the given object if it is not None.\nassert unwrap(foo) == 123\nunwrap(bar) # Raises UnwrapError\n\n# unwrap_or() returns the default value if the given object is None.\nassert unwrap_or(foo, 456) == 123\nassert unwrap_or(bar, 456) == 456\n\n# unwrap_or_else() returns the default value obtained by invoking the given function.\nassert unwrap_or_else(foo, lambda: 456) == 123\nassert unwrap_or_else(bar, lambda: 456) == 456\n\n# unwrap_unchecked() just casts the given object without value checking.\nassert unwrap_unchecked(foo) == 123\nassert unwrap_unchecked(bar) is None # Unsafe\n```\n",
"bugtrack_url": null,
"license": "Apache Software License 2.0",
"summary": "Utility functions to unwrap Optional values",
"version": "0.2.0",
"project_urls": {
"Bug Tracker": "https://github.com/odashi/unopt-py/issues",
"Homepage": "https://github.com/odashi/unopt-py"
},
"split_keywords": [
"none",
"optional",
"rust"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "452a8de3493e54bc14b823c1d427a1a702c1f484727bc5435ed9597c9b58b33e",
"md5": "2824939b426ea2486d4f57b2b710b6f2",
"sha256": "e46d972ada65f251364d8da33fe8060d99314e332df4f4d0e5d41f5707d561f4"
},
"downloads": -1,
"filename": "unopt-0.2.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "2824939b426ea2486d4f57b2b710b6f2",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<=3.13,>=3.8",
"size": 3814,
"upload_time": "2023-10-28T18:45:14",
"upload_time_iso_8601": "2023-10-28T18:45:14.521986Z",
"url": "https://files.pythonhosted.org/packages/45/2a/8de3493e54bc14b823c1d427a1a702c1f484727bc5435ed9597c9b58b33e/unopt-0.2.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "169469faaf0460d3a97b89c22dc3aa8c4114134f798d155c69386021d9861070",
"md5": "7fdf9e29c090c7eae9f0b9a0760ed34a",
"sha256": "43c2b629ef94f465bee0a74e13d46faa2245810fec50bde346226e5f573ad082"
},
"downloads": -1,
"filename": "unopt-0.2.0.tar.gz",
"has_sig": false,
"md5_digest": "7fdf9e29c090c7eae9f0b9a0760ed34a",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<=3.13,>=3.8",
"size": 3849,
"upload_time": "2023-10-28T18:45:15",
"upload_time_iso_8601": "2023-10-28T18:45:15.660337Z",
"url": "https://files.pythonhosted.org/packages/16/94/69faaf0460d3a97b89c22dc3aa8c4114134f798d155c69386021d9861070/unopt-0.2.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-10-28 18:45:15",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "odashi",
"github_project": "unopt-py",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "unopt"
}