A Simple python package to apply typing to iterables.
Project description
typediterable
typediterable 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/typediterable.git
Features and Examples
Actual Typing with Type Hint Notation
The following example shows how the main component typediterable.TypedIterable works:
from dataclasses import dataclass
from typediterable import TypedIterable
@dataclass
class User:
id: int
name: str
raw_data = [{"id": 0, "name": "Alice"}, {"id": 1, "name": "Bob"}]
for d in TypedIterable[User](raw_data):
assert isinstance(d, User)
It is equivalent to write:
from dataclasses import dataclass
from typediterable import TypedIterable
@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
typediterable.TypedIterable also has the error handling feature.
from dataclasses import dataclass
from typediterable import TypedIterable
from collections.abc import Mapping
from typing import Union
@dataclass
class User:
id: int
name: str
def error_handler(d: Mapping[str, 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 TypedIterable[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:
from dataclasses import dataclass
from typediterable import TypedIterable
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
typediterable.TypedIterable 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.
from typediterable import TypedIterable
raw_data = ["1", "2", "3", "4"]
for d in TypedIterable[int](raw_data):
assert isinstance(d, int)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file typediterable-0.0.1.tar.gz.
File metadata
- Download URL: typediterable-0.0.1.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fc6539a30d22660d537750db2e8947b45039c9c9b9766bbebb1842c320e3417e
|
|
| MD5 |
b1e2440011e4de4c759f01da90fa438b
|
|
| BLAKE2b-256 |
559135be14b46624361386806bc46ba3675aaaa24df77102b3a194da6ed8a9e1
|
File details
Details for the file typediterable-0.0.1-py3-none-any.whl.
File metadata
- Download URL: typediterable-0.0.1-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ac4620232e866cc25744482f4a01aa5c7e817458d3feb9e626193f9ff3425811
|
|
| MD5 |
4ad149bc4550beca0e9e077909e3b9b2
|
|
| BLAKE2b-256 |
5a1da726f13e03a6f6bd1371408f3460ea8c69e69b10371bbc248e4075b38f48
|