Skip to content

Commit

Permalink
Making ResultSet distinguishable (#655)
Browse files Browse the repository at this point in the history
  • Loading branch information
bbeat2782 authored Jun 28, 2023
1 parent b202771 commit 84c464e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
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

0 comments on commit 84c464e

Please sign in to comment.