def safechardecode(value, binary=False):
 retVal = value
 if isinstance(value, basestring):
  retVal = retVal.replace('\\\\', SLASH_MARKER)
  while True:
   match = re.search(HEX_ENCODED_CHAR_REGEX, retVal)
   if match:
    retVal = retVal.replace(match.group('result'), (unichr if isinstance(value, unicode) else chr)(ord(binascii.unhexlify(match.group('result').lstrip('\\x')))))
   else:
    break
  for char in SAFE_ENCODE_SLASH_REPLACEMENTS[::(-1)]:
   retVal = retVal.replace(repr(char).strip("'"), char)
  retVal = retVal.replace(SLASH_MARKER, '\\')
  if binary:
   if isinstance(retVal, unicode):
    retVal = retVal.encode('utf8')
   while True:
    match = re.search(INVALID_UNICODE_CHAR_REGEX, retVal)
    if match:
     retVal = retVal.replace(match.group('result'), chr(ord(binascii.unhexlify(match.group('result').lstrip('\\?')))))
    else:
     break
 elif isinstance(value, (list, tuple)):
  for i in xrange(len(value)):
   retVal[i] = safechardecode(value[i])
 return retVal

def safechardecode ( value , binary = False ) : retVal = value if isinstance ( value , basestring ) : retVal = retVal . replace ( '\\\\' , SLASH_MARKER ) while True : match = re . search ( HEX_ENCODED_CHAR_REGEX , retVal ) if match : retVal = retVal . replace ( match . group ( 'result' ) , ( unichr if isinstance ( value , unicode ) else chr ) ( ord ( binascii . unhexlify ( match . group ( 'result' ) . lstrip ( '\\x' ) ) ) ) ) else : break for char in SAFE_ENCODE_SLASH_REPLACEMENTS [ : : ( - 1 ) ] : retVal = retVal . replace ( repr ( char ) . strip ( "'" ) , char ) retVal = retVal . replace ( SLASH_MARKER , '\\' ) if binary : if isinstance ( retVal , unicode ) : retVal = retVal . encode ( 'utf8' ) while True : match = re . search ( INVALID_UNICODE_CHAR_REGEX , retVal ) if match : retVal = retVal . replace ( match . group ( 'result' ) , chr ( ord ( binascii . unhexlify ( match . group ( 'result' ) . lstrip ( '\\?' ) ) ) ) ) else : break elif isinstance ( value , ( list , tuple ) ) : for i in xrange ( len ( value ) ) : retVal [ i ] = safechardecode ( value [ i ] ) return retVal
reverse function to safecharencode .