def to_unicode(obj, encoding='utf-8', force_string=False):
    """
    This decodes a suitable object to the unicode format.

    Args:
        obj (any): Object to decode to unicode.
        encoding (str, optional): The encoding type to use for the
            dedoding.
        force_string (bool, optional): Always convert to string, no
            matter what type `obj` is initially.

    Returns:
        result (unicode or any): Will return a unicode object if input
            was a string. If input was not a string, the original will be
            returned unchanged unless `force_string` is also set.

    Notes:
        One needs to encode the obj back to utf-8 before writing to disk
        or printing. That non-string objects are let through without
        conversion is important for e.g. Attributes.

    """

    if force_string and not isinstance(obj, basestring):
        # some sort of other object. Try to
        # convert it to a string representation.
        if hasattr(obj, '__str__'):
            obj = obj.__str__()
        elif hasattr(obj, '__unicode__'):
            obj = obj.__unicode__()
        else:
            # last resort
            obj = str(obj)

    if isinstance(obj, basestring) and not isinstance(obj, unicode):
        try:
            obj = unicode(obj, encoding)
            return obj
        except UnicodeDecodeError:
            for alt_encoding in ENCODINGS:
                try:
                    obj = unicode(obj, alt_encoding)
                    return obj
                except __HOLE__:
                    pass
        raise Exception("Error: '%s' contains invalid character(s) not in %s." % (obj, encoding))
    return obj
dataset/ETHPy150Open evennia/evennia/evennia/utils/utils.py/to_unicode