Converts given string to ascii ignoring non ascii.
    Args:
        s (text or binary):

    Returns:
        str:
171859
def _to_ascii(s):
    
    # TODO: Always use unicode within ambry.
    from six import text_type, binary_type
    if isinstance(s, text_type):
        ascii_ = s.encode('ascii', 'ignore')
    elif isinstance(s, binary_type):
        ascii_ = s.decode('utf-8').encode('ascii', 'ignore')
    else:
        raise Exception('Unknown text type - {}'.format(type(s)))
    return ascii_
752658
def deserialize_number(s, fmt=SER_BINARY):
    
    ret = gmpy.mpz(0)
    if fmt == SER_BINARY:
        if isinstance(s, six.text_type):
            raise ValueError(
                "Encode `s` to a bytestring yourself to" +
                " prevent problems with different default encodings")
        for c in s:
            ret *= 256
            ret += byte2int(c)
        return ret
    assert fmt == SER_COMPACT
    if isinstance(s, six.text_type):
        s = s.encode('ascii')
    for c in s:
        ret *= len(COMPACT_DIGITS)
        ret += R_COMPACT_DIGITS[c]
    return ret
1031170
def nts(s, encoding, errors):
    
    p = s.find(b"\0")
    if p != -1:
        s = s[:p]
    return s.decode(encoding, errors)
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)
1161598
def encode(self, s):
        
        if isinstance(s, basestring) and self.__needs_encoding(s):
            for x in self.encodings:
                s = re.sub(x[0], x[1], s)
        return s
481135
def smart_str(s, encoding='utf-8', strings_only=False, errors='strict'):
    
    if strings_only and isinstance(s, (types.NoneType, int)):
        return s
    # if isinstance(s, Promise):
    #     return unicode(s).encode(encoding, errors)
    if not isinstance(s, basestring):
        try:
            return str(s)
        except UnicodeEncodeError:
            if isinstance(s, Exception):
                # An Exception subclass containing non-ASCII data that doesn't
                # know how to print itself properly. We shouldn't raise a
                # further exception.
                return ' '.join([smart_str(arg, encoding, strings_only,
                        errors) for arg in s])
            return unicode(s).encode(encoding, errors)
    elif isinstance(s, unicode):
        return s.encode(encoding, errors)
    elif s and encoding != 'utf-8':
        return s.decode('utf-8', errors).encode(encoding, errors)
    else:
        return s
1233392
def utf8(s):
    
    if isinstance(s, mitogen.core.UnicodeType):
        s = s.encode('utf-8')
    return s
495562
def ensureUtf(s, encoding='utf8'):
    
    # In Python2, str == bytes.
    # In Python3, bytes remains unchanged, but str means unicode
    # while unicode is not defined anymore
    if type(s) == bytes:
        return s.decode(encoding, 'ignore')
    else:
        return s
301534
def ensure_binary(s, encoding='utf-8', errors='strict'):
    
    if isinstance(s, text_type):
        return s.encode(encoding, errors)
    elif isinstance(s, binary_type):
        return s
    else:
        raise TypeError("not expecting type '%s'" % type(s))
328521
def ensure_str(s, encoding='utf-8', errors='strict'):
    
    if not isinstance(s, (text_type, binary_type)):
        raise TypeError("not expecting type '%s'" % type(s))
    if PY2 and isinstance(s, text_type):
        s = s.encode(encoding, errors)
    elif PY3 and isinstance(s, binary_type):
        s = s.decode(encoding, errors)
    return s
1233511
def stripascii(string):
    
    # TODO: pythonize this
    i = len(string)
    while i:
        i -= 1
        if 8 < byte2int(string[i]) < 127:
            break
    else:
        i = -1
    return string[:i+1]
1026579
def to_native_string(string, encoding='ascii'):
    
    out = None

    if isinstance(string, builtin_str):
        out = string
    else:
        if is_py2:
            out = string.encode(encoding)
        else:
            out = string.decode(encoding)

    return out
417655
def recode_unicode(s, encoding='utf-8'):
    
    if isinstance(s, str):
        return s.encode().decode(encoding)
    return s
667866
def tobytes(s, encoding='ascii'):
    
    # NOTE: after we abandon 2.5, we might simply instead use "bytes(s)"
    # NOTE: after we abandon all 2.*, del this and prepend byte strings with 'b'
    if PY3K:
        if isinstance(s, bytes):
            return s
        else:
            return s.encode(encoding)
    else:
        # for py2.6 on (before 3.0), bytes is same as str;  2.5 has no bytes
        # but handle if unicode is passed
        if isinstance(s, unicode):
            return s.encode(encoding)
        else:
            return s
80427
def binary_encode_if_any_high_ascii(string)
      string = ensure_utf8(string)
      string.force_encoding('BINARY') if string.bytes.detect { |byte| byte > 127 }
      string
    end
861587
def to_ascii_equivalent(text):
    

    if text is None:
        return None
    elif isinstance(text, binary_type):
        text = text.decode(DEFAULT_ENCODING)
    elif not isinstance(text, text_type):
        text = text_type(text)

    text = EMPTY_STR.join(_ASCII_PUNCTUATION_MAP.get(c, c) for c in text)
    return EMPTY_STR.join(c for c in unicodedata.normalize('NFD', text) if unicodedata.category(c) != 'Mn')
53629
def mangle_unicode_to_ascii(s: Any) -> str:
    
    # http://stackoverflow.com/questions/1207457
    if s is None:
        return ""
    if not isinstance(s, str):
        s = str(s)
    return (
        unicodedata.normalize('NFKD', s)
                   .encode('ascii', 'ignore')  # gets rid of accents
                   .decode('ascii')  # back to a string
    )