Skip to content

Commit

Permalink
Change RegExRemove to remove code cells with output
Browse files Browse the repository at this point in the history
Previously, the RegExRemovePreprocessor treated code cells without outputs specially: they were never removed, even if they matched the RegEx. This behavior was intentional but only documented in an internal method, check_conditions, not in the docs of an external method. See Issue #1091 for discussion.

This commit removes that behavior and updates the documentation of check_conditions. It further removes the test that checked for this behavior.
  • Loading branch information
charlesfrye committed Sep 4, 2019
1 parent aeaa214 commit fe529f6
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 24 deletions.
5 changes: 2 additions & 3 deletions nbconvert/preprocessors/regexremove.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ class RegexRemovePreprocessor(Preprocessor):

def check_conditions(self, cell):
"""
Checks that a cell matches the pattern and that (if a code cell)
it does not have any outputs.
Checks that a cell matches the pattern.
Returns: Boolean.
True means cell should *not* be removed.
Expand All @@ -54,7 +53,7 @@ def check_conditions(self, cell):
for pattern in self.patterns))

# Filter out cells that meet the pattern and have no outputs
return cell.get('outputs') or not pattern.match(cell.source)
return not pattern.match(cell.source)

def preprocess(self, nb, resources):
"""
Expand Down
21 changes: 0 additions & 21 deletions nbconvert/preprocessors/tests/test_regexremove.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,3 @@ def test_output(self):
for pattern in patterns:
self.assertFalse(pattern.match(cell.source))

def test_nosource_with_output(self):
"""
Test that the check_conditions returns true when given a code-cell
that has non-empty outputs but no source.
"""

cell = {
'cell_type': 'code',
'execution_count': 2,
'metadata': {},
'outputs': [{
'name': 'stdout',
'output_type': 'stream',
'text': 'I exist.\n'
}],
'source': ''
}
preprocessor = self.build_preprocessor()
node = from_dict(cell)
assert preprocessor.check_conditions(node)

0 comments on commit fe529f6

Please sign in to comment.