[IPython-User] pdoc without header
Mon Nov 28 14:37:36 CST 2011
> I use IPython to provide a command line user interface to scientists.
We use IPython for exactly the same thing (a command line user interface for astronomers, to be more exact!), and ran into the same issue.
Like Thomas said, you need to modify the pinfo behaviour for your class. We use the inspect_object decorator for this, as demonstrated in the code snippet below. (You would need to change the imports appropriately if you are using IPython 0.10 or earlier, but the functionality is the same.) Simply add an @inspect_object.when_type line for each class you want to override above the _our_doc function. We use the original pinfo for ?? functionality, as it is nice to be able to see the actual source code too.
from IPython.core.interactiveshell import InteractiveShell
from IPython.external.simplegeneric import generic
from IPython.core.page import page
ip_shell = InteractiveShell.instance()
# In order to override default docstring behaviour
# we make pinfo a generic and then use decorators
# in the code to override the docstring behaviour
# when we are looking at one of our classes.
# We keep a reference to the original pinfo in order
# to handle the '.??' case which needs to use the
# original code formatter.
original_pinfo = ip_shell.inspector.pinfo
ip_shell.inspector.pinfo = generic(original_pinfo)
inspect_object = ip_shell.inspector.pinfo
"""This is what we want to see."""
# … add more classes here, e.g. @inspect_object.when_type(AnotherClass)
def _our_doc(obj, oname, formatter, info, **kw):
if kw['detail_level'] == 1:
original_pinfo(obj, oname, formatter, info, **kw)
In : ip_shell?
Base Class: <class 'IPython.frontend.terminal.interactiveshell.TerminalInteractiveShell'>
String Form:<IPython.frontend.terminal.interactiveshell.TerminalInteractiveShell object at 0x10371e590>
Docstring: <no docstring>
In : obj = OurClass()
In : obj?
This is what we want to see.
In : obj??
Base Class: <class '__main__.OurClass'>
String Form:<__main__.OurClass object at 0x1038a39d0>
Docstring: This is what we want to see.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the IPython-User