Ask Your Question

Revision history [back]

Custom Input Transformation

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.

Custom Input Transformation

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.