MCPcopy Index your code
hub / github.com/showlab/Code2Video / GridCodeModifier

Class GridCodeModifier

src/scope_refine.py:753–802  ·  view source on GitHub ↗

Modify specific grid position code based on feedback

Source from the content-addressed store, hash-verified

751
752
753class GridCodeModifier:
754 """Modify specific grid position code based on feedback"""
755
756 def __init__(self, original_code: str):
757 self.original_code = original_code
758 self.lines = original_code.split("\n")
759
760 def apply_grid_modifications(self, modifications: List[Dict[str, Any]]) -> str:
761 modified_lines = self.lines.copy()
762 for mod in modifications:
763 try:
764 line_idx = int(mod["line_number"]) - 1
765 except Exception:
766 continue
767 if not (0 <= line_idx < len(modified_lines)):
768 continue
769 original_line = modified_lines[line_idx]
770 # print(f"Replace line {line_idx + 1}: {original_line} -> {mod['new_code'].strip()}")
771 indent = len(original_line) - len(original_line.lstrip())
772 new_code = " " * indent + mod["new_code"].strip()
773 modified_lines[line_idx] = new_code
774 return "\n".join(modified_lines)
775
776 def parse_feedback_and_modify(self, feedback_list: List[str]) -> str:
777 """feedback_list: ['... Solution: Line 121: self.place_at_grid(... )', ...]"""
778 if not isinstance(feedback_list, list):
779 return self.original_code
780
781 modifications: List[Dict[str, Any]] = []
782 line_pat = re.compile(r"\bline\s+(\d+)\b", re.IGNORECASE)
783 call_pat = re.compile(r"self\.(?:place_at_grid|place_in_area)\([^\n\r]*?\)")
784
785 for item in feedback_list:
786 if not isinstance(item, str):
787 continue
788 # Extract line number and new code from feedback
789 m_sol = re.search(r"solution\s*:\s*(.*)$", item, flags=re.IGNORECASE)
790 sol = m_sol.group(1).strip() if m_sol else item.strip()
791 # Extract line number from feedback
792 m_line = line_pat.search(sol)
793 if not m_line:
794 continue
795 line_number = int(m_line.group(1))
796 # Extract new code from feedback
797 m_call = call_pat.search(sol)
798 if not m_call:
799 continue
800 new_code = m_call.group(0)
801 modifications.append({"line_number": line_number, "new_code": new_code})
802 return self.apply_grid_modifications(modifications)

Callers 1

generate_section_codeMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected