Next / Previous / Contents / Shipman's homepage

73.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 73.1, “Design notes for the BaseEncounter class”.

baseclasses.py
# - - -   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.

baseclasses.py
        #-- 1 --
        if  self.OUT_FIELD_LIST is None:
            raise NotImplementedError, ( "Classes derived from "
                "BaseEncounter must provide an OUT_FIELD_LIST "
                "attribute." )

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.

baseclasses.py
        #-- 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.

baseclasses.py
        #-- 3 --
        return "".join ( flatList )