Aug-22-2022, 07:24 AM
(This post was last modified: Aug-22-2022, 07:24 AM by Gribouillis.)
Skaperen Wrote:with this i would need to parse the line (not too hard) and determine which name this call was (much harder).You can walk a line with the ast module to find all the expressions that were called in the line
>>> line = "print(f'woot {foo()},{bar(456)}',flush=True)\n" >>> import ast >>> p = ast.parse(line) >>> for node in ast.walk(p): ... if isinstance(node, ast.Call): ... print(ast.unparse(node.func)) ... print foo barother example
>>> line = "xyz[0](spam, bar.x[1].egg(3))" >>> p = ast.parse(line) >>> for node in ast.walk(p): ... if isinstance(node, ast.Call): ... print(ast.unparse(node.func)) ... xyz[0] bar.x[1].eggNote:
ast.unparse()
needs Python >= 3.9. For earlier pythons, there is an «astunparse» module in Pypi.