| 18 | |
| 19 | |
| 20 | class GenericRuntime: |
| 21 | GLOBAL_DICT = {} |
| 22 | LOCAL_DICT = None |
| 23 | HEADERS = [] |
| 24 | def __init__(self): |
| 25 | self._global_vars = copy.copy(self.GLOBAL_DICT) |
| 26 | self._local_vars = copy.copy(self.LOCAL_DICT) if self.LOCAL_DICT else None |
| 27 | |
| 28 | for c in self.HEADERS: |
| 29 | self.exec_code(c) |
| 30 | |
| 31 | def exec_code(self, code_piece: str) -> None: |
| 32 | if regex.search(r'(\s|^)?input\(', code_piece): |
| 33 | # regex.search(r'(\s|^)?os.', code_piece): |
| 34 | raise RuntimeError() |
| 35 | exec(code_piece, self._global_vars) |
| 36 | |
| 37 | # TODO: use: https://github.com/shroominic/codebox-api |
| 38 | # @high safe exec in sandbox |
| 39 | # byte_code = compile_restricted( |
| 40 | # code_piece, |
| 41 | # filename='<inline code>', |
| 42 | # mode='exec' |
| 43 | # ) |
| 44 | # print("global vars:", self._global_vars) |
| 45 | # _print_ = PrintCollector |
| 46 | # exec(byte_code, {'__builtins__': utility_builtins}, None) |
| 47 | |
| 48 | def eval_code(self, expr: str) -> Any: |
| 49 | return eval(expr, self._global_vars) |
| 50 | |
| 51 | def inject(self, var_dict: Dict[str, Any]) -> None: |
| 52 | for k, v in var_dict.items(): |
| 53 | self._global_vars[k] = v |
| 54 | |
| 55 | @property |
| 56 | def answer(self): |
| 57 | return self._global_vars['answer'] |
| 58 | |
| 59 | class DateRuntime(GenericRuntime): |
| 60 | GLOBAL_DICT = { |