def istext(s, text_characters=''.join(map(chr, range(32, 127))) +
    '\n\r\t\x08', threshold=0.3):
    """
    Helper to attempt support of serializing binary text by base64 encoding `s` if this returns False

    Credit: https://www.oreilly.com/library/view/python-cookbook-2nd/0596007973/ch01s12.html
    """
    if isinstance(s, bytes):
        try:
            s = s.decode('utf-8')
        except UnicodeDecodeError:
            return False
    if '\x00' in s:
        return False
    if not s:
        return True
    t = s.translate(str.maketrans('', '', text_characters))
    return len(t) / len(s) <= threshold
