##// END OF EJS Templates
Added a new table icap, which executes a Python string...
walter.doerwald -
Show More
@@ -83,7 +83,7 b' three extensions points (all of them optional):'
83 83 """
84 84
85 85 import sys, os, os.path, stat, glob, new, csv, datetime, types
86 import itertools, mimetypes
86 import itertools, mimetypes, StringIO
87 87
88 88 try: # Python 2.3 compatibility
89 89 import collections
@@ -136,7 +136,7 b' import astyle'
136 136 __all__ = [
137 137 "ifile", "ils", "iglob", "iwalk", "ipwdentry", "ipwd", "igrpentry", "igrp",
138 138 "icsv", "ix", "ichain", "isort", "ifilter", "ieval", "ienum",
139 "ienv", "ihist", "idump", "iless"
139 "ienv", "ihist", "icap", "idump", "iless"
140 140 ]
141 141
142 142
@@ -1966,6 +1966,81 b' class iless(Display):'
1966 1966 print "%s: %s" % (exc.__class__.__name__, str(exc))
1967 1967
1968 1968
1969 class _RedirectIO(object):
1970 def __init__(self,*args,**kwargs):
1971 """
1972 Map the system output streams to self.
1973 """
1974 self.stream = StringIO.StringIO()
1975 self.stdout = sys.stdout
1976 sys.stdout = self
1977 self.stderr = sys.stderr
1978 sys.stderr = self
1979
1980 def write(self, text):
1981 """
1982 Write both to screen and to self.
1983 """
1984 self.stream.write(text)
1985 self.stdout.write(text)
1986 if "\n" in text:
1987 self.stdout.flush()
1988
1989 def writelines(self, lines):
1990 """
1991 Write lines both to screen and to self.
1992 """
1993 self.stream.writelines(lines)
1994 self.stdout.writelines(lines)
1995 self.stdout.flush()
1996
1997 def restore(self):
1998 """
1999 Restore the default system streams.
2000 """
2001 self.stdout.flush()
2002 self.stderr.flush()
2003 sys.stdout = self.stdout
2004 sys.stderr = self.stderr
2005
2006
2007 class icap(Table):
2008 """
2009 Execute a python string and capture any output to stderr/stdout.
2010
2011 Examples:
2012
2013 >>> import time
2014 >>> icap("for i in range(10): print i, time.sleep(0.1)")
2015
2016 """
2017 def __init__(self, expr, globals=None):
2018 self.expr = expr
2019 self.globals = globals
2020 log = _RedirectIO()
2021 try:
2022 exec(expr, getglobals(globals))
2023 finally:
2024 log.restore()
2025 self.stream = log.stream
2026
2027 def __iter__(self):
2028 self.stream.seek(0)
2029 for line in self.stream:
2030 yield line.rstrip("\r\n")
2031
2032 def __xrepr__(self, mode="default"):
2033 if mode == "header" or mode == "footer":
2034 yield (astyle.style_default,
2035 "%s(%r)" % (self.__class__.__name__, self.expr))
2036 else:
2037 yield (astyle.style_default, repr(self))
2038
2039 def __repr__(self):
2040 return "%s.%s(%r)" % \
2041 (self.__class__.__module__, self.__class__.__name__, self.expr)
2042
2043
1969 2044 def xformat(value, mode, maxlength):
1970 2045 align = None
1971 2046 full = True
@@ -1,3 +1,9 b''
1 2007-06-05 Walter Doerwald <walter@livinglogic.de>
2
3 * IPython/Extensions/ipipe.py: Added a new table icap, which executes a
4 Python string and captures the output. (Idea and original patch by
5 St�fan van der Walt)
6
1 7 2007-06-01 Fernando Perez <Fernando.Perez@colorado.edu>
2 8
3 9 * IPython/ultraTB.py (VerboseTB.text): update printing of
General Comments 0
You need to be logged in to leave comments. Login now