Show More
@@ -11,9 +11,13 b' addition to the coalesce_streams pre-proccessor.' | |||
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | # Functions | |
|
14 | # Imports | |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | import re | |
|
16 | 17 | |
|
18 | #----------------------------------------------------------------------------- | |
|
19 | # Functions | |
|
20 | #----------------------------------------------------------------------------- | |
|
17 | 21 | def cell_preprocessor(function): |
|
18 | 22 | """ |
|
19 | 23 | Wrap a function to be executed on all cells of a notebook |
@@ -67,6 +71,10 b' def coalesce_streams(cell, resources, index):' | |||
|
67 | 71 | last.stream == output.stream |
|
68 | 72 | ): |
|
69 | 73 | last.text += output.text |
|
74 | ||
|
75 | # Respect \r characters. | |
|
76 | cr_pat = re.compile(r'.*\r(?=[^\n])') | |
|
77 | last.text = cr_pat.sub('', last.text) | |
|
70 | 78 | else: |
|
71 | 79 | new_outputs.append(output) |
|
72 | 80 | last = output |
@@ -13,7 +13,6 b' Module with tests for the coalescestreams preprocessor' | |||
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | # Imports |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | ||
|
17 | 16 | from IPython.nbformat import current as nbformat |
|
18 | 17 | |
|
19 | 18 | from .base import PreprocessorTestsBase |
@@ -23,7 +22,6 b' from ..coalescestreams import coalesce_streams' | |||
|
23 | 22 | #----------------------------------------------------------------------------- |
|
24 | 23 | # Class |
|
25 | 24 | #----------------------------------------------------------------------------- |
|
26 | ||
|
27 | 25 | class TestCoalesceStreams(PreprocessorTestsBase): |
|
28 | 26 | """Contains test functions for coalescestreams.py""" |
|
29 | 27 | |
@@ -38,10 +36,8 b' class TestCoalesceStreams(PreprocessorTestsBase):' | |||
|
38 | 36 | self.assertEqual(outputs[2].text, "cd") |
|
39 | 37 | self.assertEqual(outputs[3].text, "ef") |
|
40 | 38 | |
|
41 | ||
|
42 | 39 | def test_coalesce_sequenced_streams(self): |
|
43 | 40 | """Can the coalesce streams preprocessor merge a sequence of streams?""" |
|
44 | ||
|
45 | 41 | outputs = [nbformat.new_output(output_type="stream", stream="stdout", output_text="0"), |
|
46 | 42 | nbformat.new_output(output_type="stream", stream="stdout", output_text="1"), |
|
47 | 43 | nbformat.new_output(output_type="stream", stream="stdout", output_text="2"), |
@@ -58,3 +54,20 b' class TestCoalesceStreams(PreprocessorTestsBase):' | |||
|
58 | 54 | nb, res = coalesce_streams(nb, res) |
|
59 | 55 | outputs = nb.worksheets[0].cells[0].outputs |
|
60 | 56 | self.assertEqual(outputs[0].text, u'01234567') |
|
57 | ||
|
58 | def test_coalesce_replace_streams(self): | |
|
59 | """Are \\r characters handled?""" | |
|
60 | outputs = [nbformat.new_output(output_type="stream", stream="stdout", output_text="z"), | |
|
61 | nbformat.new_output(output_type="stream", stream="stdout", output_text="\ra"), | |
|
62 | nbformat.new_output(output_type="stream", stream="stdout", output_text="\nz\rb"), | |
|
63 | nbformat.new_output(output_type="stream", stream="stdout", output_text="\nz"), | |
|
64 | nbformat.new_output(output_type="stream", stream="stdout", output_text="\rc\n"), | |
|
65 | nbformat.new_output(output_type="stream", stream="stdout", output_text="z\rz\rd")] | |
|
66 | cells=[nbformat.new_code_cell(input="# None", prompt_number=1,outputs=outputs)] | |
|
67 | worksheets = [nbformat.new_worksheet(name="worksheet1", cells=cells)] | |
|
68 | ||
|
69 | nb = nbformat.new_notebook(name="notebook1", worksheets=worksheets) | |
|
70 | res = self.build_resources() | |
|
71 | nb, res = coalesce_streams(nb, res) | |
|
72 | outputs = nb.worksheets[0].cells[0].outputs | |
|
73 | self.assertEqual(outputs[0].text, u'a\nb\nc\nd') |
General Comments 0
You need to be logged in to leave comments.
Login now