Next / Previous / Contents / Shipman's homepage

26. Sox.write()

sox.py
# - - -   S o x . w r i t e

    def write(self, *p):
        '''Write content in the current element.
        '''

The arguments to this method are treated the same as the the content arguments to Sox.start(): they are converted to Unicode, then to UTF-8 ASCII, then written to the ouput file.

We can use Section 16, “Sox._sortParams(): Classify content and attribute arguments” to do the work of converting our arguments to a list of Unicode strings. That method, however, allows dictionaries in its “p” argument, but this method stipulates that dictionaries are not allowed. However, if any dictionaries are present, the attribute dictionary returned by ._sortParams() will be nonempty.

sox.py
        #-- 1
        # [ contentList  :=  a list of Unicode strings made from
        #       the elements of p that are not dictionaries
        #   attribs  :=  a dictionary of attributes made from
        #       the elements of p that are dictionaries ]
        contentList, attribs = self._sortParams(p, {})

        #-- 2
        if len(attribs) > 0:
            raise SoxError("Dictionaries are not allowed in calls "
                "to Sox.write().")

Next, we write the elements of contentList to the output file, with non-ASCII characters escaped.

sox.py
        #-- 2
        # [ self.outFile  +:=  elements of contentList as UTF-8 ]
        uText = u''.join(contentList)
        self.outFile.write(uText.encode('utf-8'))