Fill in the AST template for timing execution. This is quite closely tied to the template definition, which is in :meth:`ExecutionMagics.timeit`.
| 121 | |
| 122 | |
| 123 | class TimeitTemplateFiller(ast.NodeTransformer): |
| 124 | """Fill in the AST template for timing execution. |
| 125 | |
| 126 | This is quite closely tied to the template definition, which is in |
| 127 | :meth:`ExecutionMagics.timeit`. |
| 128 | """ |
| 129 | def __init__(self, ast_setup, ast_stmt): |
| 130 | self.ast_setup = ast_setup |
| 131 | self.ast_stmt = ast_stmt |
| 132 | |
| 133 | def visit_FunctionDef(self, node): |
| 134 | "Fill in the setup statement" |
| 135 | self.generic_visit(node) |
| 136 | if node.name == "inner": |
| 137 | node.body[:1] = self.ast_setup.body |
| 138 | |
| 139 | return node |
| 140 | |
| 141 | def visit_For(self, node): |
| 142 | "Fill in the statement to be timed" |
| 143 | if getattr(getattr(node.body[0], 'value', None), 'id', None) == 'stmt': |
| 144 | node.body = self.ast_stmt.body |
| 145 | return node |
| 146 | |
| 147 | |
| 148 | class Timer(timeit.Timer): |