Ask Your Question
0

Custom Input Transformation

asked 2015-10-28 12:58:34 -0600

jediknight219 gravatar image

updated 2015-10-28 13:23:44 -0600

Greetings. I am trying to emulate the << >> functionality of OCaml in python on cloud.sagemath.com. For instance, if I type:

<<stuff>>

I want it to call default_parser("stuff"). I have it working if << and >> are on the same line, but I want it to be able to span multiple lines. Here's my code:

from IPython.core.inputtransformer import (StatelessInputTransformer)

@StatelessInputTransformer.wrap
def quotations(line):
    if line is not None:
        line = line.replace("<<", 'default_parser("""')
        line = line.replace(">>", '""")')
    return line

ip = get_ipython()
ip.input_splitter.logical_line_transforms.append(quotations())
ip.input_transformer_manager.logical_line_transforms.append(quotations())

It can't handle this:

<<
stuff
>>

It replaces the << in the first line just fine, but apparently once it goes into triple quote mode, it stops calling my transformation function.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-10-28 13:36:12 -0600

jediknight219 gravatar image

Figured it out. Replaced logical_line_transforms with physical_line_transforms.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2015-10-28 12:58:34 -0600

Seen: 50 times

Last updated: Oct 28 '15