Skip to content

Commit

Permalink
lint, bonus unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-beedie committed Jul 7, 2023
1 parent 1bbff7e commit 0295e44
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
17 changes: 11 additions & 6 deletions polars/polars-core/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ fn make_str_val(v: &str, truncate: usize) -> String {
}
}

#[cfg(any(feature = "fmt", feature = "fmt_no_tty"))]
fn field_to_str(f: &Field, str_truncate: usize) -> (String, usize) {
let name = make_str_val(f.name(), str_truncate);
let name_length = name.len();
Expand All @@ -367,12 +368,12 @@ fn field_to_str(f: &Field, str_truncate: usize) -> (String, usize) {
format!("\n{}", f.data_type())
};
let mut dtype_length = column_data_type.trim_start().len();
let mut column_separator = "\n---";
let mut separator = "\n---";
if env_is_true(FMT_TABLE_HIDE_COLUMN_SEPARATOR)
| env_is_true(FMT_TABLE_HIDE_COLUMN_NAMES)
| env_is_true(FMT_TABLE_HIDE_COLUMN_DATA_TYPES)
{
column_separator = ""
separator = ""
}
let s = if env_is_true(FMT_TABLE_INLINE_COLUMN_DATA_TYPE)
& !env_is_true(FMT_TABLE_HIDE_COLUMN_DATA_TYPES)
Expand All @@ -381,10 +382,14 @@ fn field_to_str(f: &Field, str_truncate: usize) -> (String, usize) {
dtype_length = inline_name_dtype.len();
inline_name_dtype
} else {
format!("{column_name}{column_separator}{column_data_type}")
format!("{column_name}{separator}{column_data_type}")
};

(s, std::cmp::max(name_length, dtype_length) + 2)
let mut s_len = std::cmp::max(name_length, dtype_length);
let separator_length = separator.trim().len();
if s_len < separator_length {
s_len = separator_length;
}
(s, s_len + 2)
}

#[cfg(any(feature = "fmt", feature = "fmt_no_tty"))]
Expand All @@ -393,7 +398,7 @@ fn prepare_row(
n_first: usize,
n_last: usize,
str_truncate: usize,
max_elem_lengths: &mut Vec<usize>,
max_elem_lengths: &mut [usize],
) -> Vec<String> {
let reduce_columns = n_first + n_last < row.len();
let n_elems = n_first + n_last + reduce_columns as usize;
Expand Down
15 changes: 15 additions & 0 deletions py-polars/tests/unit/test_cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,21 @@ def test_set_tbl_width_chars() -> None:
pl.Config.set_tbl_width_chars(0)
assert max(len(line) for line in str(df).split("\n")) == 19

# this check helps to check that column width bucketing
# is exact; no extraneous character allocation
df = pl.DataFrame(
{
"A": [1, 2, 3, 4, 5],
"fruits": ["banana", "banana", "apple", "apple", "banana"],
"B": [5, 4, 3, 2, 1],
"cars": ["beetle", "audi", "beetle", "beetle", "beetle"],
},
schema_overrides={"A": pl.Int64, "B": pl.Int64},
).select(pl.all(), pl.all().suffix("_suffix!"))

with pl.Config(tbl_width_chars=87):
assert max(len(line) for line in str(df).split("\n")) == 87


def test_shape_below_table_and_inlined_dtype() -> None:
df = pl.DataFrame({"a": [1, 2], "b": [3, 4], "c": [5, 6]})
Expand Down

0 comments on commit 0295e44

Please sign in to comment.