Next / Previous / Contents / Shipman's homepage

74.5. BaseEncounter.flatten(): Convert to a flat-file record

This method converts the instance into a flat-file record, represented as a single string. For a discussion of the operation of this method, see Section 74.1, “Design notes for the BaseEncounter class”.
# - - -   B a s e E n c o u n t e r . f l a t t e n   - - -

    def flatten(self):
        '''Return self as a flat-file record.

First we check to make sure the derived class provided the table that drives our logic.
        #-- 1 --
        if  self.OUT_FIELD_LIST is None:
            raise NotImplementedError("Classes derived from "
                "BaseEncounter must provide an OUT_FIELD_LIST "

Next, we build up a list named flatList containing the flattened version of each field named in OUT_FIELD_LIST. This is a one-liner in Python, using a list comprehension. List comprehensions are discussed in List displays in the online Python documentation.
        #-- 2 --
        # [ flatList  :=  a list of strings representing flattened
        #       values of fields in self as defined by the class
        #       names and field names in self.OUT_FIELD_LIST ]
        flatList = [ fClass.flatten(getattr(self, fName))
                       for fClass, fName in self.OUT_FIELD_LIST ]

All that remains is to return the concatenation of all these strings.
        #-- 3 --
        return "".join(flatList)