Next / Previous / Contents / Shipman's homepage

8.11. DayNotes.writeNode(): Internal form to XML

This method generates the XML subtree corresponding to a day-notes element. The parent argument is the element under which this tree will be a sub-element. See the definition of day-notes in the schema.

birdnotes.py
# - - -   D a y N o t e s . w r i t e N o d e

    def writeNode(self, parent, byPhylo=False):
        """Generate the XML for a day-notes.
        """

First we build a dictionary of the attributes that will be attached to the new node. For some of the attributes such as date, we could supply the attribute value by passing a keyword argument like date="1978-09-20" to the et.SubElement() constructor. However, this won't work for attribute names that have hyphens in them: Python is not going to like an argument of the form day-loc='Soc'. So, we pre-build the dictionary of attribute names and values, and pass it in using Python's convention that an argument preceded by “**” is treated as a set of keyword arguments.

The day-loc attribute is optional, and is needed only when it differs from the default location code in the DaySummary instance.

birdnotes.py
        #-- 1 --
        # [ attributes  :=  a dictionary mapping rnc.STATE_A to
        #       self.regionCode, rnc.DATE_A to self.date, and
        #       rnc.DAY_LOC_A to self.dayLoc or to None if
        #       self.dayLoc matches self.daySummary.defaultLoc ]
        attributes = {
            rnc.STATE_A: self.regionCode,
            rnc.DATE_A: self.date }
        if  self.dayLoc.code != self.daySummary.defaultLoc().code:
            attributes[rnc.DAY_LOC_A] = self.dayLoc.code

Next we create the day-notes element as a subelement of parent, using the set of attributes we just built.

birdnotes.py
        #-- 2 --
        # [ parent  :=  parent with a new rnc.DAY_NOTES_N node added
        #       with attributes=(attributes)
        #   selfNode  :=  that node ]
        selfNode = et.SubElement(parent, rnc.DAY_NOTES_N,
            attributes)

That takes care of all the XML at this level. Next we attach the new node's sub-elements: a day-summary element. See Section 9.8, “DaySummary.writeNode(): Translate to XML”.

birdnotes.py
        #-- 3 --
        # [ selfNode  :=  selfNode with a new rnc.DAY_SUMMARY_N
        #       child appended representing self.daySummary ]
        self.daySummary.writeNode(selfNode)

Next we attach the form elements in the specified order. If byPhylo is true, we use order them using the keys of self._txMap; otherwise we use the keys of self._seqMap. See Section 12.12, “BirdForm.writeNode(): Translate to XML”.

birdnotes.py
        #-- 4 --
        # [ if byPhylo ->
        #     selfNode  :=  selfNode with new rnc.FORM_N nodes added
        #       representing the values from self._txMap in order
        #       according to the keys from self._txMap ]
        #   else ->
        #     selfNode  :=  selfNode with new rnc.FORM_N nodes added
        #       representing the values from self._seqMap in order
        #       according to the keys from self._seqMap ]
        if byPhylo:
            for txKey in sorted(self._txMap.keys()):
                self._txMap[txKey].writeNode(selfNode)
        else:
            for k in sorted(self._seqMap.keys()):
                self._seqMap[k].writeNode(selfNode)