views:

814

answers:

4

I've been learning (and enjoying learning!) Python via the IPython interactive shell recently... What's your favorite feature in IPython? Are there any tips and tricks you've picked up that other people might not know about?

+4  A: 

This is a simple one, but setting the editor used by %edit to Notepad++ in Windows (via the EDITOR environment variable) and Emacs on the Mac (via the ~/.ipython/ipy_user_conf.py file).

Nice things you can do once you've set up your own editor include:

edit /foo/bar/fizz.py

to create a file with the given name and location to easily write a new module and:

edit modulename

to edit the code for a given module (provided it's currently imported). For example you could type:

import subprocess
edit subprocess

and you'll immediately be thrown into your editor of choice with the source for the subprocess module open.

Lawrence Johnston
For whatever reason, I cannot set Notepad++ to be my editor. I've uncommented "import ipy_editors", I've tried "ipy_editors.notepadplusplus()", I've tried using ipy_editors.install_editor, I've tried setting the EDITOR variable. And still, all I ever get is Notepad! Very frustrating!
eksortso
+8  A: 

The "?" prints the useful details of an object, including the docstrings: Dynamic object information

Example:

In [1]: foo = "Hello StackOverflow!"

In [2]: foo ?
Type:    str
Base Class: <type 'str'>
String Form:    Hello StackOverflow!
Namespace:  Interactive
Length:  20
Docstring:
    str(object) -> string

    Return a nice string representation of the object.
    If the argument is a string, the return value is the same object.

In [3]: foo.split ?
Type:    builtin_function_or_method
Base Class: <type 'builtin_function_or_method'>
String Form:    <built-in method split of str object at 0xb78ef590>
Namespace:  Interactive
Docstring:
    S.split([sep [,maxsplit]]) -> list of strings

    Return a list of the words in the string S, using sep as the
    delimiter string.  If maxsplit is given, at most maxsplit
    splits are done. If sep is not specified or is None, any
    whitespace string is a separator.
DZPM
Good one. You can also use ?? to avoid truncating long doc strings in the middle.
Lawrence Johnston
+4  A: 

Being able to use python for shell scripts comes in handy quite often. Being able to substitute python variables into the script with $ is also good...

Probably not a good example, but diffing two directories looks like this...

files = !find -type f
for f in files:
    other = f.replace(".", "../other_dir", 1)
    diff $f $other

And I find igrep easier to use for simple searches then

find ... | xargs grep....

bookmarks are nice to and dhist.

Pev
+3  A: 

I'm a huge fan of the tab completion as well as the pretty printing of dir( thing ) and doc( thing ).

Pat Notz
what is `doc( thing )`? just says `'doc' is not defined`
ma3