4
4
import warnings
5
5
from itertools import chain
6
6
from pathlib import Path
7
- from typing import Any
7
+ from typing import Any , Dict , List , Optional
8
8
9
9
from gitingest .exceptions import InvalidNotebookError
10
10
@@ -32,12 +32,13 @@ def process_notebook(file: Path, include_output: bool = True) -> str:
32
32
"""
33
33
try :
34
34
with file .open (encoding = "utf-8" ) as f :
35
- notebook : dict [str , Any ] = json .load (f )
35
+ notebook : Dict [str , Any ] = json .load (f )
36
36
except json .JSONDecodeError as e :
37
37
raise InvalidNotebookError (f"Invalid JSON in notebook: { file } " ) from e
38
38
39
39
# Check if the notebook contains worksheets
40
- if worksheets := notebook .get ("worksheets" ):
40
+ worksheets = notebook .get ("worksheets" )
41
+ if worksheets :
41
42
warnings .warn (
42
43
"Worksheets are deprecated as of IPEP-17. Consider updating the notebook. "
43
44
"(See: https://github.com/jupyter/nbformat and "
@@ -57,26 +58,27 @@ def process_notebook(file: Path, include_output: bool = True) -> str:
57
58
result = ["# Jupyter notebook converted to Python script." ]
58
59
59
60
for cell in cells :
60
- if cell_str := _process_cell (cell , include_output = include_output ):
61
+ cell_str = _process_cell (cell , include_output = include_output )
62
+ if cell_str :
61
63
result .append (cell_str )
62
64
63
65
return "\n \n " .join (result ) + "\n "
64
66
65
67
66
- def _process_cell (cell : dict [str , Any ], include_output : bool ) -> str | None :
68
+ def _process_cell (cell : Dict [str , Any ], include_output : bool ) -> Optional [ str ] :
67
69
"""
68
70
Process a Jupyter notebook cell and return the cell content as a string.
69
71
70
72
Parameters
71
73
----------
72
- cell : dict [str, Any]
74
+ cell : Dict [str, Any]
73
75
The cell dictionary from a Jupyter notebook.
74
76
include_output : bool
75
77
Whether to include cell outputs in the generated script
76
78
77
79
Returns
78
80
-------
79
- str | None
81
+ str, optional
80
82
The cell content as a string, or None if the cell is empty.
81
83
82
84
Raises
@@ -101,7 +103,8 @@ def _process_cell(cell: dict[str, Any], include_output: bool) -> str | None:
101
103
return f'"""\n { cell_str } \n """'
102
104
103
105
# Add cell output as comments
104
- if include_output and (outputs := cell .get ("outputs" )):
106
+ outputs = cell .get ("outputs" )
107
+ if include_output and outputs :
105
108
106
109
# Include cell outputs as comments
107
110
output_lines = []
@@ -118,18 +121,18 @@ def _process_cell(cell: dict[str, Any], include_output: bool) -> str | None:
118
121
return cell_str
119
122
120
123
121
- def _extract_output (output : dict [str , Any ]) -> list [str ]:
124
+ def _extract_output (output : Dict [str , Any ]) -> List [str ]:
122
125
"""
123
126
Extract the output from a Jupyter notebook cell.
124
127
125
128
Parameters
126
129
----------
127
- output : dict [str, Any]
130
+ output : Dict [str, Any]
128
131
The output dictionary from a Jupyter notebook cell.
129
132
130
133
Returns
131
134
-------
132
- list [str]
135
+ List [str]
133
136
The output as a list of strings.
134
137
135
138
Raises
@@ -139,15 +142,13 @@ def _extract_output(output: dict[str, Any]) -> list[str]:
139
142
"""
140
143
output_type = output ["output_type" ]
141
144
142
- match output_type :
143
- case "stream" :
144
- return output ["text" ]
145
+ if output_type == "stream" :
146
+ return output ["text" ]
145
147
146
- case "execute_result" | "display_data" :
147
- return output ["data" ]["text/plain" ]
148
+ if output_type in ( "execute_result" , "display_data" ) :
149
+ return output ["data" ]["text/plain" ]
148
150
149
- case "error" :
150
- return [f"Error: { output ['ename' ]} : { output ['evalue' ]} " ]
151
+ if output_type == "error" :
152
+ return [f"Error: { output ['ename' ]} : { output ['evalue' ]} " ]
151
153
152
- case _:
153
- raise ValueError (f"Unknown output type: { output_type } " )
154
+ raise ValueError (f"Unknown output type: { output_type } " )
0 commit comments