Is there a Python shortcut to circumvent need for `print()` - python

In R 3 * 2 typed on the editor can be executed in the console as [1] 6 by having the cursor on the line where the code is typed; clicking on Run if using RStudio, or through Ctrl + Enter. Very convenient.
New to Python, I am coming to realize the if I want to see 6, I may need to type print(3 * 2), unless I type the expression directly on the Python console. Or, is there a shortcut?
Incidentally, I am using Pycharm as IDE.

In the Pycharm charm editor go to
Settings > Keymap > Other
And change the kep map for "Execute selection in console". Double click it and select "Add keyboard shortcut"
I think the default is set the Alt+Shift+E. I was also from an R background before Pycharm and was used to the shortcut of Ctrl+R to run selected code. I think Ctrl+R might be something in Pycharm because I decided a long while back to map mine to Alt+R.
Once this is done, you can highlight a section and use your new shortcut to run it in a console. You can also just have a cursor on the line and using the shortcut will run the line and move to the next.

You need IPython. In particular, this ability to select a section of a saved file and re-execute it with a click or keypress is the hallmark of the Jupyter interface (previously known as "IPython Notebook").

Related

Shortcut for getting a python function doc string in VSCode

When I use browser to open Jupyter notebooks, I can use Shift + Tab shortcut to get a python function doc string; however, in VSCode, that shortcut does not work. Does anyone know which shortcut gives me the docstring there?
I found this but cannot understand the suggested solution (what is Ctrl + K Ctrl + I ?)
shows the keyboard shortcuts that have been set up and help below it will show you how to change what you want to change
When using VSCode in Windows, the default shortcut key for displaying the python function docstring is "ctrl+k ctrl+i". When using it, please place the mouse on the area of the python module or method that needs to be viewed (front or middle or back All works):
In addition, please check whether other shortcut keys of "editor.action.showHover" have been set in "keybindings.json".
Reference: Keybindings in VSCode.

How can I run selected lines in Spyder 4?

In previous versions of Spyder, you can select lines in the editor, and only run those selected lines.
In Spyder 4, when you select lines and press Ctrl+Enter it executes runcell(0, '/your/dir/file.py') which runs the whole code.
How can I run just the lines which I have selected?
The hotkey for running a the line at which the cursor is, or the currently selected lines in Spyder 4 is F9. Ctrl+Enter will run the current cell. You can read more about cells in Spyder here. Basically, these are blocks of code that can be defined in the editor using a specific syntax within the code, and can be run independently by Spyder and other IDEs.
Incidentally, Ctrl+Enter is the keyboard shortcut for running the current line in R Studio, not Spyder, which may be where you got it confused.
In Spyder 4.0.1, the keyboard shortcut for 'run cell' is set to Ctrl+Return but for 'run selection' it is set to F9. You can use F9 to run a selection or if you prefer to use Ctrl+Return, you can go to Tools -> Preferences -> Keyboard shortcuts. Search for 'run selection', double click and set Ctrl+Return as the 'New shortcut'
In Spyder, you can organize your code as runnable cells
Use the the pattern # In[] or #%% to define the start of a cell. You can also have child cells by adding additional percent signs %
In Spyder 4 the keyboard shortcut to run the highlighted lines of code is F9. and if you want to use any other shortcut then you can change it from TOOLS>>PREFERENCES>>KEYBOARD SHORTCUTS then double click on any shortcut you want to change and change with your shortcut. but be very careful if you used the same shortcut that is used for any other work then for that it will remove.

Python Shell shortcuts

I've just started learning python from scratch
I've installed 3.6 for windows
On the IDLE shell how can I get the shortcut for retyping what you previously executed (this is usually the up button)?
and also there is usually a drop down list for when you're typing something and python shows a list of what you might be about to type and you can select and it types it for you?
example:
thanks
To get last command executed, use alt+p.
Otherwise, under the Options menu, go to Configure IDLE >> Keys, and set history-next/history-previous to the keys you wish to use.
Press enter on the line you want to retype
EDIT
To show autocomplete window press TAB.
There's a default delay in 2 seconds. You can set popupwait to 0 in Options->Configure IDLE->Extensions->AutoComplete. Take into accout that you should restart you shell to activate new settings.
For Python 3.6 on Windows, specifically.
Shortcut to get what you typed previously: Alt + P
To get auto completion: Ctrl + space
Also, you can navigate the configuration for these Shortcut Keys in the Navigation bar.
All of the keybindings can be changed in Options > Configure IDLE > Keys. The ones in my answer are the default on Windows.
Cycle commands backwards: Alt+p
Cycle commands forwards: Alt+n
Alternatively, put cursor on a line you want to re-run and press Enter
Pop-up auto completion: Tab (or Ctrl+Space)
Alternatively, just set Options > Configure IDLE > General > Completions Popup Wait to 0, for the autocompletion to always come up automatically

shortcut to run current Python unit test in VSCode

The question is regarding Visual Studio Code (VSCode from here) and python VSCode extension that finds and runs py.test tests.
Is it possible to assign some shortcut to run current (under cursor) test method and/or test class?
I really like the ability to run single test straight from VSCode, but my workflow is not optimal since it is necessary to click it and just use some shortcut.
This image shows the buttons that appear when tests are found and can be run. I would like to know if it is possible to just use some custom shortcut instead of clicking this buttons.
Try Test Explorer UI and Python Test Explorer for Visual Studio Code.
The command list:
However, run-test-at-cursor doesn't work for me. I use run-file. Moreover, maybe you should join workbench.action.files.save, test-explorer.reload, and test-explorer.run-test-at-cursor with a macro extension.
Go to File > Preferences > Keyboard Shortcuts
In the searchbox, type python
All Python commands will be displayed below.
Select the command you want to add a key binding and click on the '+' button. Enter your preferred key combination in the new window.
Starting with VSCode 1.59 and the new Testing API (?) there are now these two new commands:
Run Test at Cursor
Debug Test at Cursor
Assuming the unit test you want to repeatedly run is selected in the Test sidebar (which you can do just by clicking on it), I've found a keyboard-driven option that doesn't require an extension to repeat running it. From anywhere in VS Code, type:
shift+cmd+i, which for me is bound to the command workbench.view.extension.test.
(Optional) Note that annoyingly, you sometimes have to press shift+cmd+i a second time to select the unit test in question (this seems like a bug to me - this happens with all Side Bar views)
Tab-Tab-Space (i.e. Tab, then Tab, then Space). The two tabs select the little bug symbol on your test and the Space kicks off another debug run of the selected test.
Here's how the screen should look after the two Tab presses:
and then the Space "clicks" the selected debug button.
With these 2-3 steps you can repeat this unit test ad infinitum.
HTH
In my case, a lot of test functions already have shortcuts:
I also added a shortcut to get to the test bench with the keyboard shortcuts utility.
Run/Debug/Interactive console in VSCode Editor ==>
File -> Preferences -> Extensions -> Robot > Code Lens: Enable/Disable
File -> Preferences -> Extensions -> Robot: Variables -> Edit in settings.json
need to add "ENV":"DEV"
Restart VS Code editor

Python IDLE. Auto-complete/Show completions not working

IDLE is being very dodgy as to when it will actually show an Auto-complete menu. As of late it hasn't been working at all, or, more specifically, only works during an interactive session.
I've been using Code Blocks for C, and have gotten really used to the very nice auto-complete features, so it's a bit frustrating not having them -- especially while trying to learn a new frame work and what class is associated with which methods, etc, etc..
Is there an easy fix to get IDLE auto-complete working again? I'm using python version 2.7.
Is there perhaps a simple editor I should look into? I've tried Vim, which was a little too heavy for my simple needs, Ninja, which I couldn't get to work for anything, and Sublime text 2, which I couldn't get my wxpython stuff to play with. What would a nice option be? Anything similar to Code Blocks would be cool, although, I'd be perfectly happy with IDLE if it would consistently work!
REQUESTED CLARIFICATION:
OK, so it seems I may have some of my terminology backwards. By non-interactive, I mean, for instance, right clicking on a python file and selecting "edit with IDLE". This brings up what I guess could be described as a text editor. You can enter all you code here. Once ready, you then hit F5, or select Run, and it launches (what I've been referring to as) the interactive terminal. It's here that you can type in code, press return, and instantly have that code evaluated.
What my question is referring to it the former, the part of IDLE where you edit the code. Sometimes while typing, after a . it will display the available methods, or after an open parenthesis it'll give hints as to the values expected. but the thing is, sometimes it does these things, sometimes it doesn't.
The only thing i found so far is that if an editing session of IDLE is connected with python shell (called "interactive mode" in the question, i.e. after an attempt to run the edited script) then "non-interactive" IDLE can autocomplete based on values in interactive window. For example, if I type
a = [];
a.appen
and then hit < Tab > it will do nothing, but if I previously type
a = []
in corresponding python shell, IDLE will autocomplete correctly.
So my only suggestion is import same modules with same names in python shell window in order to make them "visible" for non-interactive IDLE editor.
I had the same problem with IDLE, because I want to learn Qt and therefore autocompletion is very useful.
As it says in the settings of IDLE, you can trigger the autocomplete with "Control + Space", e.g. after a "QtGui.". Then a menu opens where you can arrow-scroll through the entries.
using the 'IDLE Editor window', you need to save and execute your code first.
The application running, turn back to the Editor window to use the auto-completion.
In my case, I had to open Options menu -> Extensions tab on the editor and look to make sure AutoComplete and other relevant options were enabled. They actually were, but by just clicking on 'Apply' even though I dint have to change anything did the trick for me.
Sometimes it has to do with the time you have to wait to get a suggestions.
When you go to options > extensions > general at completions popup wait you can change it to about 500ms.
In my case it was 2000ms by default.

Categories

Resources