Skip to content

Commit

Permalink
Allow the renaming comm to rename things other that dataframes (#140)
Browse files Browse the repository at this point in the history
* Allow the renaming comm to rename things other that dataframes -- SQL cell variable results could be scalars nowadays.
  • Loading branch information
James Robinson authored Dec 21, 2022
1 parent c725f77 commit c411c0b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
18 changes: 6 additions & 12 deletions src/dx/comms/rename.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from typing import Optional

import pandas as pd
import structlog
from IPython import get_ipython
from IPython.core.interactiveshell import InteractiveShell
Expand All @@ -16,8 +15,11 @@ def _recv(msg):
handle_renaming_comm(msg)


UNSET = object()


def handle_renaming_comm(msg: dict, ipython_shell: Optional[InteractiveShell] = None):
"""Implementation behind renaming a SQL cell dataframe via comms"""
"""Implementation behind renaming a SQL cell output variable via comms"""
content = msg.get("content")
if not content:
return
Expand All @@ -32,16 +34,8 @@ def handle_renaming_comm(msg: dict, ipython_shell: Optional[InteractiveShell] =
# shell will be passed in from test suite, otherwise go with global shell.
ipython_shell = get_ipython()

value_to_rename = ipython_shell.user_ns.get(data["old_name"])

# Do not rename unless old_name mapped onto exactly a dataframe.
#
# (Handles case when it maps onto None, indicating that the old name
# hasn't been assigned to at all yet (i.e. user gestured to rename
# SQL cell dataframe name before the SQL cell has even been run the
# first time yet))
#
if isinstance(value_to_rename, pd.DataFrame):
value_to_rename = ipython_shell.user_ns.get(data["old_name"], UNSET)
if value_to_rename is not UNSET:
# New name can be empty string, indicating to drop reference to the var.
if data["new_name"]:
ipython_shell.user_ns[data["new_name"]] = value_to_rename
Expand Down
18 changes: 10 additions & 8 deletions tests/test_comms.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,26 +96,28 @@ def test_rename_skipped_missing_values(
handle_renaming_comm(msg, ipython_shell=get_ipython)
assert "new_df" not in get_ipython.user_ns.keys()

def test_rename_skipped_no_dataframe(
def test_rename_non_dataframes(
self,
get_ipython: TerminalInteractiveShell,
):
"""
Test that a rename comm is skipped if the variable matching
the `old_name` is not a pandas DataFrame.
Test that a rename comm is also works if non-dataframe is referenced.
"""
msg = {
"content": {
"data": {
"old_name": "old_df",
"new_name": "new_df",
"old_name": "old_var",
"new_name": "new_var",
}
}
}
get_ipython.user_ns["old_df"] = "I am not a dataframe; I am a string."
the_string = "I am not a dataframe; I am a string."
get_ipython.user_ns["old_var"] = the_string
handle_renaming_comm(msg, ipython_shell=get_ipython)
assert "new_df" not in get_ipython.user_ns.keys()
assert "old_df" in get_ipython.user_ns.keys()
assert (
"new_var" in get_ipython.user_ns.keys() and get_ipython.user_ns["new_var"] == the_string
)
assert "old_var" not in get_ipython.user_ns.keys()


class TestAssignmentComm:
Expand Down

0 comments on commit c411c0b

Please sign in to comment.