Example #1
0
def transform_cell(code, indent_only=False):
    """Transform IPython code to Python code."""
    number_empty_lines = count_leading_empty_lines(code)
    if indent_only:
        if not code.endswith('\n'):
            code += '\n'  # Ensure the cell has a trailing newline
        lines = code.splitlines(keepends=True)
        lines = leading_indent(leading_empty_lines(lines))
        code = ''.join(lines)
    else:
        tm = TransformerManager()
        code = tm.transform_cell(code)
    return '\n' * number_empty_lines + code
Example #2
0
def transform_cell(code, indent_only=False):
    """Transform ipython code to python code."""
    number_empty_lines = count_leading_empty_lines(code)
    if indent_only:
        # Not implemented for PY2
        if LooseVersion(ipy_version) < LooseVersion('7.0.0'):
            return code
        if not code.endswith('\n'):
            code += '\n'  # Ensure the cell has a trailing newline
        lines = code.splitlines(keepends=True)
        lines = leading_indent(leading_empty_lines(lines))
        code = ''.join(lines)
    else:
        if LooseVersion(ipy_version) < LooseVersion('7.0.0'):
            tm = IPythonInputSplitter()
            return tm.transform_cell(code)
        else:
            tm = TransformerManager()
            code = tm.transform_cell(code)
    return '\n' * number_empty_lines + code
def test_leading_indent():
    for sample, expected in [INDENT_SPACES, INDENT_TABS]:
        nt.assert_equal(ipt2.leading_indent(sample.splitlines(keepends=True)),
                        expected.splitlines(keepends=True))
def test_leading_indent():
    for sample, expected in [INDENT_SPACES, INDENT_TABS]:
        nt.assert_equal(ipt2.leading_indent(sample.splitlines(keepends=True)),
                        expected.splitlines(keepends=True))