Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
N
Notebook_Processor
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Contributor analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Maurice Herné
Notebook_Processor
Commits
02b691f3
Commit
02b691f3
authored
8 months ago
by
Maurice Herné
Browse files
Options
Downloads
Patches
Plain Diff
Use model copies instead of muting the original cell, Add tests for new built-in
parent
c40f9779
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/Notebook_Processor/desired_state/cell_modifier.py
+33
-7
33 additions, 7 deletions
src/Notebook_Processor/desired_state/cell_modifier.py
tests/desired_state/test_predefined_action.py
+33
-2
33 additions, 2 deletions
tests/desired_state/test_predefined_action.py
with
66 additions
and
9 deletions
src/Notebook_Processor/desired_state/cell_modifier.py
+
33
−
7
View file @
02b691f3
...
@@ -91,7 +91,7 @@ class CellModifier(BaseModel):
...
@@ -91,7 +91,7 @@ class CellModifier(BaseModel):
"""
"""
@staticmethod
@staticmethod
def
delete
(
cell
:
Optional
[
NotebookCellAnnotation
]
=
None
,
def
delete
(
cell
:
Optional
[
NotebookCellAnnotation
]
=
None
,
# pylint: disable=unused-argument
**
kwargs
,
# pylint: disable=unused-argument
**
kwargs
,
# pylint: disable=unused-argument
):
):
"""
"""
...
@@ -125,11 +125,29 @@ class CellModifier(BaseModel):
...
@@ -125,11 +125,29 @@ class CellModifier(BaseModel):
return
cell_class
(
**
kwargs
)
return
cell_class
(
**
kwargs
)
@staticmethod
@staticmethod
def
clear_outputs
(
cell
:
NotebookCellAnnotation
,
**
kwargs
)
->
NotebookCellAnnotation
:
def
clear_outputs
(
cell
:
NotebookCellAnnotation
,
**
kwargs
# pylint: disable=unused-argument
)
->
NotebookCellAnnotation
:
"""
"""
Clear the output of the cell
Clear the output of the cell
:param cell NotebookCellAnnotation:
The cell to be modified
:param kwargs dict:
Unused keyword arguments
:return NotebookCellAnnotation:
"""
"""
cell
.
outputs
=
[]
if
hasattr
(
cell
,
"
outputs
"
):
return
cell
.
model_copy
(
deep
=
True
,
update
=
{
"
outputs
"
:
[]
}
)
# The following could happen for cells that don't have an output such as Markdown Cells.
module_logger
.
warning
(
"
Cell
'
%s
'
has no field
'
outputs
'
. Skipping clearing of outputs.
"
,
cell
.
id
)
return
cell
return
cell
# I don't want to remove this yet but I am unsure of the value of this function.
# I don't want to remove this yet but I am unsure of the value of this function.
...
@@ -164,8 +182,12 @@ class CellModifier(BaseModel):
...
@@ -164,8 +182,12 @@ class CellModifier(BaseModel):
"""
"""
Replace the given string in the source of the cell
Replace the given string in the source of the cell
"""
"""
cell
.
source
=
[
line
.
replace
(
old
,
new
)
for
line
in
cell
.
source
]
return
cell
.
model_copy
(
return
cell
deep
=
True
,
update
=
{
"
source
"
:
[
line
.
replace
(
old
,
new
)
for
line
in
cell
.
source
]
}
)
@staticmethod
@staticmethod
def
update_metadata
(
cell
:
NotebookCellAnnotation
,
*
,
def
update_metadata
(
cell
:
NotebookCellAnnotation
,
*
,
...
@@ -175,8 +197,12 @@ class CellModifier(BaseModel):
...
@@ -175,8 +197,12 @@ class CellModifier(BaseModel):
"""
"""
Update the metadata of the cell
Update the metadata of the cell
"""
"""
cell
.
metadata
.
update
(
metadata
)
return
cell
.
model_copy
(
return
cell
deep
=
True
,
update
=
{
"
metadata
"
:
metadata
}
)
@staticmethod
@staticmethod
def
report
(
cell
:
NotebookCellAnnotation
,
*
,
def
report
(
cell
:
NotebookCellAnnotation
,
*
,
...
...
This diff is collapsed.
Click to expand it.
tests/desired_state/test_predefined_action.py
+
33
−
2
View file @
02b691f3
...
@@ -19,7 +19,16 @@ def code_cell(metadata) -> NotebookCodeCellModel:
...
@@ -19,7 +19,16 @@ def code_cell(metadata) -> NotebookCodeCellModel:
id
=
"
test_code
"
,
id
=
"
test_code
"
,
cell_type
=
"
code
"
,
cell_type
=
"
code
"
,
metadata
=
metadata
,
metadata
=
metadata
,
source
=
[
"
print(
'
Hello, World!
'
)
"
]
source
=
[
"
print(
'
Hello, World!
'
)
"
],
outputs
=
[
{
"
name
"
:
"
stdout
"
,
"
output_type
"
:
"
stream
"
,
"
text
"
:
[
"
Hello, World!
\n
"
]
},
],
)
)
...
@@ -117,6 +126,27 @@ def test_CLEAR_args(code_cell: NotebookCodeCellModel, markdown_cell: NotebookMar
...
@@ -117,6 +126,27 @@ def test_CLEAR_args(code_cell: NotebookCodeCellModel, markdown_cell: NotebookMar
assert
modified_markdown_cell
.
source
==
[
"
Goodnight, World!
"
]
assert
modified_markdown_cell
.
source
==
[
"
Goodnight, World!
"
]
def
test_CLEAR_OUTPUTS_code
(
code_cell
,
metadata
):
assert
isinstance
(
CellModifier
.
Action
.
CLEAR_OUTPUTS
,
Callable
)
modified_code_cell
=
CellModifier
.
Action
.
CLEAR_OUTPUTS
(
code_cell
)
assert
modified_code_cell
.
outputs
==
[]
assert
modified_code_cell
==
NotebookCodeCellModel
(
id
=
"
test_code
"
,
cell_type
=
"
code
"
,
metadata
=
metadata
,
source
=
[
"
print(
'
Hello, World!
'
)
"
],
outputs
=
[]
)
def
test_CLEAR_OUTPUTS_markdown
(
markdown_cell
,
metadata
,
caplog
):
assert
isinstance
(
CellModifier
.
Action
.
CLEAR_OUTPUTS
,
Callable
)
caplog
.
clear
()
modified_markdown_cell
=
CellModifier
.
Action
.
CLEAR_OUTPUTS
(
markdown_cell
)
assert
caplog
.
messages
[
0
]
==
"
Cell
'"
+
markdown_cell
.
id
+
"'
has no field
'
outputs
'
. Skipping clearing of outputs.
"
assert
modified_markdown_cell
==
markdown_cell
def
test_REPLACE_IN_SOURCE
(
code_cell
,
markdown_cell
,
metadata
,
extra_args
):
def
test_REPLACE_IN_SOURCE
(
code_cell
,
markdown_cell
,
metadata
,
extra_args
):
assert
isinstance
(
CellModifier
.
Action
.
REPLACE_IN_SOURCE
,
Callable
)
assert
isinstance
(
CellModifier
.
Action
.
REPLACE_IN_SOURCE
,
Callable
)
modified_code_cell
=
CellModifier
.
Action
.
REPLACE_IN_SOURCE
(
modified_code_cell
=
CellModifier
.
Action
.
REPLACE_IN_SOURCE
(
...
@@ -137,7 +167,8 @@ def test_REPLACE_IN_SOURCE(code_cell, markdown_cell, metadata, extra_args):
...
@@ -137,7 +167,8 @@ def test_REPLACE_IN_SOURCE(code_cell, markdown_cell, metadata, extra_args):
id
=
"
test_code
"
,
id
=
"
test_code
"
,
cell_type
=
"
code
"
,
cell_type
=
"
code
"
,
metadata
=
metadata
,
metadata
=
metadata
,
source
=
[
"
print(
'
Goodbye, World!
'
)
"
]
source
=
[
"
print(
'
Goodbye, World!
'
)
"
],
outputs
=
[{
'
name
'
:
'
stdout
'
,
'
output_type
'
:
'
stream
'
,
'
text
'
:
[
'
Hello, World!
\n
'
]}],
)
)
assert
modified_markdown_cell
.
source
==
[
assert
modified_markdown_cell
.
source
==
[
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment