What do "In [X]:" and "Out[Y]:" mean in Python code? - python

I'm a new Python user, and I'm sure this is a really basic question, but I can't find the answer anywhere. When people post Python code online it is often formatted like this:
In [1]: # some stuff
Out[1]:
# some more stuff
What are the In's, Out's, and the numbers? And why does my Python console not behave like that?

They are not Python code. They are IPython prompts, a popular Python add-on interactive shell.
Each line of code executed on the interactive prompt (denoted by In) is numbered, and so is the output produced (denoted by Out). You can then instruct IPython to refer back to those inputs and outputs for re-running code or re-using output.
See the Input caching system documentation>
IPython offers numbered prompts (In/Out) with input and output caching (also referred to as ‘input history’). All input is saved and can be retrieved as variables (besides the usual arrow key recall), in addition to the %rep magic command that brings a history entry up for editing on the next command line.

Related

Using IDLE to display script output like in Shell

I hope this makes sense. When using the IDLE python shell and typing the commands one by one, there is an output or response to most lines of code typed.
When writing a script and then running that script in IDLE I don't get to see the same output in the shell, is there a way of enabling it, or a line of code to add to my script so it displays?
Thanks
A feature of both standard interactive mode and the IDLE Shell (and presumably of other Python IDEs) is that the value of expressions entered in response to the >>> or other interactive prompt is echoed on a line below.
When running a script, the value of an expression statement is not printed. This is because such expressions are often included in files for their side effects, and because output may not be wanted. One must explicitly print() or sys.stdout.write() to see the result . So
>>> 2+3
5
must be written, for instance, as print(2+3) in a program executed all at once, whether from any editor or from a command line. (In other words, this question is not really IDLE specific, although it is a common one from beginners, who often use IDLE.)
If this is not your problem, we need much more information to help.

How to execute my code block by block?

I am new to Python, have some experience in MatLab and r. My question is: Is it possible to run part of the code in .py block by block (line by line)?
In r or Matlab, I can first have some data and variables loaded in the memory first. Then experimentally I can run a line or two to try out the syntax... this is particularly useful for new learners I believe. I know there is something called the iPython which can execute Python code line by line however this is not what I am after. Thanks.
Since ipython has already been discounted, I'm not sure this answer will be better. But I will tell you the two things that I do.
I drop into the debugger at the point where I want to "try out" something, so the code will run up to that point, and then drop me into the debugger. You do this simply by inserting this code at that point:
import pdb; pdb.set_trace()
Once you've done what needs to be done, you can either press q to quit, or c to continue running the process.
I use the -i option to python. This enters interactive mode at the end of your python code. This is useful if you want to set up a bunch of data structures, and try out some code on it, instead of typing all of it into a python shell first. (that might be why you rejected ipython?)
I think what you need is a debugger.
You can use the pydev plugin for Eclipse which has a debugger.
Another option is pdb as already suggested but it's not very easy to use.

getting bash completion options programmatically

I want a function that programmatically returns completion options from either bash or zsh. There are lots of examples of related questions on stackoverflow but no proper, generic answers anywhere. I do NOT want to know how to write a specific completer function for bash.
I've already tried implementing this by reading debian /etc/completion shell code, by echoing control-codes for tab into "bash -i", and even tried using automated subprocess interaction with python-pexpect. Every time I thought I was successful, I find some small problem that invalidates the whole solution. I'd accept a solution in any language, but ideally it would be python. Obviously the exact input output would vary depending on systems, but take a look at the example I/O below:
function("git lo") returns ["log","lol","lola"]
function("apt-get inst") returns ["apt-get install"]
function("apt-get") returns []
function("apt-get ") returns ["apt-get autoclean","apt-get autoremove", ...]
function ("./setup") returns ["./setup.py"]
If you are thinking of a solution written in shell, it would ideally be something I can execute without "source"ing. For instance bash "compgen" command looks interesting (try "compgen -F _git"), but note that "bash -c 'compgen -F _git'" does not work because the completion helper "_git" is not in scope.
This gist is my best solution so far. It meets all the requirements, works well for multiple versions of bash on multiple OS's but it requires a subprocess call and it's so complicated it's absurd. The comments includes full documentation of all the outrageous slings and arrows. I'm still hoping for something more reasonable to come along, but unless it does.. this is it!

Ipython raw_input work around?

I'm running a Python program that wants to accept raw_input which Ipython notebook does not do. (a known limitation)
What is a recommended way to achieve the functionality? (work around?) What I'd like to do is to be able to run the program, accept input and respond..(will be choices determined based on information retrieved), and also prompting for user id and password info..
Of course I'd like to do as little violence to the existing code as possible.
I found IPython.utils.io.raw_input_ext(prompt='', ps2='... ') in the Ipython docs but it calls raw_input and gets the same not implemented error
Last developement version of IPython now support raw_input in notebook. (since beginning of may 2013 for future reader)

introspective code completion with VIM? ... or other lightweight editor with this feature?

I've been all over the web trying to find a way to get VIM to have code completion similar to PyDev. It doesn't seem like it is possible!
-I have tried to use the omnicompletion suggested at this link: http://blog.dispatched.ch/2009/05/24/vim-as-python-ide/ .
-I have tried several addons to alleviate the problem, none work.
The "omnicomplete" functionality is NOT what I am looking for. It just takes all the words in the file you are working on and uses those to try and complete what I am doing. For example if I wrote:
import numpy
a_single_array = range(100)
np.a#[then I hit cntrl+n to code complete]
It would spit out "a_single_array" as a possible completion -- but that is absurd! That is not a valid completion for "numpy.a ..."
What is the issue here? All the addon would have to do is run a dir(work you want to find) from the folder you are in and then filter the output! This cannot be that difficult! (I suppose you would also have to read the file you are currently editing and filter that as well to take note of name changes... but that's pretty much it!)
Speaking of how easy it would be... if there isn't anything already made, I was thinking of writing the script myself! Any guides on how to do THAT?
No, the omni completion functionality is EXACTLY what you are looking for.
You are using <C-n> instead of <C-x><C-o>:
type <C-n> & <C-p> to complete with words from the buffer (after and before the cursor respectively)
type <C-x><C-o> to complete method/properties names
It's specifically explained in the article you linked:
In V7, VIM introduced omni completion – given it is configured to recognize Python (if not, this feature is only a plugin away) Ctrl+x Ctrl+o opens a drop down dialog like any other IDE – even the whole Pydoc gets to be displayed in a split window.
Ctrln is insert-completion.
Ctrlx Ctrlo is omni-completion.
I remap omnicompletion to CtrlSpace:
inoremap <C-Space> <C-x><C-o>
You could also try SuperTab.
I have no idea about the various completion options for Python in Vim. But if you want to roll your own you'd be well advised to study and modify one of the existing ones, like this:
http://www.vim.org/scripts/script.php?script_id=1542
Also, if all your omnicompletion is doing is listing words in current file then you don't have it set up properly for Python-specific completion. . . . Not sure how good the specialized Python completion systems get, but they certainly does compete based on Python units external to your current file. . . .

Categories