There are so many explanations of infix, postfix (reverse Polish) and prefix (Polish) notations/expressions on the web. There are also several simple implementations of algorithms that convert between these notations. However, none of these implementations that I've found so far seem to be robust, i.e. can deal with many cases (i.e. not just the usual arithmetic operators, but any Python function, with any arbitrary number of parameters), support many parameters and hyper-parameters, are able to cope with a different number of spaces between the operands, operators, and Python functions, can produce the abstract syntax tree (and related stuff) from these expressions (and vice-versa), supports different algorithms for conversion, and so on and so forth.

So, is there any Python package (which can be installed with pip and has been used in other projects, so it's something serious) that does this?

Please, do not tell me to write my own package, because I know that, in principle, I can do it, but I don't want to and I can't spend my time doing it. Note that it's very important that this package supports any type of function with an arbitrary number of parameters and that we are easily able to specify this when using the library.

Note that I am aware of the Python package ast, but it doesn't really support conversion between these notations and from and to ASTs. Moreover, Python's built-in function eval seems to support only the infix notation. It's really incredible that such a potentially useful tool does not seem to exist or, at least, to be easily retrievable.

  • So you're looking to parse/translate entire Python files? For example, you want to be able to write a file containing lines like 3 function(a b +) / or / 3 function(+ a b) and have it interpreted like 3 / function(a + b) in normal Python? – CrazyChucky Dec 22 '20 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy