Teacode macos python5/4/2023 ![]() ![]() The fortune favors the brave, so let’s go ahead and try it: If we are not mistaken, this will finally be our lambda and nothing else. How about we just try and remove them, one by one, until we get something that is syntactically correct? What we have is invalid Python, but only because of some unspecified number of extra characters. Intuitively, a lambda node nested within some other syntactical constructs will have their closing fragments (e.g. It seems, however, that there is one thing those troublesome tails have in common: they aren’t syntactically valid. Here, the extraneous suffix is the entirety of ), Integer() & GreaterThan(42)), quite a lot of more than just )).Īnd that’s of course nowhere near the limit of possiblities: for one, there may be more lambdas in there, too! Back off, slowly assert_called_with ( Matching ( lambda x : x % 2 = 0 ), Integer () & GreaterThan ( 42 )) Perhaps we can use it to retrieve the lambda AST node in order to turn it. Know how to parse itself, after all: the standard ast module Maybe there is a way to strip away the unnecessary cruft? Python does No token left behind! Trim leftĬlearly this is more than we’ve bargained for. Say hello to the complete is_even = assignment, and the entire assert_called_with invocation!Īnd in case you are wondering: yes, the result will also include any end-of-line comments. Passing a lambda function to either getsourcelines or getsource, we’ll get its source and everything else The object definition” would be much more accurate, and this seemingly small distinction is rather crucial here. More important is the ambiguity of what does “source lines for an object” actually mean. Will be thrown when you try to pass any of the Python builtins to getsourcelines. And it’s not just that the except clause should also include TypeError, because it Of course if you programmed in Python for any longer period of time, you know very well that the standard docsĪre not to be trusted. getsourcelines ( lambda_func ) except IOError : return None if len ( source_lines ) > 1 : return None return source_lines. Here’s a short lambda function, the kind of which we’d like to obtainĭef get_short_lambda_source ( lambda_func ): try : source_lines, _ = inspect. Transformations of the source code as text, and bytecode shenanigans?… The code, all the code, and… much more than the code Make no mistake, though: the task turned out to be absolutely doable, at least in the scope I wanted it done.īut what would you think of a solution that involves not just the usual Python hackery, but also AST inspection, Surely it would be possible - nay, easy - to get the source code of a short lambda function, right? Or change the behavior of the import system itself. (or even forward) and read the local variables, So I thought: why not try and implement such a mechanism? This is Python, after all - a language where you can spawn Of dereferencing pointers in your head, this won’t give you any immediate hint as to what went wrong. Provided you don’t possess a supernatural ability It would be great to see its code in the error message if the assertion fails. assert_called_with ( Matching ( lambda x : x % 2 = 0 ))
0 Comments
Leave a Reply. |