##// END OF EJS Templates
Backport PR #5568: Use __qualname__ in pretty reprs for Python 3...
Backport PR #5568: Use __qualname__ in pretty reprs for Python 3 Closes #5566.

File last commit:

r16312:f944b4c9
r16391:8fc039d4
Show More
coalescestreams.py
77 lines | 2.3 KiB | text/x-python | PythonLexer
MinRK
Backport PR #5535: fix n^2 performance issue in coalesce_streams preprocessor...
r16235 """Preprocessor for merging consecutive stream outputs for easier handling."""
# Copyright (c) IPython Development Team.
Jonathan Frederic
Cleanup and refactor, transformers
r10674 # Distributed under the terms of the Modified BSD License.
Jonathan Frederic
Post code-review, extended refactor.
r10485
Jonathan Frederic
Use regex instead
r15869 import re
Jonathan Frederic
Post code-review, extended refactor.
r10485
Jonathan Frederic
Cleanup and refactor, transformers
r10674 def cell_preprocessor(function):
"""
Wrap a function to be executed on all cells of a notebook
Thomas Kluyver
Clean up numpydoc section headers
r13587 The wrapped function should have these parameters:
Thomas Kluyver
Improvements to docs formatting.
r12553
Jonathan Frederic
Cleanup and refactor, transformers
r10674 cell : NotebookNode cell
Notebook cell being processed
resources : dictionary
Additional resources used in the conversion process. Allows
Paul Ivanov
replace 'transformer' with 'preprocessor'
r12219 preprocessors to pass variables into the Jinja engine.
Jonathan Frederic
Cleanup and refactor, transformers
r10674 index : int
Index of the cell being processed
"""
def wrappedfunc(nb, resources):
MinRK
Backport PR #5535: fix n^2 performance issue in coalesce_streams preprocessor...
r16235 from IPython.config import Application
if Application.initialized():
Application.instance().log.debug(
"Applying preprocessor: %s", function.__name__
)
for worksheet in nb.worksheets:
Jonathan Frederic
Post code-review, extended refactor.
r10485 for index, cell in enumerate(worksheet.cells):
Jonathan Frederic
Cleanup and refactor, transformers
r10674 worksheet.cells[index], resources = function(cell, resources, index)
return nb, resources
Jonathan Frederic
Post code-review, extended refactor.
r10485 return wrappedfunc
MinRK
Backport PR #5535: fix n^2 performance issue in coalesce_streams preprocessor...
r16235 cr_pat = re.compile(r'.*\r(?=[^\n])')
Jonathan Frederic
Post code-review, extended refactor.
r10485
@cell_preprocessor
Jonathan Frederic
Cleanup and refactor, transformers
r10674 def coalesce_streams(cell, resources, index):
"""
Merge consecutive sequences of stream output into single stream
to prevent extra newlines inserted at flush calls
Parameters
----------
cell : NotebookNode cell
Notebook cell being processed
resources : dictionary
Additional resources used in the conversion process. Allows
transformers to pass variables into the Jinja engine.
index : int
Index of the cell being processed
"""
Jonathan Frederic
Post code-review, extended refactor.
r10485 outputs = cell.get('outputs', [])
if not outputs:
Jonathan Frederic
Cleanup and refactor, transformers
r10674 return cell, resources
Jonathan Frederic
Post code-review, extended refactor.
r10485 last = outputs[0]
new_outputs = [last]
for output in outputs[1:]:
if (output.output_type == 'stream' and
last.output_type == 'stream' and
last.stream == output.stream
):
Jonathan Frederic
Use regex instead
r15869 last.text += output.text
Jonathan Frederic
Post code-review, extended refactor.
r10485 else:
new_outputs.append(output)
Jonathan Frederic
FIX, coalescestreams incorrect nesting.
r12268 last = output
Jonathan Frederic
Post code-review, extended refactor.
r10485
MinRK
Backport PR #5535: fix n^2 performance issue in coalesce_streams preprocessor...
r16235 # process \r characters
for output in new_outputs:
MinRK
Backport PR #5576: only process cr if it's found...
r16312 if output.output_type == 'stream' and '\r' in output.text:
MinRK
Backport PR #5535: fix n^2 performance issue in coalesce_streams preprocessor...
r16235 output.text = cr_pat.sub('', output.text)
Jonathan Frederic
Post code-review, extended refactor.
r10485 cell.outputs = new_outputs
Jonathan Frederic
Cleanup and refactor, transformers
r10674 return cell, resources