Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop converting jupyter to python after class definition is found #238

Merged
merged 4 commits into from
Feb 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions zntrack/core/jupyter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

def jupyter_class_to_file(silent, nb_name, module_name):
"""Extract the class definition form an ipynb file"""
# TOOD is it really module_name and not class name?

if not silent:
log.warning(
Expand All @@ -27,6 +28,7 @@ def jupyter_class_to_file(silent, nb_name, module_name):
)

reading_class = False
found_node = False

imports = ""

Expand All @@ -52,6 +54,15 @@ def jupyter_class_to_file(silent, nb_name, module_name):
if line.startswith("@"): # handle decorators
reading_class = True
class_definition += line
if line.startswith(f"class {module_name}") or line.startswith(
f"def {module_name}"
):
found_node = True
if found_node and not reading_class:
if re.match(r"#.*zntrack:.*break", line):
# stop converting the file after this line if the Node was already
# found
break

src = imports + "\n\n" + class_definition

Expand Down