typingiterable


Nametypingiterable JSON
Version 0.0.1 PyPI version JSON
download
home_pagehttps://github.com/osoken/typingiterable
SummaryA Simple python package to apply typing to iterables.
upload_time2023-01-16 12:42:29
maintainer
docs_urlNone
authorosoken
requires_python
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # typingiterable

`typingiterable` is a simple python package for the actual typing of each element of an iterable with type hint notation.

## Install

```
pip install git+ssh://git@github.com/osoken/typingiterable.git
```

## Features and Examples

### Actual Typing with Type Hint Notation

The following example shows how the main component `typingiterable.TypingIterable` works:

```py
from dataclasses import dataclass
from typingiterable import TypingIterable

@dataclass
class User:
    id: int
    name: str

raw_data = [{"id": 0, "name": "Alice"}, {"id": 1, "name": "Bob"}]
for d in TypingIterable[User](raw_data):
    assert isinstance(d, User)
```

It is equivalent to write:

```py
from dataclasses import dataclass
from typingiterable import TypingIterable

@dataclass
class User:
    id: int
    name: str

raw_data = [{"id": 0, "name": "Alice"}, {"id": 1, "name": "Bob"}]
for d in (User(**d) for d in raw_data):
    assert isinstance(d, User)
```

### Error Handling

`typingiterable.TypingIterable` also has the error handling feature.

```py
from dataclasses import dataclass
from typingiterable import TypingIterable
from collections.abc import Mapping
from typing import Union

@dataclass
class User:
    id: int
    name: str

def error_handler(d: Mapping[int, Union[int, str]], i: int, e: Exception) -> None:
    print(f"{i}th element `{d}` is invalid due to the following error: {e}")

raw_data = [{"id": 0, "name": "Alice"}, {"name": "lack of id"}, {"id": 1, "name": "Bob"}]
for d in TypingIterable[User](raw_data, on_error=error_handler):
    assert isinstance(d, User)
```

The above example prints the following string:

```
1th element `{'name': 'lack of id'}` is invalid due to the following error: User.__init__() missing 1 required positional argument: 'id'
```

and it doesn't stop iterating.
The example is equivalent to write:

```py
from dataclasses import dataclass
from typingiterable import TypingIterable
from collections.abc import Mapping
from typing import Union

@dataclass
class User:
    id: int
    name: str

raw_data = [{"id": 0, "name": "Alice"}, {"name": "lack of id"}, {"id": 1, "name": "Bob"}]
for i, raw_d in enumerate(raw_data):
    try:
        d = User(**raw_d)
    except Exception as e:
        print(f"{i}th element `{raw_d}` is invalid due to the following error: {e}")
    assert isinstance(d, User)
```

### Automatic Unpacking Arguments

`typingiterable.TypingIterable` checks the signature and automatically unpacks the arguments.
For functions which takes multiple positional arguments or multiple keyword arguments, such as `dataclass` and `pydantic.BaseModel`, it unpacks just like the above example.
If the function is single-argument, no unpacking is done.

```py
from typingiterable import TypingIterable

raw_data = ["1", "2", "3", "4"]
for d in TypingIterable[int](raw_data):
    assert isinstance(d, int)
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/osoken/typingiterable",
    "name": "typingiterable",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "osoken",
    "author_email": "osoken.devel@outlook.jp",
    "download_url": "https://files.pythonhosted.org/packages/84/fb/fef91b6e87d56d85eb4a585197d4be8bcd56451190b79fb5d34b57edf44c/typingiterable-0.0.1.tar.gz",
    "platform": null,
    "description": "# typingiterable\n\n`typingiterable` is a simple python package for the actual typing of each element of an iterable with type hint notation.\n\n## Install\n\n```\npip install git+ssh://git@github.com/osoken/typingiterable.git\n```\n\n## Features and Examples\n\n### Actual Typing with Type Hint Notation\n\nThe following example shows how the main component `typingiterable.TypingIterable` works:\n\n```py\nfrom dataclasses import dataclass\nfrom typingiterable import TypingIterable\n\n@dataclass\nclass User:\n    id: int\n    name: str\n\nraw_data = [{\"id\": 0, \"name\": \"Alice\"}, {\"id\": 1, \"name\": \"Bob\"}]\nfor d in TypingIterable[User](raw_data):\n    assert isinstance(d, User)\n```\n\nIt is equivalent to write:\n\n```py\nfrom dataclasses import dataclass\nfrom typingiterable import TypingIterable\n\n@dataclass\nclass User:\n    id: int\n    name: str\n\nraw_data = [{\"id\": 0, \"name\": \"Alice\"}, {\"id\": 1, \"name\": \"Bob\"}]\nfor d in (User(**d) for d in raw_data):\n    assert isinstance(d, User)\n```\n\n### Error Handling\n\n`typingiterable.TypingIterable` also has the error handling feature.\n\n```py\nfrom dataclasses import dataclass\nfrom typingiterable import TypingIterable\nfrom collections.abc import Mapping\nfrom typing import Union\n\n@dataclass\nclass User:\n    id: int\n    name: str\n\ndef error_handler(d: Mapping[int, Union[int, str]], i: int, e: Exception) -> None:\n    print(f\"{i}th element `{d}` is invalid due to the following error: {e}\")\n\nraw_data = [{\"id\": 0, \"name\": \"Alice\"}, {\"name\": \"lack of id\"}, {\"id\": 1, \"name\": \"Bob\"}]\nfor d in TypingIterable[User](raw_data, on_error=error_handler):\n    assert isinstance(d, User)\n```\n\nThe above example prints the following string:\n\n```\n1th element `{'name': 'lack of id'}` is invalid due to the following error: User.__init__() missing 1 required positional argument: 'id'\n```\n\nand it doesn't stop iterating.\nThe example is equivalent to write:\n\n```py\nfrom dataclasses import dataclass\nfrom typingiterable import TypingIterable\nfrom collections.abc import Mapping\nfrom typing import Union\n\n@dataclass\nclass User:\n    id: int\n    name: str\n\nraw_data = [{\"id\": 0, \"name\": \"Alice\"}, {\"name\": \"lack of id\"}, {\"id\": 1, \"name\": \"Bob\"}]\nfor i, raw_d in enumerate(raw_data):\n    try:\n        d = User(**raw_d)\n    except Exception as e:\n        print(f\"{i}th element `{raw_d}` is invalid due to the following error: {e}\")\n    assert isinstance(d, User)\n```\n\n### Automatic Unpacking Arguments\n\n`typingiterable.TypingIterable` checks the signature and automatically unpacks the arguments.\nFor functions which takes multiple positional arguments or multiple keyword arguments, such as `dataclass` and `pydantic.BaseModel`, it unpacks just like the above example.\nIf the function is single-argument, no unpacking is done.\n\n```py\nfrom typingiterable import TypingIterable\n\nraw_data = [\"1\", \"2\", \"3\", \"4\"]\nfor d in TypingIterable[int](raw_data):\n    assert isinstance(d, int)\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A Simple python package to apply typing to iterables.",
    "version": "0.0.1",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "893bb988db745f64f929ed8f548c36bb3f8bcfe83b2d70f4698a4565b6d14adb",
                "md5": "313f05e3c1280adf95fb985bbdabed06",
                "sha256": "1c2d08302f63ae1e9e7ab6155270a454666c2c82de7827e023529584032cfa9b"
            },
            "downloads": -1,
            "filename": "typingiterable-0.0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "313f05e3c1280adf95fb985bbdabed06",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 4568,
            "upload_time": "2023-01-16T12:42:28",
            "upload_time_iso_8601": "2023-01-16T12:42:28.253402Z",
            "url": "https://files.pythonhosted.org/packages/89/3b/b988db745f64f929ed8f548c36bb3f8bcfe83b2d70f4698a4565b6d14adb/typingiterable-0.0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "84fbfef91b6e87d56d85eb4a585197d4be8bcd56451190b79fb5d34b57edf44c",
                "md5": "3931f33168cf0da78ed664c36494f65f",
                "sha256": "982aad7e07b7c897c6cf35c5bbc07d0b34bba98ae5fcff34848008c5eaa14918"
            },
            "downloads": -1,
            "filename": "typingiterable-0.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "3931f33168cf0da78ed664c36494f65f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 4232,
            "upload_time": "2023-01-16T12:42:29",
            "upload_time_iso_8601": "2023-01-16T12:42:29.613139Z",
            "url": "https://files.pythonhosted.org/packages/84/fb/fef91b6e87d56d85eb4a585197d4be8bcd56451190b79fb5d34b57edf44c/typingiterable-0.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-01-16 12:42:29",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "osoken",
    "github_project": "typingiterable",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "typingiterable"
}
        
Elapsed time: 0.03718s