| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- """
- Script to convert f-strings to .format() for Python 3.5 compatibility
- """
- import re
- import os
- files_to_fix = [
- "bdirag/document_processor.py",
- "bdirag/embedding_models.py",
- "bdirag/vector_stores.py",
- "bdirag/rag_methods.py",
- "bdirag/benchmark.py",
- "bdirag/config.py",
- "examples/test_bm25.py",
- "examples/benchmark_all_methods.py",
- "examples/quick_demo.py",
- "examples/benchmark_retrieval_speed.py",
- "examples/bid_field_extraction_demo.py",
- ]
- def convert_fstring_in_line(line):
- # Find f"..." or f'...' patterns on a single line
- # Handle double quotes
- pattern_dq = r'f"([^"]*?\{[^}]*\}[^"]*?)"'
- pattern_sq = r"f'([^']*?\{[^}]*\}[^']*?)'"
- for pattern, quote in [(pattern_dq, '"'), (pattern_sq, "'")]:
- while True:
- match = re.search(pattern, line)
- if not match:
- break
- inner = match.group(1)
- values = []
- new_str = ""
- i = 0
- while i < len(inner):
- if inner[i] == "{" and i + 1 < len(inner) and inner[i + 1] != "{":
- j = inner.index("}", i)
- expr = inner[i + 1:j].strip()
- values.append(expr)
- new_str += "{" + str(len(values) - 1) + "}"
- i = j + 1
- elif inner[i:i + 2] == "{{":
- new_str += "{{"
- i += 2
- elif inner[i:i + 2] == "}}":
- new_str += "}}"
- i += 2
- else:
- new_str += inner[i]
- i += 1
- format_call = ".format(" + ", ".join(values) + ")"
- replacement = new_str + format_call
- line = line[:match.start()] + quote + replacement + quote + line[match.end():]
- return line
- for filepath in files_to_fix:
- if not os.path.exists(filepath):
- print("Not found: " + filepath)
- continue
- with open(filepath, "r", encoding="utf-8") as f:
- content = f.read()
- lines = content.split("\n")
- new_lines = []
- changed = False
- for line in lines:
- if "f'" in line or 'f"' in line:
- new_line = convert_fstring_in_line(line)
- if new_line != line:
- changed = True
- new_lines.append(new_line)
- else:
- new_lines.append(line)
- if changed:
- new_content = "\n".join(new_lines)
- with open(filepath, "w", encoding="utf-8") as f:
- f.write(new_content)
- print("Fixed: " + filepath)
- else:
- print("No changes: " + filepath)
|