Next / Previous / Contents / Shipman's homepage

7.8. 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 ):
        """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 8.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 their original order (that is, in order by the keys of self.__seqMap). See Section 11.12, “BirdForm.writeNode(): Translate to XML”.

birdnotes.py
        #-- 4 --
        # [ selfNode  :=  selfNode with new rnc.FORM_N nodes added
        #       representing the values from self.__seqMap in order
        #       according to the keys from self.__seqMap ]
        sequenceKeyList  =  self.__seqMap.keys()
        sequenceKeyList.sort()
        for  sequenceKey in sequenceKeyList:
            #-- 4 body --
            # [ sequenceKey is a key in self.__seqMap ->
            #     selfNode  :=  selfNode with a new rnc.FORM_N node
            #         added representing self.__seqMap[sequenceKey] ]
            birdForm  =  self.__seqMap[sequenceKey]
            birdForm.writeNode ( selfNode )