create nested data structure easily
Project description
Pydantic-resolve is a schema based, hierarchical solution for fetching and crafting data.
Features:
- The best data aggregration (bff) tool for FastAPI/Django ninja ever.
- By providing/describing your pydantic schema and instance(s), resolver will recursively resolve uncertain nodes and their descendants.
- You can modify resolved nodes, or compute new nodes based on resolved nodes, no need of iteration.
- Schemas are pluggable, easy to combine together and easy to reuse.
Install
User of pydantic v2, please use pydantic2-resolve instead.
pip install pydantic-resolve
Inherit and extend schemas, then resolve
from __future__ import annotations
class Blog(BaseModel):
id: int
title: str
class Comment(BaseModel):
id: int
content: str
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(Blog):
comments: list[Comment] = []
def resolve_comments(self, loader=LoaderDepend(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
my_blog_site = MyBlogSite(name: "tangkikodo's blog")
my_blog_site = await Resolver().resolve(my_blog_site)
Full demo with FastAPI & hey-api/openapi-ts
https://github.com/allmonday/pydantic-resolve-demo
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
File details
Details for the file pydantic_resolve-1.10.6.tar.gz
.
File metadata
- Download URL: pydantic_resolve-1.10.6.tar.gz
- Upload date:
- Size: 13.7 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 | 335413306eabd6ca60729a44606bc21547cf96100f6737bd8a51c40e86de5583 |
|
MD5 | 6975f3420e2f3a6705071e46e8d2320a |
|
BLAKE2b-256 | c704b341dd87796957a86e89d568ce9c06f64d2ab1e6b886c22c93c5d13aa21f |
File details
Details for the file pydantic_resolve-1.10.6-py3-none-any.whl
.
File metadata
- Download URL: pydantic_resolve-1.10.6-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 | 1a4f1fb6b8cc5419347a5e77cca0cc8ee1213f6905ba8beec10f0f4e18b213a8 |
|
MD5 | 1d7984ce1def04394c8ada16031e2a79 |
|
BLAKE2b-256 | e013ceb26827a54e72c47f052f5fa1426ea2cb4be5c1c26042c29e8c0e97d6da |