##// END OF EJS Templates
Add generics.py, result_display generic, first impl is LSString result printer
vivainio -
Show More
@@ -0,0 +1,28 b''
1 from IPython.ipapi import TryNext
2 from IPython.external.simplegeneric import generic
3
4 """ 'Generic' functions for extending IPython
5
6 See http://cheeseshop.python.org/pypi/simplegeneric
7
8 Here's an example from genutils.py:
9
10 def print_lsstring(arg):
11 """ Prettier (non-repr-like) and more informative printer for LSString """
12 print "LSString (.p, .n, .l, .s available). Value:"
13 print arg
14
15 print_lsstring = result_display.when_type(LSString)(print_lsstring)
16
17 (Yes, the nasty syntax is for python 2.3 compatibility. Your own extensions
18 can use the niftier decorator syntax)
19
20 """
21
22 @generic
23 def result_display(result):
24 """ print the result of computation """
25 raise TryNext
26
27 result_display = generic(result_display)
28
@@ -2,7 +2,7 b''
2 """
2 """
3 Classes for handling input/output prompts.
3 Classes for handling input/output prompts.
4
4
5 $Id: Prompts.py 2349 2007-05-15 16:20:35Z vivainio $"""
5 $Id: Prompts.py 2397 2007-05-26 10:06:26Z vivainio $"""
6
6
7 #*****************************************************************************
7 #*****************************************************************************
8 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
8 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
@@ -30,6 +30,7 b' from IPython.Itpl import ItplNS'
30 from IPython.ipstruct import Struct
30 from IPython.ipstruct import Struct
31 from IPython.macro import Macro
31 from IPython.macro import Macro
32 from IPython.genutils import *
32 from IPython.genutils import *
33 from IPython.ipapi import TryNext
33
34
34 #****************************************************************************
35 #****************************************************************************
35 #Color schemes for Prompts.
36 #Color schemes for Prompts.
@@ -536,8 +537,10 b' class CachedOutput:'
536 Do ip.set_hook("result_display", my_displayhook) for custom result
537 Do ip.set_hook("result_display", my_displayhook) for custom result
537 display, e.g. when your own objects need special formatting.
538 display, e.g. when your own objects need special formatting.
538 """
539 """
539
540 try:
540 return self.shell.hooks.result_display(arg)
541 return IPython.generics.result_display(arg)
542 except TryNext:
543 return self.shell.hooks.result_display(arg)
541
544
542 # Assign the default display method:
545 # Assign the default display method:
543 display = _display
546 display = _display
@@ -5,7 +5,7 b' General purpose utilities.'
5 This is a grab-bag of stuff I find useful in most programs I write. Some of
5 This is a grab-bag of stuff I find useful in most programs I write. Some of
6 these things are also convenient when working at the command line.
6 these things are also convenient when working at the command line.
7
7
8 $Id: genutils.py 2371 2007-05-23 18:40:26Z vivainio $"""
8 $Id: genutils.py 2397 2007-05-26 10:06:26Z vivainio $"""
9
9
10 #*****************************************************************************
10 #*****************************************************************************
11 # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
11 # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
@@ -36,6 +36,7 b' import warnings'
36 import IPython
36 import IPython
37 from IPython.Itpl import Itpl,itpl,printpl
37 from IPython.Itpl import Itpl,itpl,printpl
38 from IPython import DPyGetOpt
38 from IPython import DPyGetOpt
39 from IPython.generics import result_display
39 from path import path
40 from path import path
40 if os.name == "nt":
41 if os.name == "nt":
41 from IPython.winconsole import get_console_size
42 from IPython.winconsole import get_console_size
@@ -872,6 +873,7 b' class LSString(str):'
872 .l (or .list) : value as list (split on newlines).
873 .l (or .list) : value as list (split on newlines).
873 .n (or .nlstr): original value (the string itself).
874 .n (or .nlstr): original value (the string itself).
874 .s (or .spstr): value as whitespace-separated string.
875 .s (or .spstr): value as whitespace-separated string.
876 .p (or .paths): list of path objects
875
877
876 Any values which require transformations are computed only once and
878 Any values which require transformations are computed only once and
877 cached.
879 cached.
@@ -912,6 +914,14 b' class LSString(str):'
912 p = paths = property(get_paths)
914 p = paths = property(get_paths)
913
915
914
916
917
918 def print_lsstring(arg):
919 """ Prettier (non-repr-like) and more informative printer for LSString """
920 print "LSString (.p, .n, .l, .s available). Value:"
921 print arg
922
923 print_lsstring = result_display.when_type(LSString)(print_lsstring)
924
915 #----------------------------------------------------------------------------
925 #----------------------------------------------------------------------------
916 class SList(list):
926 class SList(list):
917 """List derivative with a special access attributes.
927 """List derivative with a special access attributes.
@@ -921,7 +931,8 b' class SList(list):'
921 .l (or .list) : value as list (the list itself).
931 .l (or .list) : value as list (the list itself).
922 .n (or .nlstr): value as a string, joined on newlines.
932 .n (or .nlstr): value as a string, joined on newlines.
923 .s (or .spstr): value as a string, joined on spaces.
933 .s (or .spstr): value as a string, joined on spaces.
924
934 .p (or .paths): list of path objects
935
925 Any values which require transformations are computed only once and
936 Any values which require transformations are computed only once and
926 cached."""
937 cached."""
927
938
General Comments 0
You need to be logged in to leave comments. Login now