File size: 449 Bytes
0109502
 
 
 
 
 
 
1
2
3
4
5
6
7
8
import re
def _norm(x): return "\n".join(line.rstrip() for line in x.replace("\r\n","\n").splitlines()).strip()
def extract_code(output):
    blocks=[_norm(x) for x in re.findall(r"```(?:python|py)?\s*\n?(.*?)```",output or "",re.I|re.S) if _norm(x)]
    if blocks: return max(blocks,key=len)
    text=_norm(output or "")
    return text if any(x in text for x in ("def ","class ","import ","from ","for ","while ","if ","try:","async ")) else None