import sys import re file_path = 'core/views.py' with open(file_path, 'r') as f: content = f.read() # Fix door_visits which was accidentally patched # Find door_visits function and remove the sign_types_display loop if it's there # Actually, I'll just check where sign_types is NOT initialized but h['sign_types'] is used. # A more specific approach: # Replace the bad part in door_visits. bad_part = """ households_list = list(households_dict.values()) for h in households_list: h['sign_types_display'] = ", ".join(sorted(list(h['sign_types']))) del households_dict # Free memory""" good_part = """ households_list = list(households_dict.values()) del households_dict # Free memory""" # But wait, yard_sign_voters and view_signs DO need this loop. # I need to distinguish them. # Let's use re.finditer to find all occurrences and check context. # Or just use the fact that door_visits has 'voters_json' or 'target_voters' initialized. def fix_views(text): # Match functions functions = re.split(r'\n(?=def )', text) new_functions = [] for func in functions: if 'def door_visits' in func: # Remove the sign_types_display loop func = func.replace( " for h in households_list:\n h['sign_types_display'] = \", \".join(sorted(list(h['sign_types']))) ", "" ) new_functions.append(func) return '\n'.join(new_functions) content = fix_views(content) with open(file_path, 'w') as f: f.write(content)