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

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
returns safe representation of a given basestring value .