import sys


def safe_utf8_encode(s):
    """If string is a binary string - return it as is, if string is a unicode string - encode it in UTF-8

    :type s: str | unicode
    :rtype: str
    """
    if sys.version_info.major >= 3:
        if isinstance(s, bytes):
            return s
        elif isinstance(s, str):
            return s.encode('utf-8')
        else:
            return bytes(s)
    else:
        if isinstance(s, str):
            return s
        elif isinstance(s, unicode):
            return s.encode('utf-8')
        else:
            return str(s)


def safe_utf8_decode(s):
    """If string is a unicode string - return it as is, if string is a binary string - decode it with UTF-8

    :type s: str | unicode
    :rtype: unicode
    """
    if sys.version_info.major >= 3:
        if isinstance(s, str):
            return s
        elif isinstance(s, bytes):
            return s.decode('utf-8')
        else:
            return str(s)
    else:
        if isinstance(s, unicode):
            return s
        elif isinstance(s, str):
            return s.decode('utf-8')
        else:
            return unicode(s)
