newb comment request (uncommented code + sample data)

Alexandre ac007 at bluewin.ch
Thu Nov 27 10:24:20 EST 2003


######################## <EIPyFormatToXML> ######################################
import sys
import types

_data = [['Table1',(['Field01','Field02','Field03',],
[['a string', 12345, 1.000123],
['a second string', None, 3406.3],
['', 64654564, 35]])],
['Table2', (['Field04', 'Field05'], [[None, -0.3]])],
['Table3',(['Field06', 'Field07', 'Field08'],
[['', 0, 0.001],
['', None, 646464.0],
['', 6546, 0.1],
['', -6444, 0.2],
['', 0, 0.3]])]]

def ExtractTablesFromData(data):
    """Extracts all the table names from the Dumped items data file and returns the list."""
    tablesR = []
    for tables in data:
        tablesR.append([tables[0]])
    return tablesR

def ExtractFieldNamesFromData(data):
    """Extract all fields from data list (the calling function defines for which table in 'data' argument)."""
    fieldsR = []
    for fields in data:
        fieldsR.append([fields])
    return fieldsR

def ExtractFieldValuesFromData(data, indexField):
    """Check each value of the field passed as argument to the function."""
    values , floatPrecision, NoneValues = [], None, False
    valueType, maxLength, maxValue, minValue = None, None, None, 999999999999
    sampleValue = 'numeric value, check min and max values as sample'
    for valuesD in data:
        value = valuesD[indexField]
        if type(value) is not types.NoneType:
            valueType = type(value)
        else:
            NoneValues = True
        if valueType is str:
            minValue = None
            if len(value) > maxLength:
                maxLength = len(value)
                sampleValue = value
        else:
            if value > maxValue:
                maxValue = value
            if value and value < minValue:
                minValue = value
            if valueType is float and value != 0:
                precisionTemp = len(str(value - int(value)))-2
                if precisionTemp > floatPrecision:
                    floatPrecision = precisionTemp
    if valueType is float and floatPrecision == None:
        floatPrecision = 1
    if valueType is not float and floatPrecision != None:
        valueType = type(1.234)
    if valueType is str and maxLength == 0:
        NoneValues = True
    if minValue == 999999999999:
        minValue = None
    values[:] = [valueType, maxLength, maxValue, minValue, floatPrecision, NoneValues, sampleValue]
    return values

def AddFieldsPerTable():
    """Appends field list to each table."""
    tables = ExtractTablesFromData(_data)
    for i, table in enumerate(tables):
        fields = ExtractFieldNamesFromData(_data[i][1][0])
        tables[i].append(fields)
    return tables

def AddFieldsDetailsPerField():
    """Extend field list with details for each field."""
    tables = AddFieldsPerTable()
    for iTable, table in enumerate(tables):
        for iField, field in enumerate(table[1]):
            values = ExtractFieldValuesFromData(_data[iTable][1][1], iField)
            field.extend(values)
    return tables

def AddNbOfRecordsPerTable():
    """Extend 'tables' details with number of records per table."""
    tables = AddFieldsDetailsPerField()
    for i, table in enumerate(tables):
        nbOfRecords = len(_data[i][1][1])
        table.insert(1, nbOfRecords)
    return tables

def WriteFileTableFormat(fileName):
    tables = AddNbOfRecordsPerTable()
    f = open(fileName, 'w')
    f.write("""<?xml version="1.0" encoding="ISO-8859-1"?>\n""")
    f.write("<Root>\n")

    for table in tables:
        f.write("\t<table>\n")
        f.write("\t\t<name>%s</name>\n" % table[0])
        f.write("\t\t<nbOfRecords>%s</nbOfRecords>\n" % table[1])
        for field in table[2][:]:
            f.write("\t\t<field>\n")
            f.write("\t\t\t<name>%s</name>\n" % field[0])
            if str(field[1])[:7] == "<type '":
                field[1] = str(field[1])[7:-2]
            f.write("\t\t\t<pythonType>%s</pythonType>\n" % str(field[1]))
            f.write("\t\t\t<maxLength>%s</maxLength>\n" % str(field[2]))
            f.write("\t\t\t<maxValue>%s</maxValue>\n" % str(field[3]))
            f.write("\t\t\t<minValue>%s</minValue>\n" % str(field[4]))
            f.write("\t\t\t<floatPrecision>%s</floatPrecision>\n" % str(field[5]))
            f.write("\t\t\t<NoneValues>%s</NoneValues>\n" % str(field[6]))
            f.write("\t\t\t<sampleValue>%s</sampleValue>\n" % str(field[7]))
            f.write("\t\t\t<mysqlFieldType></mysqlFieldType>\n")
            f.write("\t\t</field>\n")
        f.write("\t</table>\n")

    f.write("</Root>")
    f.close

WriteFileTableFormat('EITablesFormat.xml')

#################################### </EIPyFormatToXML>



-> result xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<Root>

<table>

<name>Table1</name>

<nbOfRecords>3</nbOfRecords>

<field>

<name>Field01</name>

<pythonType>str</pythonType>

<maxLength>15</maxLength>

<maxValue>None</maxValue>

<minValue>None</minValue>

<floatPrecision>None</floatPrecision>

<NoneValues>False</NoneValues>

<sampleValue>a second string</sampleValue>

<mysqlFieldType/>

</field>

<field>

<name>Field02</name>

<pythonType>int</pythonType>

<maxLength>None</maxLength>

<maxValue>64654564</maxValue>

<minValue>12345</minValue>

<floatPrecision>None</floatPrecision>

<NoneValues>True</NoneValues>

<sampleValue>numeric value, check min and max values as sample</sampleValue>

<mysqlFieldType/>

</field>

<field>

<name>Field03</name>

<pythonType>float</pythonType>

<maxLength>None</maxLength>

<maxValue>3406.3</maxValue>

<minValue>1.000123</minValue>

<floatPrecision>6</floatPrecision>

<NoneValues>False</NoneValues>

<sampleValue>numeric value, check min and max values as sample</sampleValue>

<mysqlFieldType/>

</field>

</table>

<table>

<name>Table2</name>

<nbOfRecords>1</nbOfRecords>

<field>

<name>Field04</name>

<pythonType>None</pythonType>

<maxLength>None</maxLength>

<maxValue>None</maxValue>

<minValue>None</minValue>

<floatPrecision>None</floatPrecision>

<NoneValues>True</NoneValues>

<sampleValue>numeric value, check min and max values as sample</sampleValue>

<mysqlFieldType/>

</field>

<field>

<name>Field05</name>

<pythonType>float</pythonType>

<maxLength>None</maxLength>

<maxValue>-0.3</maxValue>

<minValue>-0.3</minValue>

<floatPrecision>2</floatPrecision>

<NoneValues>False</NoneValues>

<sampleValue>numeric value, check min and max values as sample</sampleValue>

<mysqlFieldType/>

</field>

</table>

<table>

<name>Table3</name>

<nbOfRecords>5</nbOfRecords>

<field>

<name>Field06</name>

<pythonType>str</pythonType>

<maxLength>0</maxLength>

<maxValue>None</maxValue>

<minValue>None</minValue>

<floatPrecision>None</floatPrecision>

<NoneValues>True</NoneValues>

<sampleValue/>

<mysqlFieldType/>

</field>

<field>

<name>Field07</name>

<pythonType>int</pythonType>

<maxLength>None</maxLength>

<maxValue>6546</maxValue>

<minValue>-6444</minValue>

<floatPrecision>None</floatPrecision>

<NoneValues>True</NoneValues>

<sampleValue>numeric value, check min and max values as sample</sampleValue>

<mysqlFieldType/>

</field>

<field>

<name>Field08</name>

<pythonType>float</pythonType>

<maxLength>None</maxLength>

<maxValue>646464.0</maxValue>

<minValue>0.001</minValue>

<floatPrecision>3</floatPrecision>

<NoneValues>False</NoneValues>

<sampleValue>numeric value, check min and max values as sample</sampleValue>

<mysqlFieldType/>

</field>

</table>

</Root>






More information about the Python-list mailing list