Skip to main content

create nested data structure easily

Project description

pypi Downloads Python Versions Test Coverage CI

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

Features:

  1. with pydantic schema and instances, resolver recursively resolve uncertain nodes and their descendants.
  2. nodes could be modified during post-process
  3. 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

Sponsor

If this code helps and you wish to support me

Paypal: https://www.paypal.me/tangkikodo

Discussion

Discord

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

pydantic_resolve-1.10.7.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

pydantic_resolve-1.10.7-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_resolve-1.10.7.tar.gz.

File metadata

  • Download URL: pydantic_resolve-1.10.7.tar.gz
  • Upload date:
  • Size: 13.6 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 pydantic_resolve-1.10.7.tar.gz
Algorithm Hash digest
SHA256 6c3e67d1ec85ef4a3daab69eec26cdc555e50e136bcbf78f9ee4c97857228b6f
MD5 5fb6bb885f69ec60b9b77d3fa161bb25
BLAKE2b-256 e3bd5c2bca2d29bea4b137931d04dfca18b093c8a55e91bb36cb88077545335a

See more details on using hashes here.

File details

Details for the file pydantic_resolve-1.10.7-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_resolve-1.10.7-py3-none-any.whl
Algorithm Hash digest
SHA256 5d9f264d409772a61adbc690e1356bbc135afac9caca45954520540cf7e8d513
MD5 a259ef68934c7a884f3318a22a12764e
BLAKE2b-256 aa9ea026002adc0563f64c50cc4c169a750f5a74095cb95e6e6b635ea3c42936

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