Background: I'm currently creating a line-magic for ipython. This magic shall only work for lines, where the return value of a function is assigned to a variable.
I'm looking for a way to make sure, that a line is a valid function-call + assignment in python.
e.g. the following shall be accepted:
a = b()
a,b = c(d,e="f")
a = b(c()+c)
and the following shall be declined:
a = def fun() # no function call
b(a=2) # no assignment
a = b + c # no function call
a = b() + c() # top-level on right-hand-side must be function call
If the line is no valid python at all, I don't care whether it passes, as this will be handled at another stage.
You could use Python's own parser, accessible through the
ast
module, to directly inspect each statement to see if it's an assignment whose right-hand-side is a call.Result: