Skip to content

Commit 4e430b3

Browse files
committed
Add _format_single_number_with_gt() util fn
1 parent 4c23931 commit 4e430b3

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

pointblank/validate.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13820,6 +13820,35 @@ def _format_numbers_with_gt(
1382013820
return formatted_values
1382113821

1382213822

13823+
def _format_single_number_with_gt(
13824+
value: int, n_sigfig: int = 3, compact: bool = True, locale: str = "en", df_lib=None
13825+
) -> str:
13826+
"""Format a single number using Great Tables GT object to avoid pandas dependency."""
13827+
if df_lib is None:
13828+
# Use library detection to select appropriate DataFrame library
13829+
if _is_lib_present("polars"):
13830+
import polars as pl
13831+
13832+
df_lib = pl
13833+
elif _is_lib_present("pandas"):
13834+
import pandas as pd
13835+
13836+
df_lib = pd
13837+
else:
13838+
raise ImportError("Neither Polars nor Pandas is available for formatting")
13839+
13840+
# Create a single-row, single-column DataFrame using the specified library
13841+
df = df_lib.DataFrame({"value": [value]})
13842+
13843+
# Create GT object and format the column
13844+
gt_obj = GT(df).fmt_number(columns="value", n_sigfig=n_sigfig, compact=compact, locale=locale)
13845+
13846+
# Extract the formatted value using _get_column_of_values
13847+
formatted_values = _get_column_of_values(gt_obj, column_name="value", context="html")
13848+
13849+
return formatted_values[0] # Return the single formatted value
13850+
13851+
1382313852
def _transform_test_units(
1382413853
test_units: list[int], interrogation_performed: bool, active: list[bool], locale: str
1382513854
) -> list[str]:

0 commit comments

Comments
 (0)