def autogen_python(types, fields):
    global write_buffer

    functions = {}

    writeln("\n%s\n" % banner_str('BEGIN FIELD TYPES'))

    for _, type in sorted(types.iteritems()):

        write("FieldType(%s, FieldTypeBase(%s), " % (repr(type.name), FIELD_BASE_TYPES[type.base_type]))

        if type.name in SPECIAL_TYPES_ALL:
            special_type = SPECIAL_TYPES_ALL[type.name]
            if type.base_type != special_type.base_type:
                raise Exception("Type misatch on '%s'" % type.name)

            func_name = special_type.func_name
            if not special_type.func_name:
                func_name = '_convert_%s' % type.name.replace('-', '_')
            functions.setdefault(func_name, []).append(type.name)

            writeln("%s)  # base type: %s\n" % (func_name, type.base_type))

        else:
            writeln("{  # base type: %s" % type.base_type)
            for _, value in sorted(type.values.iteritems()):
                writeln("    %s: %s," % (value.value, repr(value.name)))
            writeln("})\n")

    writeln("\n%s\n" % banner_str('BEGIN MESSAGE TYPES'))

    for msg_num, message in sorted(types['mesg_num'].values.iteritems()):
        msg_name = message.name

        writeln("MessageType(%s, %s, {" % (msg_num, repr(msg_name)))

        msg_fields = fields[msg_name]
        for f_num, field in sorted(msg_fields.iteritems()):
            write("    %s: " % f_num)

            def field_gen(field):
                is_base_type = False
                is_special_function_type = False
                write("%s(%s, " % (field.__class__.__name__, repr(field.name)))

                special_type_name = "%s-%s" % (msg_name, field.name)
                # Predefined type
                if field.type in types or special_type_name in types:
                    type_name = field.type
                    special_type = SPECIAL_TYPES_ALL.get(special_type_name)
                    if special_type:
                        type_name = special_type_name
                        is_special_function_type = True
                        if special_type.base_type != field.type:
                            raise Exception("Type misatch on '%s'" % field.name)

                    write("FieldType(%s)," % repr(type_name))

                # Base type
                elif field.type in FIELD_BASE_TYPES:
                    write("FieldTypeBase(%s)," % FIELD_BASE_TYPES[field.type])
                    is_base_type = True
                else:
                    raise Exception("Unknown field type: %s" % field.type)

                write(" %s, %s, %s" % (
                    repr(field.units), repr(field.scale), repr(field.offset)))

                if isinstance(field, DynamicField):
                    write(", {")
                else:
                    write('),')

                write("  # base type: ")
                if is_base_type or is_special_function_type:
                    writeln(field.type)
                else:
                    writeln(types[field.type].base_type)

            field_gen(field)

            if isinstance(field, DynamicField):
                for ref_name, dynamic_fields in field.possibilities.iteritems():
                    writeln('        %s: {' % repr(ref_name))
                    for ref_value, dynamic_field in dynamic_fields.iteritems():
                        write('            %s: ' % repr(ref_value))
                        field_gen(dynamic_field)
                    writeln('        },')
                writeln('    }),')
        writeln("})\n")

    writeln("\n%s\n" % banner_str('DELETE CONVERSION FUNCTIONS'))

    for func in sorted(set(functions.iterkeys())):
        writeln("del %s" % func)

    writeln("\n\n%s" % banner_str('AUTOGENERATION COMPLETE'))

    # Prepend a required functions header to write_buffer
    req_func_out = '#' * BANNER_PADDING + "\n#\n"
    req_func_out += "# Please define the following functions (types that use them are listed):\n#\n"
    for func_name, type_names in sorted(functions.iteritems()):
        req_func_out += "#  %s\n" % func_name
        req_func_out += "#    * Used by types:\n"
        for type_name in type_names:
            req_func_out += "#       - %s\n" % type_name
        req_func_out += '#\n'
    req_func_out += ('#' * BANNER_PADDING) + "\n\n"

    write_buffer = req_func_out + write_buffer
dataset/ETHPy150Open dtcooper/python-fitparse/scripts/generate_profile.py/autogen_python/original