fix_fstrings.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. """
  2. Script to convert f-strings to .format() for Python 3.5 compatibility
  3. """
  4. import re
  5. import os
  6. files_to_fix = [
  7. "bdirag/document_processor.py",
  8. "bdirag/embedding_models.py",
  9. "bdirag/vector_stores.py",
  10. "bdirag/rag_methods.py",
  11. "bdirag/benchmark.py",
  12. "bdirag/config.py",
  13. "examples/test_bm25.py",
  14. "examples/benchmark_all_methods.py",
  15. "examples/quick_demo.py",
  16. "examples/benchmark_retrieval_speed.py",
  17. "examples/bid_field_extraction_demo.py",
  18. ]
  19. def convert_fstring_in_line(line):
  20. # Find f"..." or f'...' patterns on a single line
  21. # Handle double quotes
  22. pattern_dq = r'f"([^"]*?\{[^}]*\}[^"]*?)"'
  23. pattern_sq = r"f'([^']*?\{[^}]*\}[^']*?)'"
  24. for pattern, quote in [(pattern_dq, '"'), (pattern_sq, "'")]:
  25. while True:
  26. match = re.search(pattern, line)
  27. if not match:
  28. break
  29. inner = match.group(1)
  30. values = []
  31. new_str = ""
  32. i = 0
  33. while i < len(inner):
  34. if inner[i] == "{" and i + 1 < len(inner) and inner[i + 1] != "{":
  35. j = inner.index("}", i)
  36. expr = inner[i + 1:j].strip()
  37. values.append(expr)
  38. new_str += "{" + str(len(values) - 1) + "}"
  39. i = j + 1
  40. elif inner[i:i + 2] == "{{":
  41. new_str += "{{"
  42. i += 2
  43. elif inner[i:i + 2] == "}}":
  44. new_str += "}}"
  45. i += 2
  46. else:
  47. new_str += inner[i]
  48. i += 1
  49. format_call = ".format(" + ", ".join(values) + ")"
  50. replacement = new_str + format_call
  51. line = line[:match.start()] + quote + replacement + quote + line[match.end():]
  52. return line
  53. for filepath in files_to_fix:
  54. if not os.path.exists(filepath):
  55. print("Not found: " + filepath)
  56. continue
  57. with open(filepath, "r", encoding="utf-8") as f:
  58. content = f.read()
  59. lines = content.split("\n")
  60. new_lines = []
  61. changed = False
  62. for line in lines:
  63. if "f'" in line or 'f"' in line:
  64. new_line = convert_fstring_in_line(line)
  65. if new_line != line:
  66. changed = True
  67. new_lines.append(new_line)
  68. else:
  69. new_lines.append(line)
  70. if changed:
  71. new_content = "\n".join(new_lines)
  72. with open(filepath, "w", encoding="utf-8") as f:
  73. f.write(new_content)
  74. print("Fixed: " + filepath)
  75. else:
  76. print("No changes: " + filepath)