Advertisement
Mr-A

Renaming Script

Jan 31st, 2022 (edited)
1,089
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.73 KB | None | 0 0
  1. import os, re
  2.  
  3. rename_pairs = { #"old_name": "new_name"
  4. "mom_grid": "MomSpaceGrid",
  5. "mom_grid_sigma": "MomSpaceGridSig",
  6. "rgrid": "RealSpaceGrid",
  7. "Freq_Grid": "freq_grid_t",
  8. "F_Grid": "fermionic_freq_grid_t",
  9. "B_Grid": "bosonic_freq_grid_t",
  10. "R_Grid": "real_space_grid_t",
  11. "K_Grid": "recirpocal_space_grid_t",
  12. "state_t": "frg_state_t",
  13. "F_factors": "form_factors_t",
  14. "F_factors_real": "form_factors_real_t",
  15. "ffactor_mom": "MomSpaceFormFactors",
  16. "ffactor_real": "RealSpaceFormFactors"}
  17.  
  18.  
  19. include_dir = os.getcwd()+"/include/"
  20. src_dir = os.getcwd()+"/src/"
  21.  
  22. header_files = filter(lambda d: (d.endswith(".h")  or d.endswith(".hpp")) and "#" not in d, map(lambda f: include_dir + f, os.listdir(include_dir)) )
  23. src_files = filter(lambda d: d.endswith(".cpp") and "#" not in d, map(lambda f: src_dir + f, os.listdir(src_dir)) )
  24.  
  25. all_files = header_files + src_files
  26.  
  27. # prepare the regex objects                                                                                                                                                                                        
  28. old_names_re = {}
  29. for old_name in rename_pairs.keys():
  30.     old_names_re[old_name] = re.compile(r'([^a-zA-Z_]|^)('+old_name+r')([^a-zA-Z_]|$)')
  31.  
  32. def apply_renames(content):
  33.     for old_name in rename_pairs.keys():
  34.         content = old_names_re[old_name].sub(lambda m: m.group(1) + rename_pairs[old_name] + m.group(3), content)
  35.     return content
  36.  
  37. for f in all_files:
  38.     print("File: ", f)
  39.     with open(f) as f_obj:
  40.         content = f_obj.read()
  41.     print("Content length: ", len(content))
  42.     new_content = apply_renames(content)
  43.     with open(f, 'w') as f_obj:
  44.         f_obj.write(new_content)
  45.     print("New content length: ", len(new_content))
  46.  
  47.  
  48.  
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement