| 32 | |
| 33 | |
| 34 | class GenericRuntime: |
| 35 | GLOBAL_DICT = {} |
| 36 | LOCAL_DICT = None |
| 37 | HEADERS = [] |
| 38 | |
| 39 | def __init__(self): |
| 40 | self._global_vars = copy.copy(self.GLOBAL_DICT) |
| 41 | self._local_vars = copy.copy(self.LOCAL_DICT) if self.LOCAL_DICT else None |
| 42 | |
| 43 | for c in self.HEADERS: |
| 44 | self.exec_code(c) |
| 45 | |
| 46 | def exec_code(self, code_piece: str) -> None: |
| 47 | if regex.search(r'(\s|^)?input\(', code_piece) or regex.search(r'(\s|^)?os.system\(', code_piece): |
| 48 | raise RuntimeError() |
| 49 | exec(code_piece, self._global_vars) |
| 50 | |
| 51 | def eval_code(self, expr: str) -> Any: |
| 52 | return eval(expr, self._global_vars) |
| 53 | |
| 54 | def inject(self, var_dict: Dict[str, Any]) -> None: |
| 55 | for k, v in var_dict.items(): |
| 56 | self._global_vars[k] = v |
| 57 | |
| 58 | @property |
| 59 | def answer(self): |
| 60 | return self._global_vars['answer'] |
| 61 | |
| 62 | |
| 63 | class DateRuntime(GenericRuntime): |