Skip to main content

Add your description here

Project description

Progressive JSON Handler

A Python package for progressively streaming JSON data with asynchronously computed fields.

Description

This package provides a mechanism to define a JSON schema where some fields are computed by functions (which can be long-running). The package then streams the JSON, sending the immediately available fields first, and then sending the computed fields as they become available. This is useful for building responsive applications where you want to display partial data to the user while the rest of the data is being computed.

The package is built on top of Pydantic and provides a ProgressiveSchema class that you can use to define your data models.

Installation

To install the package, you can use pip:

pip install progressive-json-handler

Usage

  1. Define your schema: Create a class that inherits from ProgressiveSchema and define your fields. Use the Computation type for fields that need to be computed.

  2. Instantiate your schema: Create an instance of your schema, passing the functions that will compute the values for the Computation fields.

  3. Stream the data: Use the to_streamer() method on your schema instance to get a streamer object, and then call stream_sync() to get a generator that yields the JSON chunks.

  4. Assemble the JSON: Use the ProgressiveAssembler to consume the generator and assemble the final JSON object.

Example

Here's an example of how to use the package to stream a user profile where the products and loyalty_score fields are computed by functions:

import time
from pydantic import BaseModel
from src.models.progressive_schema import ProgressiveSchema
from src.models.computation import Computation
from src.models.progressive_streamer import ProgressiveJSONStreamer
from src.models.progressive_assembler import ProgressiveAssembler

class Products(BaseModel):
    name: str
    price: float

def get_user_products_sync() -> list[Products]:
    """Simulates a slow, sync database call."""
    time.sleep(2)  # Simulate 2 seconds of work
    return [
        Products(name="Laptop Bag", price=50.0),
        Products(name="Monitor", price=200.0),
        Products(name="Mechanical Keyboard", price=150.0),
    ]

def calculate_loyalty_score_sync() -> int:
    """Simulates a slower, sync external API call or heavy computation."""
    time.sleep(1)  # Simulate 1 second of work
    return 95

class UserAddress(BaseModel):
    street: str
    city: str

class UserProfile(ProgressiveSchema):
    user_id: int
    username: str
    email: str
    address: UserAddress
    products: Computation[list[Products]]
    loyalty_score: Computation[int]

# 1. Instantiate your schema
user_data = UserProfile(
    user_id=101,
    username="jdoe",
    email="john.doe@example.com",
    address=UserAddress(street="123 Placeholder Dr", city="Streamington"),
    products=Computation(get_user_products_sync),
    loyalty_score=Computation(calculate_loyalty_score_sync),
)

# 2. Stream the data
streamer = user_data.to_streamer()
generator = streamer.stream_sync()

# 3. Assemble the JSON
assembler = ProgressiveAssembler()
final_json = assembler.assamble(generator)

print(final_json)

This will output:

{
  "user_id": 101,
  "username": "jdoe",
  "email": "john.doe@example.com",
  "address": {
    "street": "123 Placeholder Dr",
    "city": "Streamington"
  },
  "products": [
    {
      "name": "Laptop Bag",
      "price": 50.0
    },
    {
      "name": "Monitor",
      "price": 200.0
    },
    {
      "name": "Mechanical Keyboard",
      "price": 150.0
    }
  ],
  "loyalty_score": 95
}

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

ame_json-0.1.0.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ame_json-0.1.0-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

Details for the file ame_json-0.1.0.tar.gz.

File metadata

  • Download URL: ame_json-0.1.0.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.5

File hashes

Hashes for ame_json-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9cb29ed25ff4e5f84a15d3c036430c416b32423093193d59d4580b2f1d43f914
MD5 c1c9103987fa628d0376df4a21d44524
BLAKE2b-256 2423bea3752479cbea069dd201cd9aa391907c9c234cc6da802b3d76eb9379e8

See more details on using hashes here.

File details

Details for the file ame_json-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: ame_json-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.5

File hashes

Hashes for ame_json-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 109e495d3d788c19ba0316ad034c9fe5bcc3810a511ece91711cdab519b4ebd0
MD5 698b3079362273a33a4bec95e9de6891
BLAKE2b-256 728926c2c9569c049d17de5a712e7de68f1308c8a896500c5d9404d7fad39df5

See more details on using hashes here.

Supported by

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