Skip to main content

Weaver - Read and Write Python Objects

Green Python in a Woven Basket

Weaver writes Python objects to a JSON format, storing the larger items as binary blobs on the local filesystem.

Why?

I wanted a way of saving Python objects that could still be examined in a common, human-readable, format, while still containing all the original data. Also it's nice to be able to load and save from different versions of a library for breaking changes.

How?

Same things as Pickle really. Save all the object state to a dictionary, throw it into JSON. When it comes to load, create a new object via __new__ and add the state back in. Then do that recursively both ways.

What are the requirements for loading them back?

You need the same libraries loaded. Maybe not the same versions of those libraries though. If you have a data format that you need to save in one version, and load in the other, you can implement serializers for it;

Let's write a serializer for a class that looks like set from base python.

from weaver.registry import WeaverSerializer, WeaverRegistry
from weaver.data import WovenClass, ItemMetadataWithVersion
from weaver.version import Version
from my_package import MySet
from typing import Any, Callable

class WeaverSetSerializer(WeaverSerializer[set]):
    _metadata = ItemMetadataWithVersion(
        module=tuple(["MyLibrary"]), name="MySet", version=Version(0, 1, 1)
    )

    @classmethod
    def weave(
            cls,
            item: MySet,
            registry: WeaverRegistry,
            cache: dict[int, Any],
            weave_fn: Callable,
    ) -> WovenClass:
        return WovenClass(
            pointer=id(item),
            metadata=cls._metadata,
            artefacts=set(),
            documentation={cls._metadata: MySet.__doc__},
            method_source={},
            json={"__inner__": [weave_fn(i) for i in item]},
        )

And a deserializer on the other side for our custom class that looks a lot like set.

from weaver.registry import WeaverDeserializer, WeaverRegistry
from weaver.data import WovenClass, ItemMetadataWithVersion
from weaver.version import Version
from my_package import MySet
from typing import Any, Callable

class WeaverSetDeserializer(WeaverDeserializer[MySet]):
    _metadata = ItemMetadataWithVersion(
        module=tuple(["MyLibrary"]), name="MySet", version=Version(0, 1, 1)
    )

    @classmethod
    def unweave(
            cls,
            item: WovenClass,
            registry: WeaverRegistry,
            cache: dict[int, Any],
            unweave_fn: Callable,
    ) -> MySet:
        return MySet({unweave_fn(i) for i in item.json["__inner__"]})

So we can read from Version 0.1.1 and write to Version 0.1.1. We can also specify that we can read from 'AllVersions', although more complex constraints don't exist yet.

Isn't this Camel, but for JSON?

Yes, but with tweaks. We fall back to Pickle, they had a clear philosophy against it.

Release files for weaver-json 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for weaver-json 0.0.2
File Size Uploaded
weaver_json-0.0.2.tar.gz 11.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for weaver-json 0.0.2
File Interpreter ABI Platform
weaver_json-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 23.5 kB

Release files / weaver_json-0.0.2.tar.gz

Download URL weaver_json-0.0.2.tar.gz
Size 11.0 kB
Tags Source
SHA-256 checksum
How to use checksums
73a68d98d16e6d66843d6bb008f20e568a75b0dae30c2029bfb9d694a5940fab
BLAKE2b-256 checksum
How to use checksums
08d67650fe6b684cde1f93b8c6539334c1be33e8a5ec187cff7cf87359395e84
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.2 CPython/3.10.9 Windows/10

Release files / weaver_json-0.0.2-py3-none-any.whl

Download URL weaver_json-0.0.2-py3-none-any.whl
Size 12.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c2dd31bdb8a598099ae272c4dad977659e877d3b21f7fddd10f8acd77d2518aa
BLAKE2b-256 checksum
How to use checksums
642cec4b7c81e946e495db11a6f955b992701370869733d235244c7fc3f6f266
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.2 CPython/3.10.9 Windows/10

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page