This repository has been archived by the owner on Oct 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
117 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import asyncio | ||
from pydantic import BaseModel | ||
from pydantic_resolve import Resolver | ||
|
||
comments_table = [ | ||
dict(id=1, blog_id=1, content='its interesting'), | ||
dict(id=2, blog_id=1, content='i dont understand'), | ||
dict(id=3, blog_id=2, content='why? how?'), | ||
dict(id=4, blog_id=2, content='wow!'), | ||
] | ||
|
||
async def query_comments(blog_id: int): | ||
print(f'run query - {blog_id}') | ||
return [c for c in comments_table if c['blog_id'] == blog_id] | ||
|
||
class Comment(BaseModel): | ||
id: int | ||
content: str | ||
|
||
class Blog(BaseModel): | ||
id: int | ||
title: str | ||
|
||
comments: list[Comment] = [] | ||
async def resolve_comments(self): | ||
return await query_comments(self.id) | ||
|
||
comment_count: int = 0 | ||
def post_comment_count(self): | ||
return len(self.comments) | ||
|
||
class MyBlogSite(BaseModel): | ||
blogs: list[Blog] | ||
|
||
comment_count: int = 0 | ||
def post_comment_count(self): | ||
return sum([b.comment_count for b in self.blogs]) | ||
|
||
|
||
async def single(): | ||
blog = Blog(id=1, title='what is pydantic-resolve') | ||
blog = await Resolver().resolve(blog) | ||
print(blog) | ||
|
||
|
||
async def batch(): | ||
my_blog_site = MyBlogSite( | ||
blogs = [ | ||
Blog(id=1, title='what is pydantic-resolve'), | ||
Blog(id=2, title='what is composition oriented development pattarn'), | ||
] | ||
) | ||
my_blog_site = await Resolver().resolve(my_blog_site) | ||
print(my_blog_site.model_dump_json(indent=4)) | ||
|
||
|
||
async def main(): | ||
await single() | ||
await batch() | ||
|
||
asyncio.run(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import asyncio | ||
from pydantic import BaseModel | ||
from pydantic_resolve import Resolver, build_list, LoaderDepend | ||
|
||
comments_table = [ | ||
dict(id=1, blog_id=1, content='its interesting'), | ||
dict(id=2, blog_id=1, content='i dont understand'), | ||
dict(id=3, blog_id=2, content='why? how?'), | ||
dict(id=4, blog_id=2, content='wow!'), | ||
] | ||
|
||
async def blog_to_comments_loader(blog_ids: list[int]): | ||
print(blog_ids) | ||
return build_list(comments_table, blog_ids, lambda c: c['blog_id']) | ||
|
||
|
||
class Comment(BaseModel): | ||
id: int | ||
content: str | ||
|
||
class Blog(BaseModel): | ||
id: int | ||
title: str | ||
|
||
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) | ||
|
||
class MyBlogSite(BaseModel): | ||
blogs: list[Blog] | ||
|
||
comment_count: int = 0 | ||
def post_comment_count(self): | ||
return sum([b.comment_count for b in self.blogs]) | ||
|
||
async def batch(): | ||
my_blog_site = MyBlogSite( | ||
blogs = [ | ||
Blog(id=1, title='what is pydantic-resolve'), | ||
Blog(id=2, title='what is composition oriented development pattarn'), | ||
] | ||
) | ||
my_blog_site = await Resolver().resolve(my_blog_site) | ||
print(my_blog_site.model_dump_json(indent=4)) | ||
|
||
|
||
async def main(): | ||
await batch() | ||
|
||
asyncio.run(main()) |