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?
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.
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.
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.