remove_association_methods.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. 临时脚本:删除 association 相关的方法
  5. """
  6. # 需要删除的方法列表(方法名)
  7. METHODS_TO_REMOVE = [
  8. '_is_classification',
  9. '_navigate_to_node',
  10. '_recursive_search',
  11. '_search_classification_path',
  12. 'stage2_find_associations',
  13. '_find_associations',
  14. '_find_intra_dimension_associations',
  15. '_collect_classification_info',
  16. 'stage3_filter_high_similarity_matches',
  17. '_collect_scope_from_associations',
  18. '_collect_stage2_scope',
  19. '_find_features_by_path',
  20. ]
  21. def find_method_bounds(lines, method_name):
  22. """
  23. 查找方法的起始和结束行号
  24. Returns:
  25. (start_line, end_line) 或 None
  26. """
  27. start_line = None
  28. indent_level = None
  29. # 查找方法开始
  30. for i, line in enumerate(lines):
  31. if f'def {method_name}(' in line:
  32. start_line = i
  33. # 获取方法的缩进级别
  34. indent_level = len(line) - len(line.lstrip())
  35. break
  36. if start_line is None:
  37. return None
  38. # 查找方法结束(下一个同级或更外层的def/class,或遇到注释分隔符)
  39. for i in range(start_line + 1, len(lines)):
  40. line = lines[i]
  41. stripped = line.lstrip()
  42. # 空行跳过
  43. if not stripped:
  44. continue
  45. current_indent = len(line) - len(line.lstrip())
  46. # 遇到 # ========== 注释分隔符
  47. if stripped.startswith('# =========='):
  48. return (start_line, i)
  49. # 遇到同级或更外层的def/class
  50. if current_indent <= indent_level and (stripped.startswith('def ') or stripped.startswith('class ')):
  51. return (start_line, i)
  52. # 如果到文件末尾都没找到
  53. return (start_line, len(lines))
  54. def main():
  55. input_file = 'enhanced_search_v2.py'
  56. output_file = 'enhanced_search_v2_cleaned.py'
  57. # 读取文件
  58. with open(input_file, 'r', encoding='utf-8') as f:
  59. lines = f.readlines()
  60. # 收集所有要删除的行范围
  61. ranges_to_remove = []
  62. for method_name in METHODS_TO_REMOVE:
  63. result = find_method_bounds(lines, method_name)
  64. if result:
  65. ranges_to_remove.append(result)
  66. print(f"找到方法 {method_name}: 行 {result[0]+1} - {result[1]+1}")
  67. else:
  68. print(f"未找到方法 {method_name}")
  69. # 按起始行排序(倒序)
  70. ranges_to_remove.sort(reverse=True)
  71. # 删除方法(从后往前删)
  72. for start, end in ranges_to_remove:
  73. print(f"删除行 {start+1} - {end}")
  74. del lines[start:end]
  75. # 写入新文件
  76. with open(output_file, 'w', encoding='utf-8') as f:
  77. f.writelines(lines)
  78. print(f"\n✓ 已生成清理后的文件: {output_file}")
  79. print(f"原文件行数: {len(open(input_file).readlines())}")
  80. print(f"新文件行数: {len(lines)}")
  81. if __name__ == '__main__':
  82. main()