Zodbpickle

4.3 · active · verified Thu Apr 16

Zodbpickle is a fork of Python's built-in `pickle` module, primarily designed to provide a uniform pickling interface for ZODB (Zope Object Database). It extends Python 2.7's `pickle` and `cPickle` to support protocol 3 opcodes and introduces `zodbpickle.binary` for consistent binary value handling across Python 2 and 3. For Python 3, it forks the `pickle` module to re-add support for the `noload()` operation, which ZODB utilizes. The library is currently active, with version 4.3 as of the last check, and aims to ensure seamless data serialization and deserialization in ZODB environments spanning different Python versions.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic serialization (pickling) and deserialization (unpickling) of a custom Python object using `zodbpickle.pickle`. It shows how to replace the standard `pickle` import to leverage `zodbpickle`'s specialized features, particularly useful for ZODB environments.

from zodbpickle import pickle

class MyObject:
    def __init__(self, name, value):
        self.name = name
        self.value = value

    def __eq__(self, other):
        if not isinstance(other, MyObject):
            return NotImplemented
        return self.name == other.name and self.value == other.value

# Pickle an object
obj = MyObject('example', 123)
pickled_obj = pickle.dumps(obj, protocol=pickle.HIGHEST_PROTOCOL)
print(f"Pickled object (bytes): {pickled_obj}")

# Unpickle the object
unpickled_obj = pickle.loads(pickled_obj)
print(f"Unpickled object: {unpickled_obj.name}, {unpickled_obj.value}")

assert obj == unpickled_obj

view raw JSON →