Skip to main content

create nested data structure easily

Project description

pypi Downloads Python Versions CI Test Coverage

Pydantic-resolve is a schema based, hierarchical solution for fetching and crafting data.

It combines the advantages of restful and graphql.

img

Advantages:

  1. use declaretive way to define view data, easy to maintain and develop
  2. enhance the traditional restful response, to support gql-like style data structure.
  3. provide post_method and other tools to craft resolved data.

Discord

Install

If you are using pydantic v1, please use pydantic-resolve instead.

pip install pydantic-resolve

Concepts from GraphQL to Pydantic-resolve

query {
    MyBlogSite {
        name
        blogs {
            id
            title
            comments {
                id
                content
            }
            # comment_count
        }
        # comment_count
    }
}


This is how we do queries in GraphQL, dive by describing schema and field names.

Assuming `comment_count` is a extra field (length of comment), which is required and calculated by client after fetching the data.

client side so need to iterate over the blogs to get the length and the sum, which is boring (things gets worse if the structure is deeper).

In pydantic-resolve, we can handle comment_count at server side, by transforming the query into pydantic schemas and attach some resolve, post methods.


```python
import blog_service as bs
import comment_service as cs

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])

# -------- inherit and extend ----------
class MySiteBlog(bs.Blog):  
    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)
        
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.

So that we can declare customrized schema by simpily INHERIT and EXTEND from base schemas.

This just sounds 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.

Collector is a powerful feature for adjusting data structures. https://allmonday.github.io/pydantic-resolve/reference_api/#collector

API Reference

https://allmonday.github.io/pydantic-resolve/reference_api/

Composition oriented development-pattern (wip)

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydantic2_resolve-2.1.1.tar.gz (14.4 kB view details)

Uploaded Source

Built Distribution

pydantic2_resolve-2.1.1-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

Details for the file pydantic2_resolve-2.1.1.tar.gz.

File metadata

  • Download URL: pydantic2_resolve-2.1.1.tar.gz
  • Upload date:
  • Size: 14.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.4 Darwin/22.2.0

File hashes

Hashes for pydantic2_resolve-2.1.1.tar.gz
Algorithm Hash digest
SHA256 85fd6300d804d5d49a47fa03c46a2199895cc2afb33ba588059b13f0b8dd5148
MD5 23a1cd13b7d24fb1ba888ed8bb34ddf8
BLAKE2b-256 d164c6cf91e3fe8d221f51622e2dcf843767dd387d99708f01bcf265465c8f0f

See more details on using hashes here.

File details

Details for the file pydantic2_resolve-2.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic2_resolve-2.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 189f5e99d7f842fb8f6d1b138bfe9340f94e3f804b2180e635b65fac5c9523d1
MD5 a1e2f1b469c03e11a802bca9e9c29ebe
BLAKE2b-256 e48a09243ae2b8976663c10513615c8113715e9b41b51ab7b23dc6775c4c6889

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page