Next / Previous / Contents / Shipman's homepage

89.3. flattenRecord(): Flatten one record

The header argument contains the field definitions, and the record argument contains one record. This function pads each field to its full length, concatenates them, and writes them to the standard output. See Section 90.4, “DbfRecord: Record object”.

dbflatten
# - - -   f l a t t e n R e c o r d   - - -

def flattenRecord(header, record):
    '''Flatten and write one record.

      [ (header is the DbfHeader object defining the
        format of the file) and
        (record is a record from that file as a DbfRecord object) ->
          sys.stdout  +:=  record, with each field padded
              to the length for that field in header ]
    '''

First, we use a list comprehension to assemble a list of the padded fields. See Section 89.4, “flattenField(): Flatten one field”.

dbflatten
    #-- 1 --
    # [ fieldList  :=  a list of the fields from record in order,
    #       converted to strings, and padded to their full length
    #       according to header ]
    fieldList = [ flattenField(f, record[f.name])
                  for f in header.fields]

    #-- 2 --
    # [ fieldList is a list of strings ->
    #       sys.stdout  +:=  concatenation of the elements
    #                        of fieldList ]
    print "".join(fieldList)