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.8.tar.gz
(13.8 kB
view details)
Built Distribution
File details
Details for the file pydantic_resolve-1.10.8.tar.gz
.
File metadata
- Download URL: pydantic_resolve-1.10.8.tar.gz
- Upload date:
- Size: 13.8 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 | f6f1baa430d3581bb098e69ecd32aaf188bca151c05bf43dfcf4e2e08b872fdf |
|
MD5 | fe0616f462c6a82dc36fcc0bc394cfff |
|
BLAKE2b-256 | 8cf95770eb7d4f035bf13b829820a4842ec9b0701e97c93ca02d1b4fb745feaf |
File details
Details for the file pydantic_resolve-1.10.8-py3-none-any.whl
.
File metadata
- Download URL: pydantic_resolve-1.10.8-py3-none-any.whl
- Upload date:
- Size: 14.9 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 | 3bd176aad9cef705ae67ac6d57eb1e4a1538e330e78f8c4e8ccadcc1d0deed79 |
|
MD5 | c874f461eb3f3b5a5d9bf393d0524911 |
|
BLAKE2b-256 | 5bd14e021d4bdba8dba7e5368c50e2bb5409a3a6cebc7ab97f56f4feaffc3d79 |