(format_, params)
return retVal
Avoids problems with inappropriate string format strings

    >>> safeStringFormat('SELECT foo FROM %s LIMIT %d', ('bar', '1'))
    u'SELECT foo FROM bar LIMIT 1'
Avoids problems with inappropriate string format strings
Avoids problems with inappropriate string format strings
def safeStringFormat(format_, params):
    """
    Avoids problems with inappropriate string format strings

    >>> safeStringFormat('SELECT foo FROM %s LIMIT %d', ('bar', '1'))
    u'SELECT foo FROM bar LIMIT 1'
    """

    if format_.count(PAYLOAD_DELIMITER) == 2:
        _ = format_.split(PAYLOAD_DELIMITER)
        _[1] = re.sub(r"(\A|[^A-Za-z0-9])(%d)([^A-Za-z0-9]|\Z)", r"\g<1>%s\g<3>", _[1])
        retVal = PAYLOAD_DELIMITER.join(_)
    else:
        retVal = re.sub(r"(\A|[^A-Za-z0-9])(%d)([^A-Za-z0-9]|\Z)", r"\g<1>%s\g<3>", format_)

    if isinstance(params, basestring):
        retVal = retVal.replace("%s", params, 1)
    elif not isListLike(params):
        retVal = retVal.replace("%s", str(params), 1)
    else:
        start, end = 0, len(retVal)
        match = re.search(r"%s(.+)%s" % (PAYLOAD_DELIMITER, PAYLOAD_DELIMITER), retVal)
        if match and PAYLOAD_DELIMITER not in match.group(1):
            start, end = match.start(), match.end()
        if retVal.count("%s", start, end) == len(params):
            for param in params:
                index = retVal.find("%s", start)
                retVal = retVal[:index] + getUnicode(param) + retVal[index + 2:]
        else:
            if any('%s' in _ for _ in conf.parameters.values()):
                parts = format_.split(' ')
                for i in xrange(len(parts)):
                    if PAYLOAD_DELIMITER in parts[i]:
                        parts[i] = parts[i].replace(PAYLOAD_DELIMITER, "")
                        parts[i] = "%s%s" % (parts[i], PAYLOAD_DELIMITER)
                        break
                format_ = ' '.join(parts)

            count = 0
            while True:
                match = re.search(r"(\A|[^A-Za-z0-9])(%s)([^A-Za-z0-9]|\Z)", retVal)
                if match:
                    if count >= len(params):
                        raise Exception("wrong number of parameters during string formatting")
                    else:
                        retVal = re.sub(r"(\A|[^A-Za-z0-9])(%s)([^A-Za-z0-9]|\Z)", r"\g<1>%s\g<3>" % params[count], retVal, 1)
                        count += 1
                else:
                    break
    return retVal
def safeStringFormat ( format_ , params ) : if format_ . count ( PAYLOAD_DELIMITER ) == 2 : _ = format_ . split ( PAYLOAD_DELIMITER ) _ [ 1 ] = re . sub ( r"(\A|[^A-Za-z0-9])(%d)([^A-Za-z0-9]|\Z)" , r"\g<1>%s\g<3>" , _ [ 1 ] ) retVal = PAYLOAD_DELIMITER . join ( _ ) else : retVal = re . sub ( r"(\A|[^A-Za-z0-9])(%d)([^A-Za-z0-9]|\Z)" , r"\g<1>%s\g<3>" , format_ ) if isinstance ( params , basestring ) : retVal = retVal . replace ( "%s" , params , 1 ) elif not isListLike ( params ) : retVal = retVal . replace ( "%s" , str ( params ) , 1 ) else : start , end = 0 , len ( retVal ) match = re . search ( r"%s(.+)%s" % ( PAYLOAD_DELIMITER , PAYLOAD_DELIMITER ) , retVal ) if match and PAYLOAD_DELIMITER not in match . group ( 1 ) : start , end = match . start ( ) , match . end ( ) if retVal . count ( "%s" , start , end ) == len ( params ) : for param in params : index = retVal . find ( "%s" , start ) retVal = retVal [ : index ] + getUnicode ( param ) + retVal [ index + 2 : ] else : if any ( '%s' in _ for _ in conf . parameters . values ( ) ) : parts = format_ . split ( ' ' ) for i in xrange ( len ( parts ) ) : if PAYLOAD_DELIMITER in parts [ i ] : parts [ i ] = parts [ i ] . replace ( PAYLOAD_DELIMITER , "" ) parts [ i ] = "%s%s" % ( parts [ i ] , PAYLOAD_DELIMITER ) break format_ = ' ' . join ( parts ) count = 0 while True : match = re . search ( r"(\A|[^A-Za-z0-9])(%s)([^A-Za-z0-9]|\Z)" , retVal ) if match : if count >= len ( params ) : raise Exception ( "wrong number of parameters during string formatting" ) else : retVal = re . sub ( r"(\A|[^A-Za-z0-9])(%s)([^A-Za-z0-9]|\Z)" , r"\g<1>%s\g<3>" % params [ count ] , retVal , 1 ) count += 1 else : break return retVal