MCPcopy Create free account
hub / github.com/BUPT-GAMMA/MASFactory / extract_code

Function extract_code

applications/mapcoder/components/tester.py:40–56  ·  view source on GitHub ↗

Extract the first fenced code block; fall back to raw text. The regex is permissive about the language tag (e.g. ```py / ```python3 / ``` ... ```). We pick the *first* fence on the assumption that the LLM obeys the prompt and emits a single solution block.

(text: str)

Source from the content-addressed store, hash-verified

38
39
40def extract_code(text: str) -> str:
41 """Extract the first fenced code block; fall back to raw text.
42
43 The regex is permissive about the language tag (e.g. ```py / ```python3 /
44 ``` ... ```). We pick the *first* fence on the assumption that the LLM
45 obeys the prompt and emits a single solution block.
46 """
47 if not isinstance(text, str):
48 return ""
49 text = text.strip()
50 if not text:
51 return ""
52 m = _FENCE_RE.search(text)
53 if m:
54 return m.group(1).strip()
55 # No fence: treat the whole response as code (best-effort fallback).
56 return text
57
58
59def _build_driver(candidate_code: str, sample_io: list[dict]) -> str:

Calls

no outgoing calls

Tested by

no test coverage detected