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 pymutantic
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.2.0.tar.gz
.
File metadata
- Download URL: pymutantic-0.2.0.tar.gz
- Upload date:
- Size: 7.3 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 | 0dc323ec703568ddd3cf78ea5b381b675047bfa854676ee8fe1e73a34c7b5765 |
|
MD5 | 98fae05bdd579a555660959ba8003bd8 |
|
BLAKE2b-256 | 0c9aa6a615f9e58121fb28c5bb8cb84f61ed98ef310624fb18238d239fbd2a9a |
File details
Details for the file pymutantic-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: pymutantic-0.2.0-py3-none-any.whl
- Upload date:
- Size: 8.7 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 | 497237319f9cb93f72105189351e49cd57fb59538e264437bcc605546f03aa85 |
|
MD5 | c184e265aad16fad9b9701b160a506dd |
|
BLAKE2b-256 | d27f964148b4c50b7c86e9b7b52bc98bba17d5b439e70ef0120f1b4e073f1e76 |