def pdoc(self, obj, oname='', formatter=None): """Print the docstring for any object. Optional: -formatter: a function to run the docstring through for specially formatted docstrings.""" head = self.__head # so that itpl can find it even if private if formatter is None: ds = inspect.getdoc(obj) else: ds = formatter(inspect.getdoc(obj)) if type(obj) is types.ClassType: init_ds = inspect.getdoc(obj.__init__) output = itpl('$head("Class Docstring:")\n' '$indent(ds)\n' '$head("Constructor Docstring"):\n' '$indent(init_ds)') elif type(obj) is types.InstanceType and hasattr(obj, '__call__'): call_ds = inspect.getdoc(obj.__call__) if call_ds: output = itpl('$head("Class Docstring:")\n$indent(ds)\n' '$head("Calling Docstring:")\n$indent(call_ds)') else: output = ds else: output = ds if output == None: self.noinfo('documentation', oname) return page(output)
def psource(self, obj, oname=''): """Print the source code for an object.""" try: src = inspect.getsource(obj) except: self.noinfo('source', oname) else: page(self.format(src))
def pfile(self, obj, oname=''): """Show the whole file where an object was defined.""" try: sourcelines, lineno = inspect.getsourcelines(obj) except 'ha': self.noinfo('file', oname) else: # run contents of file through pager starting at line # where the object is defined page(self.format(open(inspect.getabsfile(obj)).read()), lineno)
def gphelp(): """Print information about the Gnuplot facilities in IPython.""" page(""" IPython provides an interface to access the Gnuplot scientific plotting system, in an environment similar to that of Mathematica or Matlab. New top-level global objects ---------------------------- Please see their respective docstrings for further details. - gp: a running Gnuplot instance. You can access its methods as gp.<method>. gp(`a string`) will execute the given string as if it had been typed in an interactive gnuplot window. - plot, splot, replot and hardcopy: aliases to the methods of the same name in the global running Gnuplot instance gp. These allow you to simply type: In [1]: plot(x,sin(x),title='Sin(x)') # assuming x is a Numeric array and obtain a plot of sin(x) vs x with the title 'Sin(x)'. - gp_new: a function which returns a new Gnuplot instance. This can be used to have multiple Gnuplot instances running in your session to compare different plots, each in a separate window. - Gnuplot: alias to the Gnuplot2 module, an improved drop-in replacement for the original Gnuplot.py. Gnuplot2 needs Gnuplot but redefines several of its functions with improved versions (Gnuplot2 comes with IPython). - gpdata, gpfile, gpfunc, gpgrid: aliases to Gnuplot.Data, Gnuplot.File, Gnuplot.Func and Gnuplot.GridData respectively. These functions create objects which can then be passed to the plotting commands. See the Gnuplot.py documentations for details. Keep in mind that all commands passed to a Gnuplot instance are executed in the Gnuplot namespace, where no Python variables exist. For example, for plotting sin(x) vs x as above, typing In [2]: gp('plot x,sin(x)') would not work. Instead, you would get the plot of BOTH the functions 'x' and 'sin(x)', since Gnuplot doesn't know about the 'x' Python array. The plot() method lives in python and does know about these variables. New magic functions ------------------- @gpc: pass one command to Gnuplot and execute it or open a Gnuplot shell where each line of input is executed. @gp_set_default: reset the value of IPython's global Gnuplot instance.""")
def pinfo(self, obj, oname='', ospace='', formatter=None, detail_level=0, ismagic=0): """Show detailed information about an object. Optional arguments: - ospace: name of the namespace where object is defined. - formatter: special formatter for docstrings (see pdoc) - detail_level: if set to 1, more information is given. - ismagic: special parameter for IPython's magic functions. """ header = self.__head ds = indent(inspect.getdoc(obj)) if formatter is not None: ds = formatter(ds) # store output in a list which gets joined with \n at the end. out = myStringIO() string_max = 200 # max size of strings to show (snipped if longer) shalf = int((string_max - 5) / 2) if ismagic: obj_type = 'Magic function' else: obj_type = type(obj).__name__ out.writeln(header('Type:\t\t') + obj_type) try: bclass = obj.__class__ out.writeln(header('Base Class:\t') + str(bclass)) except: pass # String form, but snip if too long in ? form (full in ??) try: ostr = str(obj) str_head = 'String Form:\t' if not detail_level and len(ostr) > string_max: ostr = ostr[:shalf] + ' <...> ' + ostr[-shalf:] ostr = ("\n" + " " * len(str_head.expandtabs())).\ join(map(string.strip,ostr.split("\n"))) out.writeln(header(str_head) + ostr) except: pass if ospace: out.writeln(header('Namespace:\t') + ospace) # Length (for strings and lists) try: length = str(len(obj)) out.writeln(header('Length:\t\t') + length) except: pass # Filename where object was defined try: file = inspect.getabsfile(obj) if file.endswith('<string>'): file = 'Dynamically generated function. No source code available.' out.writeln(header('File:\t\t') + file) except: pass # reconstruct the function definition and print it: try: defln = self.__getdef(obj, oname) except: pass else: out.write(header('Definition:\t') + self.format(defln)) # Docstrings only in detail 0 mode (unless source fails, see below) if ds and detail_level == 0: out.writeln(header('Docstring:\n') + ds) # Original source code for any callable if detail_level: try: source = self.format(inspect.getsource(obj)) out.write(header('Source:\n') + source.rstrip()) except: if ds: out.writeln(header('Docstring:\n') + ds) # Constructor docstring for classes if type(obj) is types.ClassType: # reconstruct the function definition and print it: gotdef = init_ds = 0 try: init_def = self.__getdef(obj.__init__, oname) gotdef = 1 except: gotdef = 0 try: init_ds = inspect.getdoc(obj.__init__) except: init_ds = None if gotdef or init_ds: out.writeln(header('\nConstructor information:')) if gotdef: out.write(header('Definition:\t') + self.format(init_def)) if init_ds: out.writeln(header('Docstring:\n') + indent(init_ds)) # Call form docstring for callable instances if type(obj) is types.InstanceType and hasattr(obj, '__call__'): call_ds = inspect.getdoc(obj.__call__) call_def = self.__getdef(obj.__call__, oname) out.writeln(header('Callable:\t') + 'Yes') out.write(header('Call def:\t') + self.format(call_def)) if call_ds: out.writeln(header('Call docstring:\n') + indent(call_ds)) # Finally send to printer/pager output = out.getvalue() if output: page(output)