Download runners/common.py from dotlabs/smolscript: direct link, hf CLI and curl.
- Browser
- Download file 3.94 kB
-
https://huggingface.co/datasets/dotlabs/smolscript/resolve/main/runners/common.py
- Command line
-
hf download hf://datasets/dotlabs/smolscript/runners/common.py
-
curl -L -o common.py https://huggingface.co/datasets/dotlabs/smolscript/resolve/main/runners/common.py
3.94 kB
| import re | |
| from decimal import Decimal,InvalidOperation | |
| def extract_code(text,preserve_indentation=False): | |
| text=text.strip('\r\n') if preserve_indentation else text.strip() | |
| # Closing fences must occupy their own line; backticks inside a string are code. | |
| blocks=re.findall(r'^[ \t]*```[^\r\n`]*\r?\n(.*?)^[ \t]*```[ \t]*(?=\r?$)',text,re.S|re.M) | |
| if len(blocks)==1: | |
| body=blocks[0].rstrip('\r\n') if preserve_indentation else blocks[0].strip() | |
| return body+'\n' | |
| if len(blocks)>1:raise ValueError('Multiple code blocks are ambiguous') | |
| if re.search(r'^[ \t]*```',text,re.M):raise ValueError('Unclosed code fence') | |
| return text+'\n' | |
| def compare_output(actual,expected): | |
| # Remove one terminal newline only: spaces in string results are observable. | |
| value=actual.removesuffix('\n').removesuffix('\r') | |
| if isinstance(expected,int): | |
| try:return Decimal(value.strip())==expected and value.strip()!='' | |
| except InvalidOperation:return False | |
| return value==expected | |
| class FunctionRunner: | |
| def evaluate(self,row,code,hidden,sandbox): | |
| inputs=[t['input'] for t in hidden['tests']] | |
| response=sandbox.run({'language':row['language'],'kind':row['metadata']['family'], | |
| 'code':code,'inputs':inputs,'timeout_seconds':row['timeout_seconds']}, | |
| timeout=50+len(inputs)*(row['timeout_seconds']+2)) | |
| results=[] | |
| observations=response['observations'] | |
| for i,test in enumerate(hidden['tests']): | |
| actual=observations[i] if i<len(observations) else None | |
| passed=bool(actual and actual['exit_code']==0 and not actual['timeout'] and not actual['output_limit'] and compare_output(actual['stdout'],test['expected'])) | |
| results.append({'test':i,'passed':passed,'actual':actual}) | |
| return {'syntax_success':response['syntax_success'],'passed':bool(results) and all(t['passed'] for t in results), | |
| 'tests':results,'compilation':response.get('compilation')} | |
| class BrowserRunner: | |
| def evaluate(self,row,code,hidden,sandbox): | |
| from runners.semantic import matches | |
| scenarios=[];expected=[] | |
| for t in hidden['tests']: | |
| widths=sorted(set(c.get('viewport',800) for c in t['checks'])) | |
| for width in widths: | |
| checks=[dict(c,kind='html_text') if row['language']=='html' and c['kind']=='text' else c | |
| for c in t['checks'] if c.get('viewport',800)==width] | |
| # Deliberately do not send expected values into the sandbox. | |
| scenarios.append({'viewport':width,'steps':t.get('steps',[]), | |
| 'checks':[{k:v for k,v in c.items() if k not in ('value','viewport')} for c in checks]}) | |
| expected.append(checks) | |
| response=sandbox.run({'language':row['language'],'kind':row['metadata']['family'],'code':code, | |
| 'fixture':row['metadata'].get('fixture',''),'base_css':row['metadata'].get('base_css',''), | |
| 'scenarios':scenarios,'timeout_seconds':row['timeout_seconds']},timeout=270) | |
| results=[] | |
| for i,checks in enumerate(expected): | |
| actual=response['observations'][i] if i<len(response['observations']) else None | |
| for j,c in enumerate(checks): | |
| observed=actual['actual'][j] if actual and j<len(actual['actual']) else None | |
| target=actual['baseline'][j] if actual and c['kind']=='unchanged' else c.get('value',True) | |
| passed=bool(actual and not actual['error'] and j<len(actual['actual']) and matches(c,observed,target)) | |
| results.append({'scenario':i,'test':j,'passed':passed,'actual':observed,'error':actual['error'] if actual else 'syntax failure'}) | |
| return {'syntax_success':response['syntax_success'],'passed':bool(results) and all(r['passed'] for r in results),'tests':results,'candidate_timeout':response.get('candidate_timeout',False)} | |