Infer destination-passing style by checking the ``run()`` signature. If the last parameter of ``run()`` matches the last output name in the definition, the solution writes into pre-allocated output buffers (DPS).
(code: str, definition: dict)
| 83 | |
| 84 | |
| 85 | def _infer_dps(code: str, definition: dict) -> bool: |
| 86 | """Infer destination-passing style by checking the ``run()`` signature. |
| 87 | |
| 88 | If the last parameter of ``run()`` matches the last output name in the |
| 89 | definition, the solution writes into pre-allocated output buffers (DPS). |
| 90 | """ |
| 91 | output_names = list(definition.get("outputs", {}).keys()) |
| 92 | if not output_names: |
| 93 | return False |
| 94 | |
| 95 | last_output = output_names[-1] |
| 96 | |
| 97 | try: |
| 98 | tree = ast.parse(code) |
| 99 | except SyntaxError: |
| 100 | return False |
| 101 | |
| 102 | for node in ast.walk(tree): |
| 103 | if ( |
| 104 | isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)) |
| 105 | and node.name == "run" |
| 106 | ): |
| 107 | args = node.args |
| 108 | # Last positional arg name |
| 109 | if args.args: |
| 110 | last_param = args.args[-1].arg |
| 111 | return last_param == last_output |
| 112 | break |
| 113 | |
| 114 | return False |
| 115 | |
| 116 | |
| 117 | def build_solution_for_problem( |
no outgoing calls
no test coverage detected