create nested data structure easily
Project description
Pydantic-resolve is a schema based, hierarchical solution for data fetching and crafting.
Features:
- with pydantic schema and instances, resolver recursively resolve uncertain nodes and their descendants.
- nodes could be modified during post-process
- plugable, easy to combine together and reuse.
Install
User of pydantic v2, please use pydantic2-resolve instead.
pip install pydantic-resolve
Hello world
build blogs with comments
import asyncio
import json
from pydantic import BaseModel
from pydantic_resolve import Resolver
class Comment(BaseModel):
id: int
content: str
class Blog(BaseModel):
id: int
title: str
content: str
comments: list[Comment] = []
def resolve_comments(self):
return get_comments(self.id)
def get_blogs():
return [
dict(id=1, title="hello world", content="hello world detail"),
dict(id=2, title="hello Mars", content="hello Mars detail"),
]
def get_comments(id):
comments = [
dict(id=1, content="world is beautiful", blog_id=1),
dict(id=2, content="Mars is beautiful", blog_id=2),
dict(id=3, content="I love Mars", blog_id=2),
]
return [c for c in comments if c['blog_id'] == id]
async def main():
blogs = [Blog.parse_obj(blog) for blog in get_blogs()]
blogs = await Resolver().resolve(blogs)
print(json.dumps(blogs, indent=2, default=lambda o: o.dict()))
asyncio.run(main())
[
{
"id": 1,
"title": "hello world",
"content": "hello world detail",
"comments": [
{
"id": 1,
"content": "world is beautiful"
}
]
},
{
"id": 2,
"title": "hello Mars",
"content": "hello Mars detail",
"comments": [
{
"id": 2,
"content": "Mars is beautiful"
},
{
"id": 3,
"content": "I love Mars"
}
]
}
]
Documents
- Quick start: https://allmonday.github.io/pydantic-resolve/about/
- API: https://allmonday.github.io/pydantic-resolve/reference_api/
- Demo: https://github.com/allmonday/pydantic-resolve-demo
- Composition oriented pattern: https://github.com/allmonday/composition-oriented-development-pattern
Sponsor
If this code helps and you wish to support me
Paypal: https://www.paypal.me/tangkikodo
Discussion
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
pydantic_resolve-1.10.7.tar.gz
(13.6 kB
view details)
Built Distribution
File details
Details for the file pydantic_resolve-1.10.7.tar.gz
.
File metadata
- Download URL: pydantic_resolve-1.10.7.tar.gz
- Upload date:
- Size: 13.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.10.4 Darwin/22.2.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6c3e67d1ec85ef4a3daab69eec26cdc555e50e136bcbf78f9ee4c97857228b6f |
|
MD5 | 5fb6bb885f69ec60b9b77d3fa161bb25 |
|
BLAKE2b-256 | e3bd5c2bca2d29bea4b137931d04dfca18b093c8a55e91bb36cb88077545335a |
File details
Details for the file pydantic_resolve-1.10.7-py3-none-any.whl
.
File metadata
- Download URL: pydantic_resolve-1.10.7-py3-none-any.whl
- Upload date:
- Size: 14.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.10.4 Darwin/22.2.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d9f264d409772a61adbc690e1356bbc135afac9caca45954520540cf7e8d513 |
|
MD5 | a259ef68934c7a884f3318a22a12764e |
|
BLAKE2b-256 | aa9ea026002adc0563f64c50cc4c169a750f5a74095cb95e6e6b635ea3c42936 |