Next / Previous / Contents / TCC Help System / NM Tech homepage

4.9. writeOutput(): Generate the Python file

This function writes the actual Python declarations to the output file. This output is prefaced by a brief Python comment warning users not to edit the file, that it is produced by this script.

pyrang
def writeOutput(args, outFile, nameTable):
    """Output the Python assignment statements.

      [ (args is an Args object) and
        (outFile is a writeable file) and
        (nameTable is a dictionary whose keys are Python names
        and each value is a string) ->
            outFile  +:=  (opening comment) + (lines of the
                form 'name = value', one for each entry in
                nameTable) ]
    """

    #-- 1
    print(
        "'''Do not edit this file.  It was produced automatically\n"
        "   from the {0} schema by the pyrang script at\n"
        "   http://www.nmt.edu/~shipman/soft/pyrang/.\n"
        "'''".format(args.inFileName), file=outFile)

Since the ordering of entries in a dictionary is arbitrary, we'll extract the keys and sort them so the output won't be completely random.

pyrang
    #-- 2
    # [ outFile  +:  (lines of the form 'name = value', one for
    #       each entry in nameTable) ]
    keyList = nameTable.keys()
    keyList.sort()
    for key in keyList:
        print("{0} = '{1}'".format(key, nameTable[key]), file=outFile)

    outFile.close()