Skip to content

Instantly share code, notes, and snippets.

@djedi
Created June 29, 2015 21:22
Show Gist options
  • Save djedi/688491231d61d7d0e01d to your computer and use it in GitHub Desktop.
Save djedi/688491231d61d7d0e01d to your computer and use it in GitHub Desktop.

IPython Shell

How to install: pip install ipython

  • Tab Completion
In [1]: x = 3+4j

In [2]: x.<Tab>
x.__abs__           x.__doc__           x.__gt__            x.__mod__           x.__radd__          x.__rmod__          x.__sub__
x.__add__           x.__eq__            x.__hash__          x.__mul__           x.__rdiv__          x.__rmul__          x.__subclasshook__
x.__class__         x.__float__         x.imag              x.__ne__            x.__rdivmod__       x.__rpow__          x.__truediv__
x.__coerce__        x.__floordiv__      x.__init__          x.__neg__           x.real              x.__rsub__
x.conjugate         x.__format__        x.__int__           x.__new__           x.__reduce__        x.__rtruediv__
x.__delattr__       x.__ge__            x.__le__            x.__nonzero__       x.__reduce_ex__     x.__setattr__
x.__div__           x.__getattribute__  x.__long__          x.__pos__           x.__repr__          x.__sizeof__
x.__divmod__        x.__getnewargs__    x.__lt__            x.__pow__           x.__rfloordiv__     x.__str__
  • easy access to documentation with ? and source code with ??
In [3]: x.conjugate?
Type:       builtin_function_or_method
Base Class: <type 'builtin_function_or_method'>
String Form:    <built-in method conjugate of complex object at 0x9521320>
Namespace:  Interactive
Docstring:
    complex.conjugate() -> complex

    Returns the complex conjugate of its argument. (3-4j).conjugate() == 3+4j.
  • comes with some magic commands
    • %run script.py
      • execute script. after finishing drops you to an ipython prompt.
      • all variables that are still in scope are avaibe -> debugging
    • %pdb
      • jump into the python debugger if an exception is thrown
      • insert breakpoints in your script by adding a 1/0 somewhere
def test():
    private = 1
    private += 1
    1/0


test()
In [1]: %pdb
Automatic pdb calling has been turned ON

In [2]: %run examples/pdb.py
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)

/home/eike/desy/icecube/talks/icetray/pyroot2/examples/pdb.py in <module>()
      7
      8
----> 9 test()
     10
     11

/home/eike/desy/icecube/talks/icetray/pyroot2/examples/pdb.py in test()
      4     private = 1
      5     private += 1
----> 6     1/0
      7
      8

ZeroDivisionError: integer division or modulo by zero
> /home/eike/desy/icecube/talks/icetray/pyroot2/examples/pdb.py(6)test()
      5     private += 1
----> 6     1/0
      7

ipdb> private
2
ipdb>

In [3]: test??
Type:       function
Base Class: <type 'function'>
String Form:    <function test at 0xa4ec95c>
Namespace:  Interactive
File:       /home/eike/desy/icecube/talks/icetray/pyroot2/examples/pdb.py
Definition: test()
Source:
def test():
    private = 1
    private += 1
    1/0

DB Util with IPython

Add the following to your .bashrc/.bash_profile/.zshrc:

alias db="ipython -i DB.py"

In [1]: C<tab>
CHANNEL                 CT                      ChannelWatcher          ClippingUploadConfig    ClippingUploadServices
CMSEntitlementToRW      Channel                 Clip                    ClippingUploadService   CloudSlicerJob

IPython Notebook

Additional requirements:

  • pyzmq
  • markupsafe
  • jinja2
  • tornado

Easiest to just install with:

pip install "ipython[all]"

To run IPython notebook:

ipython notebook

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment