️ Speed up is_readable by 101% in embedchain/utils/misc.py (#1258)

Co-authored-by: codeflash-ai[bot] <148906541+codeflash-ai[bot]@users.noreply.github.com>
This commit is contained in:
Saurabh Misra
2024-02-15 23:18:53 -08:00
committed by GitHub
parent 38b4e06963
commit 9a11683003

View File

@@ -109,11 +109,11 @@ def is_readable(s):
:param s: string :param s: string
:return: True if the string is more than 95% printable. :return: True if the string is more than 95% printable.
""" """
try: len_s = len(s)
printable_ratio = sum(c in string.printable for c in s) / len(s) if len_s == 0:
except ZeroDivisionError: return False
logging.warning("Empty string processed as unreadable") printable_chars = set(string.printable)
printable_ratio = 0 printable_ratio = sum(c in printable_chars for c in s) / len_s
return printable_ratio > 0.95 # 95% of characters are printable return printable_ratio > 0.95 # 95% of characters are printable