IPython - cycle through blocks in input history? - python

Say I input the following:
def foo():
print(2)
Then later, when I want to edit my function, I press the Up arrow key to rewrite it, and that cycles through my inputs one line at a time (i.e. first it shows print(2), then def foo():). Is there any way to make it cycle through blocks of code the way IDLE does it?

I have this problem with an earlier version of iPython on Linux (11 I think). New versions do not do this.
I currently still use 11, and my solution was to just use the iPython qtconsole instead which does what you want. I would assume it would do the same on Windows.

Since IPython has stopped using readline, this issue has come back, the "solution" is to use PageUp and PageDown instead C-up and C-down.

Related

Spyder - hints disappear too fast

So I'm using Spyder as my Python IDE. It has a great feature which are hints, f.e when I type numpy.arange( it shows me, that I need to insert stop, start, step etc. But it appears on screen, and disappears after like 2-3s, and most of the times I don't manage to read the whole thing, but anyways I would still like to see it, just to think about what should I type. So is there a way to extend the timeout of those hints, or make them stay there until f.e I close the parentheses?
P.S Am I having delusions, or is IPython interpreter much faster than simple Python command line interpreter?
P.S2 Is there a way, to make Spyder do auto-indentation (f.e after going to a new line inside of a function?)
FryninoS,
If you put your mouse over the information box it will stay open until you move the mouse off the box.
Austin.

Ipython Notebook: Elegant way of turning off part of cells?

In my ipython notebook, there is part of cells that serves as preliminary inspection.
Now I want to turn it off, since after running it I know the status of the dataset, but I also want to keep it, so other people using this notebook can have this functionality.
How can I do it? Is there any example of doing it?
I can comment out these cells, but then switching between on and off would be quite laborious. And may not be quite convinent for other people.
I can abstract it into a function, but that itself has some methods, so the code would be quite convoluted, and may be hard to read?
Using Jupyter notebook you can click on a cell, press esc and then r. That converts it to a "raw" cell. Similar thing can be done to convert it back, esc + y. No comments needed, just key presses.
Within Jupyer notebook, go to Help -> Keyboard shortcuts for more.
Here's a snippet:
Command Mode (press Esc to enable)
↩ : enter edit mode
⇧↩ : run cell, select below
⌃↩ : run cell
⌥↩ : run cell, insert below
y : to code
m : to markdown
r : to raw
In Jupyter notebooks one can use this magic preamble at the beginning of a cell to avoid its execution:
%%script false --no-raise-error
You can use a condition at the cost of one extra indentation.
cellEnabled = 0
#cellEnabled = 1
if cellEnabled:
doA()
doB()
I had the same kind of desire and I eventually found out about the nbextension called Freeze. When you enable it, you get a nice freeze button in your toolbar. When you click it, the cell you're currently in will become "frozen". This means it will turn green (making it visually clear) and it will be ignored by the Run All process. It's also locked for editing, so you do need to unfreeze it (unlock button, two over to the left of the freeze button) before editing or running the cell. That's really easy to do though because it's just one button.
Let me know if this wasn't super clear. Otherwise, I hope this helps!

Python clear terminal without using system calls

I would like to write a python script for use on Windows and Linux that clears the screen.
Most of the examples for this are something like:
import os
os.system('cls')
which works, but is a bit dicey given all of the issues with making system calls (security issues). Is there a better way of clearing the terminal in python without needing to use system?
The best alternative I have found so far was this:
print("\033c");
but it has the slight annoyance of removing everything from the terminal
(ie I would like it to clear the terminal, but the user should be able to scroll up and see previous output in the terminal).
The following ANSI escape code should help on linux (and most *nix unless you find a really weird terminal):
print("\x1b[2J\x1b[H",end="")
It'll clear the screen and put your cursor at the top left. You can still scroll up to find your old stuff but you may have to go up a decent distance to find it.
I have absolutely no idea what it'll do on windows. You may find you need to detect the os and use a different method there.
For python 2.x you'll need to use sys.stdout.write instead of the print statement as you can't suppress the \n on print in 2.x as far as I know.
If you have special knowledge of the screen size you can use a modified version of your original print-based answer.
def cls(x):
"""Clears the screen after printing x newlines."""
print "\n" * x
print "\033c"
In Python 3.3 and later you can divine the size of the Terminal window with shutil, but I don't think there's a great way to do it in 2.7 without actually importing os, which you said should be avoided.
This piece of code doesn't call os directly from the code.
Try this:
from subprocess import call
def clear(int=None):
call('clear')
if int == 0:
exit()
clear()
It worked for me, I work on linux but I think it will work on windows to.

keyboard short cut for accessing previous statements in python IDLE using a Mac

Is there a keyboard short cut for accessing previous statements in python IDLE? I am using a Mac
Thanks
On Windows this hotkey is alt-p.
However, assuming IDLE has options in Mac ;) you should be able to find out by going to Options->Configure IDLE. Then look in the Keys tab and under Custom Key Bindings, look for "history-previous".
That should tell you what you need to know.
Using IDLE on the MAC you would need Ctrl+P (not command) for Previous statement (instruction) and Ctrl+N for Next statement.
I was struggling with this same question as I am always using the Up arrow on most of the "shell" style programs.
Also, you can scroll back up to any previous line and hit 'return', which will place that statement in the current working line.

How can I make my program execute a function from a keyboard shortcut?

How can I do it, even if my application.exe is not the focused window ?
For example, like the Windows+D shortcut... Works everywhere...
I want that ALT+1 does a function, ALT+2 does another one, and so on...
You mean like this?
You need to use pyHook.
I have a pyHook example on my site. http://fadedbluesky.com/2011/using-pyhook-to-block-windows-key/
I originally wrote this to block keys in a game written using pygame. You should be able to easily adapt it to other windows programs.

Categories