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.8.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

pydantic_resolve-1.10.8-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pydantic_resolve-1.10.8.tar.gz
  • Upload date:
  • Size: 13.8 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.8.tar.gz
Algorithm Hash digest
SHA256 f6f1baa430d3581bb098e69ecd32aaf188bca151c05bf43dfcf4e2e08b872fdf
MD5 fe0616f462c6a82dc36fcc0bc394cfff
BLAKE2b-256 8cf95770eb7d4f035bf13b829820a4842ec9b0701e97c93ca02d1b4fb745feaf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pydantic_resolve-1.10.8-py3-none-any.whl
Algorithm Hash digest
SHA256 3bd176aad9cef705ae67ac6d57eb1e4a1538e330e78f8c4e8ccadcc1d0deed79
MD5 c874f461eb3f3b5a5d9bf393d0524911
BLAKE2b-256 5bd14e021d4bdba8dba7e5368c50e2bb5409a3a6cebc7ab97f56f4feaffc3d79

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