This function tries to identify encode in available encodings.
    If it finds, then it will convert text into unicode string.

    Author : Arulalan.T

    04.08.2014
192350
def auto2unicode(text):
    

    _all_unique_encodes_, _all_common_encodes_ = _get_unique_common_encodes()
    # get unique word which falls under any one of available encodes from
    # user passed text lines
    unique_chars = _get_unique_ch(text, _all_common_encodes_)
    # count common encode chars
    clen = len(_all_common_encodes_)
    msg = "Sorry, couldn't find encode :-(\n"
    msg += 'Need more words to find unique encode out side of %d ' % clen
    msg += 'common compound characters'
    if not unique_chars:
        print(msg)
        return ''
    # end of if not unique_chars:

    for encode_name, encode_keys in _all_unique_encodes_:
        if not len(encode_keys): continue
        for ch in encode_keys:
            # check either encode char is presnent in word
            if ch in unique_chars:
                # found encode
                print(("Found encode : ", encode_name))
                encode = _all_encodes_[encode_name]
                return encode2unicode(text, encode)
            # end of if ch in unique_chars:
        # end of ifor ch in encode_keys:
    else:
        print(msg)
        return ''
773060
def force_unicode(s, encoding='utf-8', errors='strict'):
    
    # Handle the common case first, saves 30-40% when s is an instance of
    # six.text_type. This function gets called often in that setting.
    if isinstance(s, six.text_type):
        return s
    if not isinstance(s, six.string_types):
        if six.PY3:
            if isinstance(s, bytes):
                s = six.text_type(s, encoding, errors)
            else:
                s = six.text_type(s)
        else:
            s = six.text_type(bytes(s), encoding, errors)
    else:
        # Note: We use .decode() here, instead of six.text_type(s,
        # encoding, errors), so that if s is a SafeBytes, it ends up being
        # a SafeText at the end.
        s = s.decode(encoding, errors)
    return s
1197170
def _is_ascii_encoding(encoding):
    
    if encoding is None:
        return False
    try:
        return codecs.lookup(encoding).name == "ascii"
    except LookupError:
        return False
650264
def encoded(string, encoding='utf-8'):
    
    assert isinstance(string, string_types) or isinstance(string, binary_type)
    if isinstance(string, text_type):
        return string.encode(encoding)
    try:
        # make sure the string can be decoded in the specified encoding ...
        string.decode(encoding)
        return string
    except UnicodeDecodeError:
        # ... if not use latin1 as best guess to decode the string before encoding as
        # specified.
        return string.decode('latin1').encode(encoding)
137022
def _detect_encoding(data=None):
    
    import locale
    enc_list = ['utf-8', 'latin-1', 'iso8859-1', 'iso8859-2',
                'utf-16', 'cp720']
    code = locale.getpreferredencoding(False)
    if data is None:
        return code
    if code.lower() not in enc_list:
        enc_list.insert(0, code.lower())
    for c in enc_list:
        try:
            for line in data:
                line.decode(c)
        except (UnicodeDecodeError, UnicodeError, AttributeError):
            continue
        return c
    print("Encoding not detected. Please pass encoding value manually")
178384
def is_unicode_string(string):
    
    if string is None:
        return None
    if PY2:
        return isinstance(string, unicode)
    return isinstance(string, str)
835338
def _decode_data(self, data, charset):
        

        try:
            return smart_unicode(data, charset)
        except UnicodeDecodeError:
            raise errors.BadRequest('wrong charset')
434128
def _EncodeString(self, string):
    
    try:
      # Note that encode() will first convert string into a Unicode string
      # if necessary.
      encoded_string = string.encode(self._encoding, errors=self._errors)
    except UnicodeEncodeError:
      if self._errors == 'strict':
        logging.error(
            'Unable to properly write output due to encoding error. '
            'Switching to error tolerant encoding which can result in '
            'non Basic Latin (C0) characters to be replaced with "?" or '
            '"\\ufffd".')
        self._errors = 'replace'

      encoded_string = string.encode(self._encoding, errors=self._errors)

    return encoded_string
14905
def detect_unicode(str)
      str = detect_unicode_bom(str)

      # Fallback to UTF-8
      if str.encoding == Encoding::BINARY
        str.force_encoding(Encoding::UTF_8)
      end

      str
    end
1102120
def text(string, encoding='utf8'):
    
    if isinstance(string, six.text_type):
        return string
    elif isinstance(string, six.binary_type):
        return string.decode(encoding)
    else:
        return six.text_type(string)
312079
def encode (text):
    """"""
    if isinstance(text, unicode):
        return text.encode(i18n.default_encoding, 'ignore')
    return text
612657
def to_unicode(value):
  
  if isinstance(value, str):
    return value.decode('utf-8')

  assert isinstance(value, unicode)
  return value
1002122
def guess_decode_from_terminal(text, term):
    
    if getattr(term, 'encoding', None):
        try:
            text = text.decode(term.encoding)
        except UnicodeDecodeError:
            pass
        else:
            return text, term.encoding
    return guess_decode(text)
203845
def to_unicode(text):
    
    if not text or isinstance(text, unicode if PY2 else str):
        return text

    try:
        # Try UTF-8 first
        return text.decode("UTF-8")
    except UnicodeError:
        try:
            # Then Windows Latin-1
            return text.decode("CP1252")
        except UnicodeError:
            # Give up, return byte string in the hope things work out
            return text
252200
def to_unicode(s):
    
    if not isinstance(s, TEXT):
        if not isinstance(s, bytes):
            raise TypeError('You are required to pass either unicode or '
                            'bytes here, not: %r (%s)' % (type(s), s))
        try:
            s = s.decode('utf-8')
        except UnicodeDecodeError as le:
            raise TypeError('You are required to pass either a unicode '
                            'object or a utf-8-encoded bytes string here. '
                            'You passed a bytes object which contained '
                            'non-utf-8: %r. The UnicodeDecodeError that '
                            'resulted from attempting to interpret it as '
                            'utf-8 was: %s'
                            % (s, le,))
    return s
1145017
def to_unicode(string):
    
    if isinstance(string, six.binary_type):
        return string.decode('utf8')
    if isinstance(string, six.text_type):
        return string
    if six.PY2:
        return unicode(string)
    return str(string)
193456
def make_unicode(s, encoding='utf-8', encoding_errors='strict'):
    
    if not isinstance(s, unicode):
        if not isinstance(s, basestring):
            return unicode(str(s), encoding, encoding_errors)
        return unicode(s, encoding, encoding_errors)
    return s