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

Make TidyImports + notebook integration idemponent #267

Merged
merged 2 commits into from
Oct 23, 2023
Merged
Changes from 1 commit
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
19 changes: 9 additions & 10 deletions lib/python/pyflyby/_comms.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,23 @@ def _reformat_helper(input_code, imports):

def extract_import_statements(text):
"""This is a util for notebook interactions and extracts import statements
from some python code.

from some python code. This function also re-orders imports.
Args:
code (str): The code from which import statements have to be extracted

Returns:
(list[str], str): The first returned value is a list of all import
statements. The second returned value is the remaining code after
(str, str): The first returned value contains all the import statements.
The second returned value is the remaining code after
extracting the import statements.
"""
transformer = SourceToSourceFileImportsTransformation(text)
imports = [str(im.pretty_print()) for im in transformer.import_blocks]
imports = '\n'.join([str(im.pretty_print()) for im in transformer.import_blocks])
remaining_code = "\n".join([str(st.pretty_print()) if not isinstance(st, SourceToSourceImportBlockTransformation) else "" for st in transformer.blocks])
return imports, remaining_code

def collect_code_with_imports_on_top(imports, cell_array):
return (
"\n".join(imports)
def collect_code_with_imports_on_top(imports: str, cell_array):
return (
imports
+ "\n"
+ "\n".join(
[
Expand Down Expand Up @@ -185,7 +184,7 @@ def _recv(msg):
elif data["type"] == TIDY_IMPORTS:
checksum = data.get("checksum", '')
cell_array = data.get("cellArray", [])
import_statements, processed_cell_array = [], []
import_statements, processed_cell_array = "", []
for cell in cell_array:
text = cell.get("text")
cell_type = cell.get("type")
Expand All @@ -201,6 +200,6 @@ def _recv(msg):
"checksum": checksum,
"type": TIDY_IMPORTS,
"cells": processed_cell_array,
"imports": [im.strip() for im in import_statements],
"imports": import_statements,
}
)
Loading