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

Making ResultSet distinguishable (#655) #19

Merged
merged 1 commit into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

* [Feature] Modified `TableDescription` to add styling, generate messages and format the calculated outputs (#459)
* [Feature] Support flexible spacing `myvar=<<` operator ([#525](https://github.com/ploomber/jupysql/issues/525))
* [Feature] Added a line under `ResultSet` to distinguish it from data frame and error message when invalid operations are performed (#468)

* [Doc] Modified integrations content to ensure they're all consistent (#523)
* [Doc] Document --persist-replace in API section (#539)
* [Fix] Fixed CI issue by updating `invalid_connection_string_duckdb` in `test_magic.py` (#631)
Expand Down
22 changes: 22 additions & 0 deletions src/sql/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@ def _repr_html_(self):
if self.pretty:
self.pretty.add_rows(self)
result = self.pretty.get_html_string()
HTML = (
"%s\n<span style='font-style:italic;font-size:11px'>"
"<code>ResultSet</code> : to convert to pandas, call <a href="
"'https://jupysql.ploomber.io/en/latest/integrations/pandas.html'>"
"<code>.DataFrame()</code></a> or to polars, call <a href="
"'https://jupysql.ploomber.io/en/latest/integrations/polars.html'>"
"<code>.PolarsDataFrame()</code></a></span><br>"
)
result = HTML % (result)

# to create clickable links
result = html.unescape(result)
result = _cell_with_spaces_pattern.sub(_nonbreaking_spaces, result)
Expand Down Expand Up @@ -179,6 +189,18 @@ def __getitem__(self, key):
raise KeyError('%d results for "%s"' % (len(result), key))
return result[0]

def __getattribute__(self, attr):
"Raises AttributeError when invalid operation is performed."
try:
return object.__getattribute__(self, attr)
except AttributeError:
err_msg = (
f"'{attr}' is not a valid operation, you can convert this "
"into a pandas data frame by calling '.DataFrame()' or a "
"polars data frame by calling '.PolarsDataFrame()'"
)
raise AttributeError(err_msg) from None

def dict(self):
"""Returns a single dict built from the result set

Expand Down
28 changes: 27 additions & 1 deletion src/tests/test_resultset.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,33 @@ def test_resultset_repr_html(result_set):
"<th>x</th>\n </tr>\n </thead>\n <tbody>\n "
"<tr>\n <td>0</td>\n </tr>\n <tr>\n "
"<td>1</td>\n </tr>\n <tr>\n <td>2</td>\n "
"</tr>\n </tbody>\n</table>"
"</tr>\n </tbody>\n</table>\n"
"<span style='font-style:italic;font-size:11px'>"
"<code>ResultSet</code> : to convert to pandas, call <a href="
"'https://jupysql.ploomber.io/en/latest/integrations/pandas.html'>"
"<code>.DataFrame()</code></a> or to polars, call <a href="
"'https://jupysql.ploomber.io/en/latest/integrations/polars.html'>"
"<code>.PolarsDataFrame()</code></a></span><br>"
)


@pytest.mark.parametrize(
"fname, parameters",
[
("head", -1),
("tail", None),
("value_counts", None),
("not_df_function", None),
],
)
def test_invalid_operation_error(result_set, fname, parameters):
with pytest.raises(AttributeError) as excinfo:
getattr(result_set, fname)(parameters)

assert str(excinfo.value) == (
f"'{fname}' is not a valid operation, you can convert this "
"into a pandas data frame by calling '.DataFrame()' or a "
"polars data frame by calling '.PolarsDataFrame()'"
)


Expand Down