diff --git a/IPython/Prompts.py b/IPython/Prompts.py index 1bd3d24..a70ad5b 100644 --- a/IPython/Prompts.py +++ b/IPython/Prompts.py @@ -2,7 +2,7 @@ """ Classes for handling input/output prompts. -$Id: Prompts.py 2349 2007-05-15 16:20:35Z vivainio $""" +$Id: Prompts.py 2397 2007-05-26 10:06:26Z vivainio $""" #***************************************************************************** # Copyright (C) 2001-2006 Fernando Perez @@ -30,6 +30,7 @@ from IPython.Itpl import ItplNS from IPython.ipstruct import Struct from IPython.macro import Macro from IPython.genutils import * +from IPython.ipapi import TryNext #**************************************************************************** #Color schemes for Prompts. @@ -536,8 +537,10 @@ class CachedOutput: Do ip.set_hook("result_display", my_displayhook) for custom result display, e.g. when your own objects need special formatting. """ - - return self.shell.hooks.result_display(arg) + try: + return IPython.generics.result_display(arg) + except TryNext: + return self.shell.hooks.result_display(arg) # Assign the default display method: display = _display diff --git a/IPython/generics.py b/IPython/generics.py new file mode 100644 index 0000000..9944a45 --- /dev/null +++ b/IPython/generics.py @@ -0,0 +1,28 @@ +from IPython.ipapi import TryNext +from IPython.external.simplegeneric import generic + +""" 'Generic' functions for extending IPython + +See http://cheeseshop.python.org/pypi/simplegeneric + +Here's an example from genutils.py: + + def print_lsstring(arg): + """ Prettier (non-repr-like) and more informative printer for LSString """ + print "LSString (.p, .n, .l, .s available). Value:" + print arg + + print_lsstring = result_display.when_type(LSString)(print_lsstring) + +(Yes, the nasty syntax is for python 2.3 compatibility. Your own extensions +can use the niftier decorator syntax) + +""" + +@generic +def result_display(result): + """ print the result of computation """ + raise TryNext + +result_display = generic(result_display) + diff --git a/IPython/genutils.py b/IPython/genutils.py index a9c28a2..6148b72 100644 --- a/IPython/genutils.py +++ b/IPython/genutils.py @@ -5,7 +5,7 @@ General purpose utilities. This is a grab-bag of stuff I find useful in most programs I write. Some of these things are also convenient when working at the command line. -$Id: genutils.py 2371 2007-05-23 18:40:26Z vivainio $""" +$Id: genutils.py 2397 2007-05-26 10:06:26Z vivainio $""" #***************************************************************************** # Copyright (C) 2001-2006 Fernando Perez. @@ -36,6 +36,7 @@ import warnings import IPython from IPython.Itpl import Itpl,itpl,printpl from IPython import DPyGetOpt +from IPython.generics import result_display from path import path if os.name == "nt": from IPython.winconsole import get_console_size @@ -872,6 +873,7 @@ class LSString(str): .l (or .list) : value as list (split on newlines). .n (or .nlstr): original value (the string itself). .s (or .spstr): value as whitespace-separated string. + .p (or .paths): list of path objects Any values which require transformations are computed only once and cached. @@ -912,6 +914,14 @@ class LSString(str): p = paths = property(get_paths) + +def print_lsstring(arg): + """ Prettier (non-repr-like) and more informative printer for LSString """ + print "LSString (.p, .n, .l, .s available). Value:" + print arg + +print_lsstring = result_display.when_type(LSString)(print_lsstring) + #---------------------------------------------------------------------------- class SList(list): """List derivative with a special access attributes. @@ -921,7 +931,8 @@ class SList(list): .l (or .list) : value as list (the list itself). .n (or .nlstr): value as a string, joined on newlines. .s (or .spstr): value as a string, joined on spaces. - + .p (or .paths): list of path objects + Any values which require transformations are computed only once and cached."""