Преглед изворни кода

⚡️ 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>
Saurabh Misra пре 1 година
родитељ
комит
9a11683003
1 измењених фајлова са 5 додато и 5 уклоњено
  1. 5 5
      embedchain/utils/misc.py

+ 5 - 5
embedchain/utils/misc.py

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