[lxml-dev] Pickling objectified trees
Christian Zagrodnick
cz at gocept.com
Sun Feb 25 14:12:37 CET 2007
Hi,
the other day I had to pickle objectified trees. I just thought to
share my findings.
Pickling is about serialization. IMHO the natural serialization of an
objectified tree is its XML representation. So the following basically
does that:
--------------------------
import copy_reg
import lxml.etree
import lxml.objectify
def treeFactory(state):
"""Un-Pickle factory."""
return lxml.objectify.fromstring(state)
copy_reg.constructor(treeFactory)
def reduceObjectifiedElement(object):
"""Reduce function for lxml.objectify trees.
See http://docs.python.org/lib/pickle-protocol.html for details.
"""
return (treeFactory,
(lxml.etree.tostring(object), ))
copy_reg.pickle(lxml.objectify.ObjectifiedElement,
reduceObjectifiedElement,
treeFactory)
-----------------------------------------
You might consider just registering the reduce function in lxml itself.
Shouldn't hurt, should it.
--
Christian Zagrodnick
gocept gmbh & co. kg · forsterstrasse 29 · 06112 halle/saale
www.gocept.com · fon. +49 345 12298894 · fax. +49 345 12298891
More information about the lxml-dev
mailing list