A simple package to transform/map dictionaries, before parsing it into Pydantic.
Project description
Pydantic Model Parser
A simple package to transform/map dictionaries, before parsing it into Pydantic.
Requirements
- The models/entities should conform to
Pydantic'sModel specifications and should inherit thepydantic.BaseModel.
Installation
pip3 install pydantic-model-parser
Usage
Firstly, define your entity using Pydantic's BaseModel.
# comment.py
from pydantic import BaseModel
class User(BaseModel):
id: int
name: str
class Comment(BaseModel):
id: int
comment_str: str
user: User
Next, define the Mapper. The mapper can be used to rearrange dictionary keys and perform transformations on the values.
The tuples definitions are as follows:
- (
old_field_path: str,new_field_path: str,transform_func: Optional[Callable]) transform_funcofNonemaps the value as per the original valuetransform_funcoflambda x: x * 2maps the value as double of the original valueold_field_path's value is placed innew_field_pathin the new dictionary and subsequently parsed into theBaseModel- The
.in the path delimits the nested levels in the dictionaries. e.g.user.idrefers to:
- The
{
"user": {
"id": 1
}
}
Defining a Mapper:
# comment.py
from model_parser.custom_types import Mapping
from model_parser.mapper import BaseMapper
class CommentMapper(BaseMapper):
@staticmethod
def get_mapping() -> List[Mapping]:
return [
('id', 'id', None),
('comment_str', 'comment_str', None),
('user_name', 'user.name', None),
('user_id', 'user.id', None)
]
Lastly, to parse objects:
# main.py
from comment import Comment, CommentMapper
data = {
"id": 1,
"comment_str": "HelloWorld",
"user_id": 2,
"user_name": "bob"
}
data_list = [data, data]
parser = Parser(Comment, CommentMapper)
# parse into a Comment entity
comment = parser.parse(data)
# parse into a list of Comment entities
comments = parser.parse(data_list)
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 pydantic-model-parser-1.0.1.tar.gz.
File metadata
- Download URL: pydantic-model-parser-1.0.1.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
57e1032d4eb2a6f60b7b7b4a24d991b989a7937f01a7ee98ee77906ece02563f
|
|
| MD5 |
e9c087c88c7a0e9411562a98d59f8d3e
|
|
| BLAKE2b-256 |
9dd6252830fd36a5f2f5363766e6728c6cfd3ae3e3c87171dbe07d194941ceaa
|
File details
Details for the file pydantic_model_parser-1.0.1-py3-none-any.whl.
File metadata
- Download URL: pydantic_model_parser-1.0.1-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fb7912040b5f9c404f9a68b9be12303deea18fa1d18e2a2092e55a56c7337f45
|
|
| MD5 |
b6b5205c8bd73865f8d89e7bae9b91f1
|
|
| BLAKE2b-256 |
60ff1d979acdcd949d51ccd2560bac0b68d1b98ee7a6560204b68d775bc47783
|