| 21 | |
| 22 | |
| 23 | class GenericRuntime: |
| 24 | GLOBAL_DICT = {} |
| 25 | LOCAL_DICT = None |
| 26 | HEADERS = [] |
| 27 | def __init__(self): |
| 28 | self._global_vars = copy.copy(self.GLOBAL_DICT) |
| 29 | self._local_vars = copy.copy(self.LOCAL_DICT) if self.LOCAL_DICT else None |
| 30 | |
| 31 | for c in self.HEADERS: |
| 32 | self.exec_code(c) |
| 33 | |
| 34 | def exec_code(self, code_piece: str) -> None: |
| 35 | if regex.search(r'(\s|^)?input\(', code_piece) or regex.search(r'(\s|^)?os.system\(', code_piece): |
| 36 | raise RuntimeError() |
| 37 | exec(code_piece, self._global_vars) |
| 38 | |
| 39 | def eval_code(self, expr: str) -> Any: |
| 40 | return eval(expr, self._global_vars) |
| 41 | |
| 42 | def inject(self, var_dict: Dict[str, Any]) -> None: |
| 43 | for k, v in var_dict.items(): |
| 44 | self._global_vars[k] = v |
| 45 | |
| 46 | @property |
| 47 | def answer(self): |
| 48 | return self._global_vars['answer'] |
| 49 | |
| 50 | class DateRuntime(GenericRuntime): |
| 51 | GLOBAL_DICT = { |