How do you look up python builtin documentation with intellij/pycharm? - python

Say I have a file open and I know the type of a variable is a dict, but the editor doesn't know that. Is there a way I can navigate to dict documentation?
I tried search everywhere, but that doesn't seem to work.
Thanks!

If you haven't changed your default keymap you can place your cursor on the variable from which you want its documentation and hit Ctrl + q which opens a popup with the available documentation!
If Ctrl + q does not work for you, open File > Settings > Keymap and in the search bar search for "Quick Documentation" and use the listed hot-key mentioned there for that action!

Related

PyCharm keyword argument

How to get PyCharm to show the keyword arguments while coding, like the options you can pick from? I can get VSCode to show this normally, but I just can't figure out how to get PyCharm to do it. You'll see the difference in the screenshots. One is from PyCharm and the other one is from VSCode
If you start a new project in PyCharm, add a .py file and entire the following code:
import os
os.chdir()
And you position the cursor between the two parentheses while typing, or when you press Ctrl+P in that position, PyCharm will show a hint saying "path: int | str | bytes | PathLike[str] | PathLike[bytes]", like this:
If you're just looking for the autocomplete, instead of the type hints, similarly ensure you're typing something that could be completed and PyCharm wil show type hints you can navigate with arrow keys (or can just complete by hitting Enter).
Note that you can cause PyCharm to show it again by hitting Ctrl+Space.
Mind you, if you type something that has no logical completion (i.e. nothing starts with the string you've written so far), there is no autocompletion, and so none is shown - instead PyCharm will show a small message in its place saying 'no suggestions'.

PyCharm, how to skip autocomplete suggestions

I like the autocomplete feature of PyCharm in most cases. But sometimes it just suggests nonsense.
For example if I want to name a method one it renames it to __round__(self, n=None)
How can I skip the suggestion in this case? I didn't find the correct keyword in the PyCharm documentation.
If I write eg. def one I would prefer the following behaviour:
tab or/and enter --> accept suggestion
shift + ctrl + enter --> complete statement to def one(self):
Maybe you can tap space when the autocomplete suggestion appears and then delete the space instead of using Mouse.
It works for the first time pycharm shows suggestion, but if you use ctrl+space for suggstion and then tapping space, it will autocomplete default code suggestion.
Anyway, space is enough for preventing coding interruption
Go to File -> Settings -> Editor -> General -> Code Completion
UNCHECK - Insert selected suggestion by pressing space, dot, or other context-dependent keys
Now, you can use SPACE to skip.
TAB or RETURN will insert.

Using ycm in vim with python, how to retain the docstring preview while e.g. detailing parameters?

YouCompleteMe is a lovely tool for autocompletion in vim. It also shows the docstring of the 'hovered' autocomplete candidate, which is a very useful tool for me. This preview is sadly closed as soon as one confirms the candidate, e.g. by opening parentheses.
Example:
First Docstring is shown:
Typing a parenthesis will kill the docstring though:
Now i would love to keep the docstring while my 'cursor' is in the parentheses of whatever i just autocompletion for (to be detailed: obviously the docstring of the innermost parentheses, if they are nested).
Can this be done, and if yes, how?
Thank you so much in advance,
LJKS
Add below to your vimrc
let g:ycm_autoclose_preview_window_after_completion = 0
or Default.
The optional g:ycm_autoclose_preview_window_after_completion is 0 by default.
I think this will help you out:
let g:ycm_autoclose_preview_window_after_completion = 0 " default
let g:ycm_autoclose_preview_window_after_insertion = 1
ycm_autoclose_preview_window_after_insertion:
When this option is set to 1, YCM will auto-close the preview window after the user leaves insert mode. This option is irrelevant if g:ycm_autoclose_preview_window_after_completion is set or if no preview window is triggered.
Default: 0

How to open a newtab page using the webbrowser module in python?

I'm currently trying to open my chrome default new tab page using the webbrowser module in python. I've gotten it to work for opening up random urls, however, when I try chrome://newtab as the url, I just get a message saying that there are "no apps installed to open this type of link".
Here's the relevant bit of code (not much):
import webbrowser
webbrowser.open_new_tab("chrome://newtab")
Yes, chrome is my default browser. Thanks for the help!
Notice that the documentation states that:
Note that on some platforms, trying to open a filename using this function, may work and start the operating system’s associated program. However, this is neither supported nor portable.
It has been a while since I looked at this, but my recollection is that on at least some systems, the way it works under the hood is that is passes the given URI to a system specific built-in command which then opens the URI in the system default for whatever type of URI was passed in. In other words, the default application for a given file type is used. It doesn't matter if the URI points to a local file or not. Therefore, the URI http://examplce.comn/somefile.pdf would open the PDF file on the system default PDF viewer, which may not be the browser. As the documentation notes, this works by accident due to the underlying implementation.
However, in a different OS, such a system specific command doesn't exist, and all URIs will be opened in a web browser.
You failed to mention which OS you are working on (and I forget which OS works which way), but I suspect you are working on an OS of the first type. You might (again depends on which system you have) be able override the default behavior by specifying that a specific browser be used.
You could try setting the environment variable BROWSER as an os.pathsep-separated list of browsers to try in order. Check the value of os.pathsep (import os; print os.pathsep) to see which character is used by your system (usually ':' for POSIX or ';' for Windows) and then use that character to separate items in the list. Of course, you may need to only assign one item to the list (chrome), in which case you don't need to use the separator at all. Like this (be sure to use the correct path for your system):
SET BROWSER="C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
Or, you could try using webrowser.get() to choose your browser programically. However, support for Chrome hasn't been added until Python 3.3. If you are using Python 3.3+, then try:
import webbrowser
chrome = webbrowser.get('google-chrome') # or webbrowser.get('chrome')
chrome.open_new_tab('chrome://newtab')
Note: the above is untested. I don't know which system you have and am therefore not able to replicate your specific setup. YMMV.
Update:
As I now know you are on a pre-Python 3.3 windows machine perhaps the following will help. You can also register a browser so that Python knows about it:
pth = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
webbrowser.register('chrome', None, webbrowser.BackgroundBrowser(pth))
chrome = webbrowser.get('chrome')
chrome.open_new_tab('chrome://newtab')
Seemingly Bulletproof Rock Solid Command Line From Python Option
In development, the methods above worked well for me. Then I moved my code to a production server. I found different flavors of Windows don't all play the same with python's webdriver module - I was very bummed!
I needed a bulletproof method to open auto generated html reports in a specific order.
The code below is a modification of what I did that worked very well.
import subprocess as SP
import time
def display_reports_in_chrome(param_1, param_2):
front = f'start chrome.exe {param_1}\\reports\\' # beginning of command string
reports_list = [
'report_1.html', 'report_2.html', 'report_3.html', 'report_4.html']
url_list = []
for report in reports_list:
url_list.append(f'{front}{param_2}\\{report}') # complete build of commands
for url_cs in url_list: # url_cs = url open command string
time.sleep(0.1) # helps to ensure the tabs order correctly
clo = SP.run(url_cs, shell=True, capture_output=True) # actual shell command
# Stuff below makes sure it worked
check = clo.returncode == 0
error = clo.stderr.decode('utf-8')
url_open_ok = check and not error
err_msg = f'{error}. Let Thom know please!'
assert url_open_ok, err_msg
I should point out that I was enlightened to try this by this answer on SuperUser

Changing variable name in Spyder

I have a program with many formulas and variables. I need to change the name of some of these variables. Is there any command or function in Spyder that can help me change all the names in one step?
I am looking for something like the Matlab's MAIUSC+ENTER.
1) Select the variable you want to change, then Press CTRL + R
2) Enter the new variable name you want. (in replace by)
3) Replace all
No way to do it currently. It's an open issue.
https://github.com/spyder-ide/spyder/issues/415
Alternatively, PyCharm can be used instead of Spyder, which is now supported in Anaconda.
In PyCharm: Select the variable > Right Click > Refactor > Rename

Categories