python - Adding syntax to IPython? -
i add syntax changes (my installation of) ipython. example, might want use \+
mean operator.add
. imagine can insert code process input , turn actual (i)python, , ipython can own processing. don't know put code.
(disclaimer: don't production code, or code that's intended other people see/use.)
here example of how transform "\+ b"
"a + b"
.
from ipython.core.inputtransformer import statelessinputtransformer @statelessinputtransformer.wrap def my_filter(line): words = line.split() if line.startswith(r'\+ ') , len(words) == 3: return '{} + {}'.format(*words[1:]) return line ip = get_ipython() ip.input_transformer_manager.physical_line_transforms.insert(0, my_filter())
note string based. hook executes in unevaluated context. means can't conditional transformation based on value a
or b
. magic best suit need in case.
moreover, have careful when parsing input string. in example, following broken \+ (a * b) c
because of split. in case, need tokenization tool. ipython provides 1 tokeninputtransformer
. works statelessinputtransformer
called list of tokens instead of whole line.
simply run code add filter. if want available start ipython, can save .py
or .ipy
file , put in ~/.ipython/profile_*/startup
https://ipython.org/ipython-doc/dev/config/inputtransforms.html
Comments
Post a Comment