convert string to utf8 in Py2 or unicode in Py3
        :param v:
        :return:
180501
def _n(v):
        
        if v is None:
            return ""

        if isinstance(v, (dict, list)):
            try:
                v = json.dumps(v)
            except Exception:
                pass
        elif six.PY2 and isinstance(v, six.text_type):
            v = v.encode('utf8', "ignore")
        elif six.PY3 and isinstance(v, six.binary_type):
            v = v.decode('utf8', "ignore")

        return str(v)
1131343
def safe_str(value):
    
    if sys.version_info < (3,0) and isinstance(value, unicode):
        return value.encode('utf-8')
    else:
        return str(value)
394868
def utf8(value):
    
    if isinstance(value, _UTF8_TYPES):
        return value
    elif isinstance(value, unicode_type):
        return value.encode("utf-8")
    else:
        return str(value)
1233823
def _get_unicode(data, force=False):
    """"""
    if isinstance(data, binary_type):
        return data.decode('utf-8')
    elif data is None:
        return ''
    elif force:
        if PY2:
            return unicode(data)
        else:
            return str(data)
    else:
        return data
1065149
def _convert_to_unicode(string):
    
    codepoints = []
    for character in string.split('-'):
        if character in BLACKLIST_UNICODE:
            next

        codepoints.append(
            '\U{0:0>8}'.format(character).decode('unicode-escape')
        )

    return codepoints
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")
276362
def to_utf8(self, data):
        
        # if this is a unicode string, return its string representation
        if isinstance(data, unicode):
            return data.encode('utf-8')

        # if this is a list of values, return list of string values
        if isinstance(data, list):
            return [self.to_utf8(item) for item in data]

        # if this is a dictionary, return dictionary of string keys and values
        if isinstance(data, dict):
            return {
                self.to_utf8(key): self.to_utf8(value)
                for key, value in data.iteritems()
            }

        # if it's anything else, return it in its original form
        return data
1216724
def default_encode(v):
    
    if isinstance(v, ObjectId):
        return unicode_type(v)

    if isinstance(v, datetime):
        return format_time(v)

    if isinstance(v, date):
        return format_time(v)

    return v
884602
def native_string_to_unicode(s, encoding="ascii", errors="strict"):
    
    if not isinstance(s, str):
        raise TypeError("{} must be type str, not {}".format(s, type(s)))
    if str is unicode:
        # Python 3
        return s
    else:
        # Python 2
        return s.decode(encoding=encoding, errors=errors)
976307
def str_on_2_unicode_on_3(s):
    

    if not PY3:
        return str(s)
    else:  # 3+
        if not isinstance(s, str):
            return str(s, encoding="utf-8")
        return s
1277629
def unicode(self, var, default=NOTSET):
        
        return self.get_value(var, cast=str, default=default)
1017252
def pretty_unicode(string):
    
    if isinstance(string, six.text_type):
        return string
    try:
        return string.decode("utf8")
    except UnicodeDecodeError:
        return string.decode('Latin-1').encode('unicode_escape').decode("utf8")
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
285689
def to_unicode(value):
    
    if isinstance(value, _TO_UNICODE_TYPES):
        return value
    if not isinstance(value, bytes_type):
        raise TypeError(
            "Expected bytes, unicode, or None; got %r" % type(value)
        )
    return value.decode("utf-8")
1286249
def convert_unicode_2_utf8(input):
    '''
    '''
    if isinstance(input, dict):
        try:
            # python-2.6
            return dict((convert_unicode_2_utf8(key), convert_unicode_2_utf8(value))
                        for key, value
                        in input.iteritems())
        except AttributeError:
            # since python-2.7 cf. http://stackoverflow.com/a/1747827
            # [the ugly eval('...') is required for a valid syntax on
            # python-2.6, cf. http://stackoverflow.com/a/25049535]
            return eval('''{convert_unicode_2_utf8(key): convert_unicode_2_utf8(value)
                           for key, value
                           in input.items()}''')
    elif isinstance(input, list):
        return [convert_unicode_2_utf8(element) for element in input]
    # elif order relevant: python2 vs. python3
    # cf. http://stackoverflow.com/a/19877309
    elif isinstance(input, str):
        return input
    else:
        try:
            if eval('''isinstance(input, unicode)'''):
                return input.encode('utf-8')
        except NameError:
            # unicode does not exist in python-3.x
            pass
        return input
592301
def python_portable_string(string, encoding='utf-8'):
  
  if isinstance(string, six.string_types):
    return string

  if six.PY3:
    return string.decode(encoding)

  raise ValueError('Unsupported type %s' % str(type(string)))
1080785
def utf16_to_utf8(obj)
      str = obj[2, obj.size]
      str = str.unpack("n*").pack("U*")
      str.force_encoding("utf-8")
      str
    end