create nested data structure easily
Project description
Pydantic-resolve is a schema based, hierarchical solution for fetching and crafting data.
Features:
- By providing your pydantic schema and instance(s), resolver will recursively resolve uncertain nodes and their descendants.
- You can modify resolve nodes or compute new nodes based on resolved nodes, no iteration.
- Schemas are pluggable, easy to combine and reuse.
Install
User of pydantic v2, please use pydantic2-resolve instead.
pip install pydantic-resolve
For user from GraphQL
This is how we make queries in GraphQL, dive by describing schema and field names.
query {
MyBlogSite {
name
blogs {
id
title
comments {
id
content
}
# comment_count
}
# comment_count
}
}
using query statement is flexible, but for scenarios like
API integration
it can be difficult to maintain.
In client, field comment_count
is the length of each blog and also the total count (just for demo, pls ignore pagination staff), so client side need to iterate over the blogs to get the length and the sum, which is boring (things getting worse if the structure is deeper).
With pydantic-resolve, we can handle these calculations at server side. Let see how it works.
By transforming the query into pydantic schemas and attach some resolve, post methods, it looks like:
from __future__ import annotations
import comment_service as cs
import blog_service as bs
class MySite(BaseModel):
blogs: list[MySiteBlog] = []
async def resolve_blogs(self):
return await bs.get_blogs()
comment_count: int = 0
def post_comment_count(self):
return sum([b.comment_count for b in self.blogs]) # for total
class MySiteBlog(bs.Blog): # >>> inherit and extend <<<
comments: list[cs.Comment] = []
def resolve_comments(self, loader=LoaderDepend(cs.blog_to_comments_loader)):
return loader.load(self.id)
comment_count: int = 0
def post_comment_count(self):
return len(self.comments) # for each blog
async def main():
my_blog_site = MyBlogSite(name: "tangkikodo's blog")
my_blog_site = await Resolver().resolve(my_blog_site)
schemas , query functions and loader functions are provided by entity's service modules.
And then we can simpily inherit and extend from these base schemas to declare the schemas you want.
This just like columns of values (inherit) and of foreign keys (extend) in concept of relational database.
After transforming GraphQL query into pydantic schemas, post calculation become dead easy, and no more iterations.
API Reference
for more details, please refer to: https://allmonday.github.io/pydantic-resolve/reference_api/
Composition oriented development-pattern (wip)
This repo introduce a pattern that balance the speed of development and the maintaince, readability of your project, based on pydantic-resolve.
https://github.com/allmonday/composition-oriented-development-pattern
Unittest
poetry run python -m unittest # or
poetry run pytest # or
poetry run tox
Coverage
poetry run coverage run -m pytest
poetry run coverage report -m
Sponsor
If this code helps and you wish to support me
Paypal: https://www.paypal.me/tangkikodo
Others
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
Hashes for pydantic_resolve-1.10.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7a3f4c4b6038fb07338123cdc9bbe1ed95065a9e4f6cb98a910a4f6e652cab95 |
|
MD5 | c06f499fa559a3066c6e48d9f77fe691 |
|
BLAKE2b-256 | 8bcf0641944c339cdcdeeefb830d12751e2904754de9ec456dbe9023f6e3d339 |