mirror of
				https://github.com/hwchase17/langchain.git
				synced 2025-10-31 16:08:59 +00:00 
			
		
		
		
	See https://docs.astral.sh/ruff/rules/#pyupgrade-up All auto-fixed Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
		
			
				
	
	
		
			31 lines
		
	
	
		
			853 B
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			31 lines
		
	
	
		
			853 B
		
	
	
	
		
			Python
		
	
	
	
	
	
| from pathlib import Path
 | |
| 
 | |
| 
 | |
| def find_and_replace(source: str, replacements: dict[str, str]) -> str:
 | |
|     rtn = source
 | |
| 
 | |
|     # replace keys in deterministic alphabetical order
 | |
|     finds = sorted(replacements.keys())
 | |
|     for find in finds:
 | |
|         replace = replacements[find]
 | |
|         rtn = rtn.replace(find, replace)
 | |
|     return rtn
 | |
| 
 | |
| 
 | |
| def replace_file(source: Path, replacements: dict[str, str]) -> None:
 | |
|     try:
 | |
|         content = source.read_text()
 | |
|     except UnicodeDecodeError:
 | |
|         # binary file
 | |
|         return
 | |
|     new_content = find_and_replace(content, replacements)
 | |
|     if new_content != content:
 | |
|         source.write_text(new_content)
 | |
| 
 | |
| 
 | |
| def replace_glob(parent: Path, glob: str, replacements: dict[str, str]) -> None:
 | |
|     for file in parent.glob(glob):
 | |
|         if not file.is_file():
 | |
|             continue
 | |
|         replace_file(file, replacements)
 |