Visual Studio Code Does Not Suggest Python Object Attributes - python

I use VS Code v1.25.1 with the Python extension added. Although it suggests me the attributes and methods of libraries and classes, i.e. I type from sklearn. it makes suggestions like ensemble, exception etc., when I generate a numpy array x_data = np.linspace(0.0, 10.0, 1000000) and on the next line say I would like to use a method or an attribute such as argmax it does not show me the suggestion list after typing x_data..
In Jupyter Notebook I used to press tab after typing x_data. to see the list. I wonder if there is any shortcut to achieve the same.

If you have IntelliSense installed, it should work automatically as you type. Or you might try Ctrl + Space. It also depends on your configuration (see the link).

Related

How can I go to a function's definition in Jupyter notebook?

How can i go to a function definition in Jupyter?
I want something like Visual studio's f12, or eclipse's and PyCharm's ctrl+click.
I find it hard to believe this does not exist, yet couldn't find it
I'm not aware of such feature that will work for all kernels.
If you are using a Python kernel and have ipython installed you can use inspection functions:
%pdoc <object>: Print (or run through a pager if too long) the docstring for an object. If the given object is a class, it will print
both the class and theconstructor docstrings.
%pdef <object>: Print the call signature for any callable object. If the object is a class, print the constructor information.
%psource <object>: Print (or run through a pager if too long) the source code for an object.
%pfile <object>: Show the entire source file where an object was defined via a pager, opening it at the line where the object
definition begins.
%who/%whos: These functions give information about identifiers you have defined interactively (not things you loaded or defined in
your configuration files). %who just prints a list of identifiers and
%whos prints a table with some basic details about each identifier.
Typing ??word or word?? gives access to the full information,
including the source code where possible. Long strings are not
snipped.
Usage Example
In [4]: pd.DataFrame?
In [5]: pd.DataFrame??
In [6]: %pdef pd.Dataframe
Object `pd.Dataframe` not found.
In [7]: %pdef pd.DataFrame
Class constructor information:
pd.DataFrame(data=None, index=None, columns=None, dtype=None, copy=False)
In [8]: %pdoc pd.DataFrame
In [9]: %pfile pd.DataFrame
Resources
Dynamic object information
If you can upgrade to JupyterLab, you could install jupyterlab-go-to-definition extension with:
jupyter labextension install #krassowski/jupyterlab_go_to_definition
It supports R and Python and allows to jump to definitions with alt-click (where alt can be changed to another key modifier in the settings). For more usage details see the linked GitHub repository (above).
On the technical side, there are two ways to get the location of definitions in a notebook: static analysis and inspection in the kernel. If you cannot upgrade to JupyterLab yet, you could try to re-implement either of those for jupyter-notebook.
The kernel inspections may seem easier to implement because they do not depend on the code editor used, but have to be written for each language separately. Here is an example for Python
static analysis can be done via external tool but those will be problematic due to the nature of the notebook (being composed of multiple cells, unlike normal source code files); the approach used in the extension above is to analyze the tokens of CodeMirror editor, iterating over cells, starting from the cell with the symbol usage (where the user alt-clicked on a variable). Here is an example of implementation for such iteration.
Disclaimer: I am the author of this extension.
Edit: For an implementation which allows the same for any language (provided that you install a language server for this language), see: jupyterlab-lsp (with many other features too!). The difference is that it only uses static analysis for now (the kernel-based position retrieval is not implemented just yet).
I used below to return call signature of function, taken from above
np.matmul??
If you have the function typed out, you can use keyboard combination 'Shift+Tab' + 'Shift+Tab' to get tool tip in notebook when in edit mode, as shown below.

How to pickle a namespace in python

Is there a way to save and load an entire namespace using pickle allowing me to perform something like this:
import pickle
import numpy as np
a = 1
# other arbitrary code
pickle.dump(namespace,open('my_namespace.p','wb')
Allowing to open a new python prompt and performing the following code:
import pickle
namespace.update(pickle.load(open('my_namespace.p','rb'))
print(np.array(a)+1)
>>2
# using the rest of the namespace
If your intention is to 'freeze' a particular code version of the modules you use I suggest taking a look at Python Virtual Environment (venv). Otherwise, as jasonharper suggested - it is very unlikely to work well (particularly with modules like NumPy which are complex amalgamates of Python code and binary libraries).
venv would help you deal with that particular problem too.
I think that I get the question. You want to be able to restore all variables and interact with a shell in a different interpreter or at a different time. The easiest way to do this is probably to use a notebook like jupyter or ipython. These allow you to save all commands run and reconstitute them. Sorry for it not being a direct answer, but I think it may "equivalently accomplish" what you specified.

How to access the variables of C++ program from gdb with Python

How can one access the values like these using the Python from gdb?:
some_array_smartptr->operator[](0)->item // no errors are checked for sake of clarity
In gdb this line works fine, but I cannot figure out how to use it in Python as I am implementing the automated testing.
Please note, that both vector and smartptr are not standard, but are manually written. Semantics is the same though.
The gdb.parse_and_eval() should do exactly what you want for live-process debugging.
Documentation.
I'd like to iterate through some arrays an so on.
The parse_and_eval returns a gdb.Value. Once you have that value, you can use any of the Values methods for further access.
Example.

How do I display function arguments in ipython qtconsole?

I'm using IPython qtconsole under windows 7, and when I first write a method name and type the bracket, a popup shows method parameters.
What is the way to display that popup explicitly, once it has disappeared? This is pretty common 'show method parameters' shortcut that I'm talking about, but I've failed to find the shortcut to it after an embarrassing amount of google searches.
In Spyder, try View - Panes - Object inspector. Then type the full name of the function.
I would highly recommend relying on the Python Library Reference rather than any in-IDE tools, at least for functions and classes that are in the standard library. For objects outside those libraries however... it looks like you can type object_name followed by a question mark, that is, object_name?, to get a list of informative details about the object. (Since everything is an object, this presumably includes functions.)
For your specific question, it looks, from the iPython docs, like the TAB key is what you're looking for, but somehow I doubt you haven't already tried that.

Emacs Extension for Python Auto Complete

I'm looking for an extension which will help in Python's auto complete feature in Python.
If I type the following code:
a = [4,5,6]
a.p
Then I expect it would give me a suggestion for pop as it is one of the method of Python list. Is this thing achievable in Emacs?
I tried installing Rope, Rope mode, Ropemacs and Pymacs. The auto-complete suggestion, I get from that aren't the methods of list. But it suggests me something like print etc. Am I doing something wrong ?
Try Jedi.el. AFAIK, it should support your example.
Because Python variables don't have types, it is difficult to know what class of object a variable would contain at a given point in your code. Imagine if your code later did a = 1. Emacs would have to know when the variable a refers to a list and when it refers to a number in order to offer the correct completion. Generally this is not possible without actually running the code.
Works for me with vimrope from https://github.com/python-rope/ropevim/ (now, the official home of all rope projects)

Categories