def as_xml(self, tag=None): """ Return an XML element representing ``self``. The element has tag ``tag``, or if that is omitted (or ``None``), the ``xml_default_tag`` attribute of ``self``'s class. """ tag = tag or self.xml_default_tag instance_td = Instance(self.__class__) # TODO: Cache this t.d. in class? return instance_td.xml_element(self, tag, [tag])
def serialize(obj, tag): """ Entry point function to serialize a Python object to an XML element. :param obj: Python object to serialize :type obj: instance of class having ``xml_descriptor`` attribute :return: XML element, as instance of :class:`etree.Element`. """ instance_td = Instance(obj.__class__) return instance_td.xml_element(obj, tag)
def deserialize(cls, elt, expected_tag): """ Entry point function to deserialize a Python object from an XML element. :param cls: class of object to deserialize :param elt: XML element :type elt: :class:`etree.Element` :return: instance of class ``cls``. """ instance_td = Instance(cls) return instance_td.extract_from(elt, expected_tag)