##// END OF EJS Templates
fix n^2 performance issue in coalesce_streams preprocessor...
MinRK -
Show More
@@ -1,83 +1,85 b''
1 """Module that allows latex output notebooks to be conditioned before
1 """Module that allows latex output notebooks to be conditioned before
2 they are converted. Exposes a decorator (@cell_preprocessor) in
2 they are converted. Exposes a decorator (@cell_preprocessor) in
3 addition to the coalesce_streams pre-proccessor.
3 addition to the coalesce_streams pre-proccessor.
4 """
4 """
5 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
6 # Copyright (c) 2013, the IPython Development Team.
6 # Copyright (c) 2013, the IPython Development Team.
7 #
7 #
8 # Distributed under the terms of the Modified BSD License.
8 # Distributed under the terms of the Modified BSD License.
9 #
9 #
10 # The full license is in the file COPYING.txt, distributed with this software.
10 # The full license is in the file COPYING.txt, distributed with this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16 import re
16 import re
17
17
18 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
19 # Functions
19 # Functions
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21 def cell_preprocessor(function):
21 def cell_preprocessor(function):
22 """
22 """
23 Wrap a function to be executed on all cells of a notebook
23 Wrap a function to be executed on all cells of a notebook
24
24
25 The wrapped function should have these parameters:
25 The wrapped function should have these parameters:
26
26
27 cell : NotebookNode cell
27 cell : NotebookNode cell
28 Notebook cell being processed
28 Notebook cell being processed
29 resources : dictionary
29 resources : dictionary
30 Additional resources used in the conversion process. Allows
30 Additional resources used in the conversion process. Allows
31 preprocessors to pass variables into the Jinja engine.
31 preprocessors to pass variables into the Jinja engine.
32 index : int
32 index : int
33 Index of the cell being processed
33 Index of the cell being processed
34 """
34 """
35
35
36 def wrappedfunc(nb, resources):
36 def wrappedfunc(nb, resources):
37 for worksheet in nb.worksheets :
37 for worksheet in nb.worksheets :
38 for index, cell in enumerate(worksheet.cells):
38 for index, cell in enumerate(worksheet.cells):
39 worksheet.cells[index], resources = function(cell, resources, index)
39 worksheet.cells[index], resources = function(cell, resources, index)
40 return nb, resources
40 return nb, resources
41 return wrappedfunc
41 return wrappedfunc
42
42
43 cr_pat = re.compile(r'.*\r(?=[^\n])')
43
44
44 @cell_preprocessor
45 @cell_preprocessor
45 def coalesce_streams(cell, resources, index):
46 def coalesce_streams(cell, resources, index):
46 """
47 """
47 Merge consecutive sequences of stream output into single stream
48 Merge consecutive sequences of stream output into single stream
48 to prevent extra newlines inserted at flush calls
49 to prevent extra newlines inserted at flush calls
49
50
50 Parameters
51 Parameters
51 ----------
52 ----------
52 cell : NotebookNode cell
53 cell : NotebookNode cell
53 Notebook cell being processed
54 Notebook cell being processed
54 resources : dictionary
55 resources : dictionary
55 Additional resources used in the conversion process. Allows
56 Additional resources used in the conversion process. Allows
56 transformers to pass variables into the Jinja engine.
57 transformers to pass variables into the Jinja engine.
57 index : int
58 index : int
58 Index of the cell being processed
59 Index of the cell being processed
59 """
60 """
60
61
61 outputs = cell.get('outputs', [])
62 outputs = cell.get('outputs', [])
62 if not outputs:
63 if not outputs:
63 return cell, resources
64 return cell, resources
64
65
65 last = outputs[0]
66 last = outputs[0]
66 new_outputs = [last]
67 new_outputs = [last]
67
68 for output in outputs[1:]:
68 for output in outputs[1:]:
69 if (output.output_type == 'stream' and
69 if (output.output_type == 'stream' and
70 last.output_type == 'stream' and
70 last.output_type == 'stream' and
71 last.stream == output.stream
71 last.stream == output.stream
72 ):
72 ):
73 last.text += output.text
73 last.text += output.text
74
74
75 # Respect \r characters.
76 cr_pat = re.compile(r'.*\r(?=[^\n])')
77 last.text = cr_pat.sub('', last.text)
78 else:
75 else:
79 new_outputs.append(output)
76 new_outputs.append(output)
80 last = output
77 last = output
78
79 # process \r characters
80 for output in new_outputs:
81 if output.output_type == 'stream':
82 output.text = cr_pat.sub('', output.text)
81
83
82 cell.outputs = new_outputs
84 cell.outputs = new_outputs
83 return cell, resources
85 return cell, resources
General Comments 0
You need to be logged in to leave comments. Login now