No project description provided
Project description
User-friendly tool for combining pycrdt and pydantic.
Overview
pymutantic.MutantModel
- A type safepycrdt.Doc
⟷ pydanticpydantic.BaseModel
mapping with granular editing.pymutantic.JsonPathMutator
- Make edits using json path.
Installation
pip install pycrdt-utils
Usage
MutantModel
Given a pydantic schema...
from pydantic import BaseModel, Field
from typing import List
class Author(BaseModel):
id: str
name: str
class Comment(BaseModel):
id: str
author: Author
content: str
class Post(BaseModel):
id: str
title: str
content: str
author: Author
comments: List[Comment] = Field(default_factory=list)
class BlogPageConfig(BaseModel):
collection: str
posts: List[Post] = Field(default_factory=list)
Create pycrdt documents from instances of that schema using the state
constructor parameter:
from pycrdt_utils import MutantModel
# Define the initial state
initial_state = BlogPageConfig(
collection="tech",
posts=[
Post(
id="post1",
title="First Post",
content="This is the first post.",
author=Author(id="author1", name="Author One"),
comments=[],
)
]
)
# Create a CRDT document with the initial state
doc = MutantModel[BlogPageConfig](state=initial_state)
Get a binary update blob from the CRDT, for example for sending over the wire to other peers:
uri = "ws://example.com/websocket"
async with websockets.connect(uri) as websocket:
await websocket.send(doc.update)
Instantiate documents from a binary update blob (or multiple using the updates
parameter which accepts a list of update blobs):
uri = "ws://example.com/websocket"
async with websockets.connect(uri) as websocket:
doc = MutantModel[BlogPageConfig](update=websocket.recv())
Make granular edits with the mutate
function (applied within a transaction):
# Mutate the document
with doc.mutate() as state:
state.posts[0].comments.append(Comment(
id="comment1",
author=Author(id="author2", name="Author Two"),
content="Nice post!",
))
state.posts[0].title = "First Post (Edited)"
There is also a JsonPathMutator class which can be used to make edits to the document using json path:
# Mutate the document
from pycrdt_utils import JsonPathMutator
with doc.mutate() as state:
mutator = JsonPathMutator(state=state)
mutator.set("$.posts[0].title", "Updated First Post")
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pymutantic-0.1.0.tar.gz
.
File metadata
- Download URL: pymutantic-0.1.0.tar.gz
- Upload date:
- Size: 6.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.11.0rc1 Linux/6.5.0-44-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 18c0e06b261e93adcb3ff1545e512b57c71dd9d3b6466649dd3d705adec38eec |
|
MD5 | 578096d2724edadbf6cc422013088047 |
|
BLAKE2b-256 | 812dd4848e32890ad2be743bdcf4b3d2e59ffa4ef6e8f2fdf96dd93e17e0b52d |
File details
Details for the file pymutantic-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: pymutantic-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.11.0rc1 Linux/6.5.0-44-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c0a0ea106b9f2cbf089e785cab7b15902b8fa49f0f9832a63952049e3f0ff76a |
|
MD5 | 36648950427dbb2c48eb3394e3a6a28d |
|
BLAKE2b-256 | dae2d68a26e1b53946e587dfba0afb7773ee93f362c5ccfa887bd788b5c03cd5 |