##// END OF EJS Templates
update capture per review...
MinRK -
Show More
@@ -992,25 +992,31 b' python-profiler package from non-free.""")'
992 print macro,
992 print macro,
993
993
994 @magic_arguments.magic_arguments()
994 @magic_arguments.magic_arguments()
995 @magic_arguments.argument('-o', '--out', type=str,
995 @magic_arguments.argument('output', type=str, default='', nargs='?',
996 help="""The name of the variable in which to store stdout
996 help="""The name of the variable in which to store output.
997 This is a utils.io.CapturedIO object with stdout/err attributes
998 for the text of the captured output.
997
999
998 If unspecified: stdout is discarded
1000 CapturedOutput also has a show() method for displaying the output,
999 """
1001 and __call__ as well, so you can use that to quickly display the
1000 )
1002 output.
1001 @magic_arguments.argument('-e', '--err', type=str,
1002 help="""The name of the variable in which to store stderr
1003
1003
1004 If unspecified: stderr is discarded
1004 If unspecified, captured output is discarded.
1005 """
1005 """
1006 )
1006 )
1007 @magic_arguments.argument('--no-stderr', action="store_true",
1008 help="""Don't capture stderr."""
1009 )
1010 @magic_arguments.argument('--no-stdout', action="store_true",
1011 help="""Don't capture stdout."""
1012 )
1007 @cell_magic
1013 @cell_magic
1008 def capture(self, line, cell):
1014 def capture(self, line, cell):
1009 """run the cell, capturing stdout/err"""
1015 """run the cell, capturing stdout/err"""
1010 args = magic_arguments.parse_argstring(self.capture, line)
1016 args = magic_arguments.parse_argstring(self.capture, line)
1011 with capture_output() as io:
1017 out = not args.no_stdout
1018 err = not args.no_stderr
1019 with capture_output(out, err) as io:
1012 self.shell.run_cell(cell)
1020 self.shell.run_cell(cell)
1013 if args.out:
1021 if args.output:
1014 self.shell.user_ns[args.out] = io.stdout
1022 self.shell.user_ns[args.output] = io
1015 if args.err:
1016 self.shell.user_ns[args.err] = io.stderr
@@ -328,26 +328,50 b' class CapturedIO(object):'
328 """Simple object for containing captured stdout/err StringIO objects"""
328 """Simple object for containing captured stdout/err StringIO objects"""
329
329
330 def __init__(self, stdout, stderr):
330 def __init__(self, stdout, stderr):
331 self.stdout_io = stdout
331 self._stdout = stdout
332 self.stderr_io = stderr
332 self._stderr = stderr
333
333
334 @property
334 @property
335 def stdout(self):
335 def stdout(self):
336 return self.stdout_io.getvalue()
336 if not self._stdout:
337 return ''
338 return self._stdout.getvalue()
337
339
338 @property
340 @property
339 def stderr(self):
341 def stderr(self):
340 return self.stderr_io.getvalue()
342 if not self._stderr:
343 return ''
344 return self._stderr.getvalue()
345
346 def show(self):
347 """write my output to sys.stdout/err as appropriate"""
348 sys.stdout.write(self.stdout)
349 sys.stderr.write(self.stderr)
350 sys.stdout.flush()
351 sys.stderr.flush()
352
353 __call__ = show
341
354
342
355
343 class capture_output(object):
356 class capture_output(object):
344 """context manager for capturing stdout/err"""
357 """context manager for capturing stdout/err"""
358 stdout = True
359 stderr = True
360
361 def __init__(self, stdout=True, stderr=True):
362 self.stdout = stdout
363 self.stderr = stderr
345
364
346 def __enter__(self):
365 def __enter__(self):
347 self.sys_stdout = sys.stdout
366 self.sys_stdout = sys.stdout
348 self.sys_stderr = sys.stderr
367 self.sys_stderr = sys.stderr
349 stdout = sys.stdout = StringIO()
368
350 stderr = sys.stderr = StringIO()
369 stdout = stderr = False
370 if self.stdout:
371 stdout = sys.stdout = StringIO()
372 if self.stderr:
373 stderr = sys.stderr = StringIO()
374
351 return CapturedIO(stdout, stderr)
375 return CapturedIO(stdout, stderr)
352
376
353 def __exit__(self, exc_type, exc_value, traceback):
377 def __exit__(self, exc_type, exc_value, traceback):
@@ -47,13 +47,13 b''
47 {
47 {
48 "cell_type": "markdown",
48 "cell_type": "markdown",
49 "source": [
49 "source": [
50 "If you specify `-o` or `-e`, then stdout and/or stderr will be stored in those variables in your namespace."
50 "If you specify a name, then stdout and stderr will be stored in an object in your namespace."
51 ]
51 ]
52 },
52 },
53 {
53 {
54 "cell_type": "code",
54 "cell_type": "code",
55 "input": [
55 "input": [
56 "%%capture -o my_stdout",
56 "%%capture captured",
57 "print 'hi, stdout'",
57 "print 'hi, stdout'",
58 "print >> sys.stderr, 'hi, stderr'"
58 "print >> sys.stderr, 'hi, stderr'"
59 ],
59 ],
@@ -63,7 +63,21 b''
63 {
63 {
64 "cell_type": "code",
64 "cell_type": "code",
65 "input": [
65 "input": [
66 "my_stdout"
66 "captured"
67 ],
68 "language": "python",
69 "outputs": []
70 },
71 {
72 "cell_type": "markdown",
73 "source": [
74 "Calling the object writes the output to stdout/err as appropriate."
75 ]
76 },
77 {
78 "cell_type": "code",
79 "input": [
80 "captured()"
67 ],
81 ],
68 "language": "python",
82 "language": "python",
69 "outputs": []
83 "outputs": []
@@ -71,9 +85,7 b''
71 {
85 {
72 "cell_type": "code",
86 "cell_type": "code",
73 "input": [
87 "input": [
74 "%%capture -o my_stdout2 -e my_stderr",
88 "captured.stdout"
75 "print 'hi again, stdout'",
76 "print >> sys.stderr, 'hi there, stderr'"
77 ],
89 ],
78 "language": "python",
90 "language": "python",
79 "outputs": []
91 "outputs": []
@@ -81,8 +93,7 b''
81 {
93 {
82 "cell_type": "code",
94 "cell_type": "code",
83 "input": [
95 "input": [
84 "sys.stdout.write(my_stdout2)",
96 "captured.stderr"
85 "sys.stderr.write(my_stderr)"
86 ],
97 ],
87 "language": "python",
98 "language": "python",
88 "outputs": []
99 "outputs": []
@@ -104,7 +115,7 b''
104 {
115 {
105 "cell_type": "code",
116 "cell_type": "code",
106 "input": [
117 "input": [
107 "%%capture -o wontshutup",
118 "%%capture wontshutup",
108 "",
119 "",
109 "print \"setting up X\"",
120 "print \"setting up X\"",
110 "x = np.linspace(0,5,1000)",
121 "x = np.linspace(0,5,1000)",
@@ -120,7 +131,47 b''
120 {
131 {
121 "cell_type": "code",
132 "cell_type": "code",
122 "input": [
133 "input": [
123 "print wontshutup"
134 "wontshutup()"
135 ],
136 "language": "python",
137 "outputs": []
138 },
139 {
140 "cell_type": "markdown",
141 "source": [
142 "And you can selectively disable capturing stdout or stderr by passing `--no-stdout/err`."
143 ]
144 },
145 {
146 "cell_type": "code",
147 "input": [
148 "%%capture cap --no-stderr",
149 "print 'hi, stdout'",
150 "print >> sys.stderr, \"hello, stderr\""
151 ],
152 "language": "python",
153 "outputs": []
154 },
155 {
156 "cell_type": "code",
157 "input": [
158 "cap.stdout"
159 ],
160 "language": "python",
161 "outputs": []
162 },
163 {
164 "cell_type": "code",
165 "input": [
166 "cap.stderr"
167 ],
168 "language": "python",
169 "outputs": []
170 },
171 {
172 "cell_type": "code",
173 "input": [
174 ""
124 ],
175 ],
125 "language": "python",
176 "language": "python",
126 "outputs": []
177 "outputs": []
General Comments 0
You need to be logged in to leave comments. Login now