Linux Format

Your FleXML friend

-

We’ve opted to use XML to serialise (store) our data. This is well-supported in Python through the ElementTre­e module and means that we can store and manipulate the XML tree in memory, making for trivial loading and saving. If the program cared about human-readabilit­y, a calendar containing a single appointmen­t would look like:

<calendar> <appt> <date> <year>2015</year> <month>0</month> <day>1</day> </date> <detail>Happy new year</detail> </appt> </calendar>

You’ll notice that the calendar widget is 0-indexed for the month, but nothing else. We admit this is a little strange but it’s easy to work around. Sadly, this program doesn’t care very much about readabilit­y, so everything’s stored on a single line. The XML tree object is stored in the variable self.root .

The loadFile() function attempts to load it from a file, but if this is not found an empty tree is created instead. We can then refer to the

saveAppt() to see how to create the required structure as follows:

def saveAppt(self, *args): … newAppt = ET.SubElement(self.root,‘appt’) newDate = ET.SubElement(newAppt, ‘date’) Year = ET.SubElement(newDate, ‘year’) Month = ET.SubElement(newDate, ‘month’) Day = ET.SubElement(newDate, ‘day’) Year.text = str(date[0]) Month.text = str(date[1]) Day.text = str(date[2]) newDetail = ET.SubElement(newAppt,‘detail’) newDetail.text = detail

Newspapers in English

Newspapers from Australia