30

I am new to Python.

In short:

During scripting I continuously want to test small bits and pieces of my programs by copying/pasting some line(s) of code from my text editor to the command line Python interpreter. When these lines are indented (for example because they are part of a function), I'd like the interpreter to either ignore or not check indentation so that I don't have to unindent them before copy/pasting. Is that possible?

In more details:

Here a simplified example of what I mean:

Let's say my text editor contains the following module currently under development:

def MyFunc(arg):
    .../...
    if arg == 1:
        print "This is my function called with value 1."
        print "Done."
    else:
        print "This is my function called with value other than 1."
        print "Nothing else to say."
    .../...

And let's say I simply want to test the 2 first print lines (lines 4 and 5 of the above code) straight away just to quickly check if at least that part of my module is behaving as expected. If I select both lines together I will at least select along the indentation for the second line (if not for both). When pasted at the command line, I'll get an error for that indentation.

A simple enforced behaviour of the interpreter would be that it simply ignores indentation.

A more powerful behaviour would be to ask the interpreter to just not check the indentation. I.e. if indentation is there then the interpreter should try to use it so that I could still copy/past even a structured piece of code (e.g. lines 3 to 8 of the above code). But in case there are indentation errors it would just ignore them.

If there's no way to do what I'm asking for here, then are there tricks for doing something similar: a simple way to quickly check pieces of your code without having to run the whole program every time you just want to tune small parts of it here and there.

NB 1: unindenting is NOT the solution to what I am looking for.

NB 2: having an interpreter together with a copy/paste capability offers a very powerful way of easily testing code but the explicit indentation mechanism of Python is a strong drawback to such a usage of the interpreter as described here if a turnaround cannot be found. Would be a pity.

Joel Daroussin
  • 333
  • 4
  • 10
  • 2
    Indentation is crucial to interpretation of Python - an indented block indicates which lines of your code fall in which conditional structures, loops, etc. It can't be ignored. It is not like Java or other languages where the indentation is for style and convention only. – user2048643 Jul 12 '13 at 13:32
  • As user said above, indenting is an essential part of python, there's no way around it. Instead, copy the code without indents: you can usually select a rectangular block using Alt+Select. – RoadieRich Jul 12 '13 at 13:34
  • 9
    Use `ipython` instead – wim Jul 12 '13 at 13:35
  • And if they can't install ipython? – RoadieRich Jul 12 '13 at 13:36
  • I know this (one of the first things you learn in Python). Nevertheless I can assure you it would be extremely practical to have the capability I am describing here. A switch that would change the interpreter’s behaviour but only at command line. – Joel Daroussin Jul 12 '13 at 13:37
  • 2
    If you want the interpreter to ignore indentation, then how is it supposed to process your code with no way of understanding its structuring? – user2048643 Jul 12 '13 at 14:04
  • 4
    @JoelDaroussin: Nevertheless, we can assure you it would be extremely nonsensical to have the capability you are describing here. If you can't (or won't) use IPython or a decent programmer's editor (that allows rectangular selection), you have falsetru's trivial trick of adding an initial block-opening `if`. – John Y Jul 12 '13 at 14:04
  • @user2048643: as I said: I just want to test the 2 print lines only. Not the structure. But of course, in the ideal situation, if the interpreter gets a structured block (lines 3 to 8) it would "try" it's best to interpret properly, i.e. here it would ignore the indents due to the `def` but handle those due to the `if`. Not sure I am clear enough. – Joel Daroussin Jul 12 '13 at 14:28
  • @John Y: I'm using the gvim editor. It (at least mine) handles rectangles but limited to the shortest line in the rectangle. So it seems it's not a decent programmer's editor (that would be a scoop, what do you think?). – Joel Daroussin Jul 12 '13 at 14:34
  • @wim: I am already entering a new world (Python). If I could postpone entering that other IPython world for a while... Unless it's realy easy and worth. What do you say? – Joel Daroussin Jul 12 '13 at 14:37
  • For the 2 lines, one of the workarounds in the answers below should suit you fine; my case here was simply that it wouldn't make sense to completely override the interpreter by ignoring indentation of a module entirely. Even in the example you gave, how would it know which levels of indentation to handle/ignore? This sounds like a complicated and impractical modification to the interpreter; better to test small portions of your code by re-formatting with the other suggestions in a way that can be handled by the interpreter as is. – user2048643 Jul 12 '13 at 14:56
  • @JoelDaroussin: How are you selecting, copying and pasting using gvim? It works exactly as expected for me for varying line lengths. `Ctrl-v`, movement, Edit>Copy (or `"+y`), then paste into Python. – Dennis Williamson Jul 12 '13 at 15:43
  • 2
    I believe you should try IPython. Note that it is simple a different interactive interpreter, it isn't a new language. It provides better help mechanisms, better formatted output and errors, and a bunch of other useful things that the normal interactive interpreter doesn't do. It also ships a qtconsole that allow you to have true multiline editing(as well as inlined graphics.) – Bakuriu Jul 12 '13 at 16:50
  • Fyi, with Sublime Text you could just use the middle mouse button to select only the print lines without the indentation. Just saying... ;) – Ajedi32 Jul 12 '13 at 20:47
  • @Dennis Williamson: unless my `gvim` works differently than yours, if you `rectangular select` the 2 first `print` lines in the above example you'll only get ' **print "This i** ' from the first line (i.e. up to the end of the second line). Whereas e.g. `Notepad++` works better on this point. However I want to stick to `gvim`. So, if you know of a way of doing it better with `gvim`, that would be the best answer to my original question. – Joel Daroussin Jul 15 '13 at 09:20
  • @JoelDaroussin: If I press `End` or `$` to get to the end of the line instead of using the right arrow key, I get the full lines selected, – Dennis Williamson Jul 15 '13 at 10:58
  • @Dennis Williamson: I couldn't do it as you suggested because I'm working with `gVim` on Windows where keys are redefined. The correct keystroke sequence there from `Insert mode` is: `Ctrl-l v Ctrl-q $ Shift-Down or Up arrow`. This makes a block selection that includes lines up to their end whatever their length. This is exactly what I need. So it finaly apears as a gVim question rather than a Python question. Because I use this thoroughly I programmed the sequence with [http://www.autohotkey.com/]. I now have a perfect tool. Thanks to all who responded. – Joel Daroussin Jul 17 '13 at 14:44

4 Answers4

49

In such case, I use following trick (prepend if 1:):

>>> if 1:
...     print 1
...
1
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
falsetru
  • 357,413
  • 63
  • 732
  • 636
34

The ipython %cpaste function will allow you to paste indented code and work properly:

In [5]: %cpaste
Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
:        print "This is my function called with value 1."
:        print "Done."
:^D<EOF>
This is my function called with value 1.
Done.
Wooble
  • 87,717
  • 12
  • 108
  • 131
  • Just as I showed; within ipython, type `%cpaste` and paste away. (ipython will even tell you about this if you paste indented code that raises an error normally) – Wooble Jul 12 '13 at 13:42
  • If only the second indentation is copied. Even with your method, there is still `IndentationError`. – zhangyangyu Jul 12 '13 at 13:47
  • I just pasted the two print lines above into ipython qtconsole without doing anything(i.e. no magics), and it just worked. – Christoph Jul 12 '13 at 13:54
  • @zhangyangyu: if *only* the second level is copied, no, it won't give an error. If dedented stuff after it is also copied, not much you can do about it. – Wooble Jul 12 '13 at 14:03
  • Thanks! Using `IPython` is temptating. However I will have to postpone trying this until I have time to dive into it. – Joel Daroussin Jul 15 '13 at 09:24
8

Can you just avoid copying the tabs?

Notepad++ and Visual Studio allow rectangular selection by holding Shift + Alt when selecting text.

Whatever editor you use probably allows that as well.

Yes, this doesn't answer your question as asked, but maybe it will solve your problem.

edit:

gvim allows rectangular selections that includes all line contents by Ctrl-l v Ctrl-q $ Shift-Down or Up arrow. Thanks to Joel for clarifying this.

This link explains and provides other alternatives within vim: http://vim.wikia.com/wiki/Add_trailing_blanks_to_lines_for_easy_visual_blocks

dss539
  • 6,804
  • 2
  • 34
  • 64
  • Thanks! This would be the best answer if I could do it with `gvim`. Unfortunatly `gvim` limits a rectangular selection to it's shortest line (see my comment @Dennis Williamson above). – Joel Daroussin Jul 15 '13 at 09:29
  • I am not sure to understand your comment and question. Maybe the answer is in my last comment to the original posting: "I'm working with `gVim` on Windows where keys are redefined. The correct keystroke sequence there from `Insert` mode is: `Ctrl-l v Ctrl-q $ Shift-Down or Up arrow`. This makes a block selection that includes lines up to their end whatever their length. This is exactly what I need. So it finaly apears as a gVim question rather than a Python question. Because I use this thoroughly I programmed the sequence with [autohotkey.com](http://autohotkey.com).I now have a perfect tool." – Joel Daroussin Jul 23 '13 at 08:10
  • @JoelDaroussin I edited your details into this answer. Thanks for clarifying. I haven't used vim in years, so I did not have an easy way to try it out. – dss539 Jul 23 '13 at 13:59
  • Thanks. I edited your edit to specify that this sequence is for gVim under MS-Windows only. If I understood well from Denis Williamsson's comment, the sequence for gVim under Linux differs slightly. – Joel Daroussin Jul 24 '13 at 07:39
2

Well you can create dummy code blocks , so that after copy-paste your code indents the correct way. If the code from where you are copying looks like this:

...
    ...
        <python code to be copied>

Then in the shell, write:

>>> if True:
>>>     if True:
>>> <Paste your code here>
Sudipta
  • 4,773
  • 2
  • 27
  • 42
  • 3
    Not a bad suggestion, though @falsetru already offered the same thing. Also, you should never need more than one dummy block, assuming you are just pasting code that doesn't "dedent" further to the left than the initial line being copied. – John Y Jul 12 '13 at 13:54
  • @JohnY yes I had realised after posting my answer that the same thing was already told :-) – Sudipta Jul 12 '13 at 14:18