Decode element to unicode if necessary.
435172
def _decode_element(self, line):
        """"""
        if not self.encoding:
            return line
        if isinstance(line, str) and self.default_encoding:
            return line.decode(self.default_encoding)
        return line
278974
def convert_unicode(value):
    
    if isinstance(value, dict):
        return {convert_unicode(key): convert_unicode(value)
                for key, value in value.iteritems()}
    elif isinstance(value, list):
        return [convert_unicode(item) for item in value]
    elif isinstance(value, unicode):
        return value.encode('utf-8')
    else:
        return value
49307
def _unicode_to_native(s):
  """"""
  if six.PY2:
    return s.encode("utf-8") if isinstance(s, unicode) else s
  else:
    return s
737269
def to_unicode(value):
    
    if isinstance(value, _TO_UNICODE_TYPES):
        return value
    if not isinstance(value, bytes):
        raise TypeError(
            "Expected bytes, unicode, or None; got %r" % type(value))
    return value.decode("utf-8")
387992
def Decode(self, attribute, value):
    """"""
    required_type = self._attribute_types.get(attribute, "bytes")
    if required_type == "integer":
      return rdf_structs.SignedVarintReader(value, 0)[0]
    elif required_type == "unsigned_integer":
      return rdf_structs.VarintReader(value, 0)[0]
    elif required_type == "string":
      if isinstance(value, bytes):
        return value.decode("utf-8")
      else:
        return utils.SmartUnicode(value)
    else:
      return value
328891
def _safe_unicode(o):
    
    def clean(s):
        return u''.join([c if c in ASCII_PRINTABLE else '?' for c in s])
    if USING_PYTHON2:
        try:
            return unicode(o)
        except:
            try:
                s = str(o)
                try:
                    return s.decode("utf-8")
                except:
                    return clean(s[:2048]) + u" [Raw error message: " + unicode(s.encode("hex"), 'utf-8') + u"]"
            except:
                return u"(Unable to decode Python exception message)"
    else:
        return str(o)
1202118
def serialize(element, strip=False):
    
    text = etree.tostring(element, method='text', encoding='utf-8')
    if strip:
        text = text.strip()
    return str(text, encoding='utf-8')
486635
def deep_unicode(s, encodings=None):
    """"""
    if encodings is None:
        encodings = ['utf-8', 'latin-1']
    if isinstance(s, (list, tuple)):
        return [deep_unicode(i) for i in s]
    if isinstance(s, dict):
        return dict([
            (deep_unicode(key), deep_unicode(s[key]))
            for key in s
        ])
        # in_dict = {}
        # for key in s:
        #     in_dict[to_unicode(key)] = to_unicode(s[key])
        # return in_dict
    elif isinstance(s, str):
        for encoding in encodings:
            try:
                return s.decode(encoding)
            except:
                pass
    return s
1121473
def to_unicode(path, errors="replace"):
    """"""
    if isinstance(path, UNICODE):
        return path
    return path.decode(sys.getfilesystemencoding(), errors)
7622
def _to_unicode(self, data, encoding, errors='strict'):
    ''''''

    # strip Byte Order Mark (if present)
    if (len(data) >= 4) and (data[:2] == '\xfe\xff') and (data[2:4] != '\x00\x00'):
        encoding = 'utf-16be'
        data = data[2:]
    elif (len(data) >= 4) and (data[:2] == '\xff\xfe') and (data[2:4] != '\x00\x00'):
        encoding = 'utf-16le'
        data = data[2:]
    elif data[:3] == '\xef\xbb\xbf':
        encoding = 'utf-8'
        data = data[3:]
    elif data[:4] == '\x00\x00\xfe\xff':
        encoding = 'utf-32be'
        data = data[4:]
    elif data[:4] == '\xff\xfe\x00\x00':
        encoding = 'utf-32le'
        data = data[4:]
    newdata = unicode(data, encoding, errors)
    return newdata
594507
def u2s(self,u):
		""""""

		try:
			return u.encode('utf-8',errors='ignore')
		except (UnicodeDecodeError,AttributeError) as e:
			try:
				return str(u)
			except UnicodeEncodeError:
				return unicode(u).encode('utf-8',errors='ignore')
677728
def ConvertToWireFormat(self, value):
    """"""
    value = value.encode("utf8")
    return (self.encoded_tag, VarintEncode(len(value)), value)
1063190
def get_unicode_str(obj):
    """"""
    if isinstance(obj, six.text_type):
        return obj
    if isinstance(obj, six.binary_type):
        return obj.decode("utf-8", errors="ignore")
    return six.text_type(obj)
369812
def decode(var, encoding):
    
    if PY2:
        if isinstance(var, unicode):
            ret = var
        elif isinstance(var, str):
            if encoding:
                ret = var.decode(encoding)
            else:
                ret = unicode(var)
        else:
            ret = unicode(var)
    else:
        ret = str(var)
    return ret
1345381
def to_unicode(x, unaccent=False):
    """"""
    s = str(x)

    if unaccent:
        cs = [c for c in unicodedata.normalize('NFD', s)
              if unicodedata.category(c) != 'Mn']
        s = ''.join(cs)

    return s
1007426
def utf8(item):
    """"""
    if isinstance(item, list):
        return [utf8(i) for i in item]
    if isinstance(item, tuple):
        return tuple([utf8(i) for i in item])
    if isinstance(item, dict):
        return {utf8(k): utf8(v) for k, v in item.items()}
    try:
        return item.decode('utf-8')
    except:  # bare except since this method is supposed to be safe anywhere
        return item
1108228
def _to_unicode(instr):
    '''
    
    '''
    if instr is None or isinstance(instr, six.text_type):
        return instr
    else:
        return six.text_type(instr, 'utf8')