EZobject2xml

Easily save and load any object data to/from a xml file


Keywords
easy-to-use, object, python, xml
License
MIT
Install
pip install EZobject2xml==1.0.2

Documentation

EZobject2xml

Save and load easily any python object data into/from a xml file.

This module has only one object xmltool with 2 functions.

For saving an object into a xml file, you just have to code (python):

from EZobject2xml.xmltoolMod import *
myXmltool = xmltool()
myXmltool.saveObject2xml(myObject,myFilename)

where:

myObject is the object to save

myFileName if the xml file

And for loading:

myXmltool.loadObjectFromXml(myObject,myFilename)

Not wanna save a data of your object ?:

Just add "__" at the end of the name of the data and "saveObject2xml" function won't save this data.

Wanna save a single value data such as integer, float, string, set, tuple, list or dict ?:

You just have to use SingleType class object:

from EZobject2xml.xmltoolMod import *
myxmltool = xmltool()
myvalue_object = SingleType(myvalue)
myxmltool.saveObject2xml(myvalue_object,'myvalue.xml')

For loading:

myxmltool.loadObjectFromXml(myvalue_object,'myvalue.xml')
myvalue = myvalue_object.value

where:

myvalue is any basic data type as int, float, str, set, tuple, list or dict

Exception:

If objects have to be dynamically initialized during the data reading process (i.e. if your object has a list of an undefined number of other objects), we have to create them first before reading their saved data. In order to do that, we give a list of object class that we could use to create a new instance of these objects.

This list of class objects is a initDataList object and we pass it as:

myInitDataList = initDataList()
myInitDataList.addInitData(mySubObject1Class,(a tuple containing the parameters to initialize this SubObject))
myInitDataList.addInitData(mySubObject2Class,(a tuple containing the parameters to initialize this SubObject))
...

myXmltool = xmltool()
myXmltool.loadObjectFromXml(myObject,myFilename,myInitDataList)

where:

mySubObject1Class is the class of an object which can be contained in myObject

myObject is the object to save

myFileName if the xml file

As seen, myInitDataList is optional and only needed when your object has a list of an undefined number of other objects.

The test file has a concrete example.

Please see the python help() function to see more technical information about these functions.

To install the package :

-m pip install EZobject2xml