nl: replace '%s' in string "CREATE %sINDEX %s ON %s (%s);" with: unique, return value of the function qn called with an argument:
code: index_output . append ( "CREATE %sINDEX %s ON %s (%s);" % ( unique , qn ( '%s_%s' % ( tablename , f . name ) ) , qn ( tablename ) , qn ( f . name ) ) )