Skip to content

Commit

Permalink
Improve the detection of large integers
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Jun 24, 2024
1 parent d92fbab commit a2044f4
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 5 deletions.
9 changes: 8 additions & 1 deletion docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
ITables ChangeLog
=================

2.1.3 (2024-06-22)
------------------

**Fixed**
- We have improved the detection of large integers in the context of Polars DataFrames ([#291](https://github.com/mwouts/itables/issues/291))


2.1.2 (2024-06-19)
------------------

Expand All @@ -9,7 +16,7 @@ ITables ChangeLog
an automatic horizontal scrolling in Jupyter, Jupyter Book and also Streamlit if the table is too wide ([#282](https://github.com/mwouts/itables/pull/282)).

**Fixed**
- The dependencies of the streamlit components have been updated to fix a vulnerability in `ws` ([Alert #1](https://github.com/mwouts/itables/security/dependabot/1))
- The dependencies of the streamlit components have been updated to fix a vulnerability in `ws` ([Alert 1](https://github.com/mwouts/itables/security/dependabot/1))


2.1.1 (2024-06-08)
Expand Down
14 changes: 11 additions & 3 deletions src/itables/datatables_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,15 @@ def datatables_rows(df, count=None, warn_on_unexpected_types=False, pure_json=Fa
def n_suffix_for_bigints(js, pure_json=False):
def n_suffix(matchobj):
if pure_json:
return '"' + matchobj.group(1) + '"' + matchobj.group(2)
return 'BigInt("' + matchobj.group(1) + '")' + matchobj.group(2)
return matchobj.group(1) + '"' + matchobj.group(2) + '"' + matchobj.group(3)
return (
matchobj.group(1)
+ 'BigInt("'
+ matchobj.group(2)
+ '")'
+ matchobj.group(3)
)

return re.sub(r"(-?\d{16,})(,|])", n_suffix, js)
big_int_re = re.compile(r"^([\[\s]+)(-?\d{16,})(\]*)$")
parts = js.split(",")
return ",".join(re.sub(big_int_re, n_suffix, part) for part in parts)
2 changes: 1 addition & 1 deletion src/itables/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""ITables' version number"""

__version__ = "2.1.2"
__version__ = "2.1.3"
16 changes: 16 additions & 0 deletions tests/test_datatables_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,19 @@ def test_encode_max_int(large):
def test_encode_not_max_int(large):
large //= 10
assert n_suffix_for_bigints(json.dumps([large])) == "[{}]".format(large)


def test_encode_mixed_contents():
# Make sure that the bigint escape works for mixed content # 291
df = pd.DataFrame(
{
"bigint": [1666767918216000000],
"int": [1699300000000],
"float": [0.9510565400123596],
"neg": [-0.30901700258255005],
}
)
assert (
datatables_rows(df)
== '[[BigInt("1666767918216000000"), 1699300000000, 0.951057, -0.309017]]'
)
17 changes: 17 additions & 0 deletions tests/test_polars.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

from itables import to_html_datatable
from itables.javascript import datatables_rows
from itables.sample_dfs import get_dict_of_test_dfs, get_dict_of_test_series

try:
Expand All @@ -21,3 +22,19 @@ def test_show_polars_series(name, x, use_to_html):
)
def test_show_polars_df(name, df, use_to_html):
to_html_datatable(df, use_to_html)


def test_encode_mixed_contents():
# Make sure that the bigint escape works for mixed content # 291
df = polars.DataFrame(
{
"bigint": [1666767918216000000],
"int": [1699300000000],
"float": [0.9510565400123596],
"neg": [-0.30901700258255005],
}
)
assert (
datatables_rows(df)
== '[[BigInt("1666767918216000000"), 1699300000000, 0.9510565400123596, -0.30901700258255005]]'
)

0 comments on commit a2044f4

Please sign in to comment.