(value)
return retVal
Returns safe representation of a given basestring value

    >>> safecharencode(u'test123')
    u'test123'
    >>> safecharencode(u'test\x01\x02\xff')
    u'test\\01\\02\\03\\ff'
Returns safe representation of a given basestring value
Returns safe representation of a given basestring value
def safecharencode(value):
    """
    Returns safe representation of a given basestring value

    >>> safecharencode(u'test123')
    u'test123'
    >>> safecharencode(u'test\x01\x02\xff')
    u'test\\01\\02\\03\\ff'
    """

    retVal = value

    if isinstance(value, basestring):
        if any(_ not in SAFE_CHARS for _ in value):
            retVal = retVal.replace('\\', SLASH_MARKER)

            for char in SAFE_ENCODE_SLASH_REPLACEMENTS:
                retVal = retVal.replace(char, repr(char).strip('\''))

            retVal = reduce(lambda x, y: x + (y if (y in string.printable or isinstance(value, unicode) and ord(y) >= 160) else '\\x%02x' % ord(y)), retVal, (unicode if isinstance(value, unicode) else str)())

            retVal = retVal.replace(SLASH_MARKER, "\\\\")
    elif isinstance(value, list):
        for i in xrange(len(value)):
            retVal[i] = safecharencode(value[i])

    return retVal
def safecharencode ( value ) : retVal = value if isinstance ( value , basestring ) : if any ( _ not in SAFE_CHARS for _ in value ) : retVal = retVal . replace ( '\\' , SLASH_MARKER ) for char in SAFE_ENCODE_SLASH_REPLACEMENTS : retVal = retVal . replace ( char , repr ( char ) . strip ( '\'' ) ) retVal = reduce ( lambda x , y : x + ( y if ( y in string . printable or isinstance ( value , unicode ) and ord ( y ) >= 160 ) else '\\x%02x' % ord ( y ) ) , retVal , ( unicode if isinstance ( value , unicode ) else str ) ( ) ) retVal = retVal . replace ( SLASH_MARKER , "\\\\" ) elif isinstance ( value , list ) : for i in xrange ( len ( value ) ) : retVal [ i ] = safecharencode ( value [ i ] ) return retVal