I would like to add some syntax changes to (my installation of) IPython. For example, I might want to use \+ to mean operator.add. I imagine that I can insert some code that would process the input and turn it into actual (I)Python, and then IPython can do its own processing. But I don't know where to put that code.
(Disclaimer: Don't do it for production code, or code that's intended for other people to see/use.)
Here is an example of how to transform
"\+ a b"to"a + b".Note that this is all string based. This hook executes in an unevaluated context. It means that you can't do conditional transformation based on which value is
aorb. A magic would best suit your need in that case.Moreover, you have to be careful when parsing input string. In my example, the following is broken
\+ (a * b) cbecause of the split. In that case, you will need a tokenization tool. IPython provides one withTokenInputTransformer. It works likeStatelessInputTransformerbut it is called with a list of tokens instead of the whole line.Simply run this code to add the filter. If you want it to be available as you start IPython, you can save it as a
.pyor.ipyfile and put it in~/.ipython/profile_*/startuphttps://ipython.org/ipython-doc/dev/config/inputtransforms.html