Skip to content

Commit 6b11d4e

Browse files
committed
Implementing additional configs
1 parent 043aa34 commit 6b11d4e

File tree

4 files changed

+179
-38
lines changed

4 files changed

+179
-38
lines changed

__tests__/config.test.ts

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import pl from "@polars";
22

33
describe("config", () => {
4-
pl.Config.setAsciiTables(true);
4+
pl.Config.setAsciiTables(true);
55

6-
test("setAsciiTables", () => {
7-
const df = pl.DataFrame({
8-
int: [1, 2],
9-
str: ["a", "b"],
10-
bool: [true, null],
11-
list: [[1, 2], [3]],
12-
});
6+
test("setAsciiTables", () => {
7+
const df = pl.DataFrame({
8+
int: [1, 2],
9+
str: ["a", "b"],
10+
bool: [true, null],
11+
list: [[1, 2], [3]],
12+
});
1313

14-
const asciiTable = `shape: (2, 4)
14+
const asciiTable = `shape: (2, 4)
1515
+-----+-----+------+------------+
1616
| int | str | bool | list |
1717
| --- | --- | --- | --- |
@@ -21,7 +21,7 @@ describe("config", () => {
2121
| 2.0 | b | null | [3.0] |
2222
+-----+-----+------+------------+`;
2323

24-
pl.Config.setAsciiTables(true);
24+
pl.Config.setAsciiTables(true);
2525
expect(df.toString()).toEqual(asciiTable);
2626

2727
pl.Config.setAsciiTables(false);
@@ -38,7 +38,40 @@ describe("config", () => {
3838

3939
expect(df.toString()).toEqual(utf8Table);
4040
pl.Config.setAsciiTables();
41-
4241
expect(df.toString()).toEqual(utf8Table);
4342
});
44-
});
43+
44+
test("setTblWidthChars", () => {
45+
const df = pl.DataFrame({
46+
id: ["SEQ1", "SEQ2"],
47+
seq: ["ATGATAAAGGAG", "GCAACGCATATA"],
48+
});
49+
pl.Config.setTblWidthChars(12);
50+
expect(df.toString().length).toEqual(209);
51+
pl.Config.setTblWidthChars();
52+
expect(df.toString().length).toEqual(205);
53+
});
54+
test("setTblRows", () => {
55+
const df = pl.DataFrame({
56+
abc: [1.0, 2.5, 3.5, 5.0],
57+
xyz: [true, false, true, false],
58+
});
59+
pl.Config.setTblRows(2);
60+
expect(df.toString().length).toEqual(157);
61+
pl.Config.setTblRows();
62+
expect(df.toString().length).toEqual(173);
63+
});
64+
test("setTblCols", () => {
65+
const df = pl.DataFrame({
66+
abc: [1.0, 2.5, 3.5, 5.0],
67+
def: ["d", "e", "f", "g"],
68+
xyz: [true, false, true, false],
69+
});
70+
pl.Config.setTblCols(2);
71+
expect(df.toString().length).toEqual(213);
72+
pl.Config.setTblCols();
73+
expect(df.toString().length).toEqual(233);
74+
pl.Config.setTblCols(-1);
75+
expect(df.toString().length).toEqual(233);
76+
});
77+
});

polars/cfg.ts

Lines changed: 100 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,66 +4,142 @@ import pli from "./internals/polars_internal";
44
* Configure polars; offers options for table formatting and more.
55
*/
66
export interface Config {
7-
/** Use utf8 characters to print tables */
8-
setUtf8Tables(): Config;
9-
/** Use ascii characters to print tables */
7+
/** Use ASCII characters to display table outlines.
8+
* @param active - Set False to revert to the standard UTF8_FULL_CONDENSED formatting style. Default: true
9+
*
10+
* @example
11+
* const df = pl.DataFrame({abc: [1.0, 2.5, 5.0], xyz: [true, false, true]})
12+
pl.Config.setAsciiTables(true)
13+
console.log(df.toString());
14+
# shape: (3, 2) shape: (3, 2)
15+
# ┌─────┬───────┐ +-----+-------+
16+
# │ abc ┆ xyz │ | abc | xyz |
17+
# │ --- ┆ --- │ | --- | --- |
18+
# │ f64 ┆ bool │ | f64 | bool |
19+
# ╞═════╪═══════╡ +=============+
20+
# │ 1.0 ┆ true │ >> | 1.0 | true |
21+
# │ 2.5 ┆ false │ | 2.5 | false |
22+
# │ 5.0 ┆ true │ | 5.0 | true |
23+
# └─────┴───────┘ +-----+-------+
24+
*/
1025
setAsciiTables(): Config;
1126
setAsciiTables(active: boolean): Config;
12-
/** Set the number of character used to draw the table */
27+
/** Set the maximum width of a table in characters.
28+
* @param width - Maximum table width in characters; if n < 0 (eg: -1), display full width.
29+
*
30+
* @example
31+
* const df = pl.DataFrame({ id: ["SEQ1", "SEQ2"], seq: ["ATGATAAAGGAG", "GCAACGCATATA"] });
32+
>>> df
33+
shape: (2, 2)
34+
┌──────┬──────────────┐
35+
│ id ┆ seq │
36+
│ --- ┆ --- │
37+
│ str ┆ str │
38+
╞══════╪══════════════╡
39+
│ SEQ1 ┆ ATGATAAAGGAG │
40+
│ SEQ2 ┆ GCAACGCATATA │
41+
└──────┴──────────────┘
42+
>>> pl.Config.setTblWidthChars(12);
43+
>>> df
44+
shape: (2, 2)
45+
┌─────┬─────┐
46+
│ id ┆ seq │
47+
│ --- ┆ --- │
48+
│ str ┆ str │
49+
╞═════╪═════╡
50+
│ SEQ ┆ ATG │
51+
│ 1 ┆ ATA │
52+
│ ┆ AAG │
53+
│ ┆ GAG │
54+
│ SEQ ┆ GCA │
55+
│ 2 ┆ ACG │
56+
│ ┆ CAT │
57+
│ ┆ ATA │
58+
└─────┴─────┘
59+
*/
60+
setTblWidthChars(): Config;
1361
setTblWidthChars(width: number): Config;
14-
/** Set the number of rows used to print tables */
62+
/** Set the max number of rows used to draw the table (both Dataframe and Series).
63+
* @param n - Number of rows to display; if `n < 0` (eg: -1), display all rows (DataFrame) and all elements (Series).
64+
*
65+
* @example
66+
const df = pl.DataFrame( {abc: [1.0, 2.5, 3.5, 5.0], xyz: [True, False, True, False]} );
67+
pl.Config.setTblRows(2);
68+
console.log(df.toString());
69+
shape: (4, 2)
70+
┌─────┬───────┐
71+
│ abc ┆ xyz │
72+
│ --- ┆ --- │
73+
│ f64 ┆ bool │
74+
╞═════╪═══════╡
75+
│ 1.0 ┆ true │
76+
│ … ┆ … │
77+
│ 5.0 ┆ false │
78+
└─────┴───────┘
79+
*/
80+
setTblRows(): Config;
1581
setTblRows(n: number): Config;
16-
/** Set the number of columns used to print tables */
82+
/** Set the number of columns that are visible when displaying tables.
83+
* @param n - Number of columns to display; if `n < 0` (eg: -1), display all columns.
84+
*
85+
* @example
86+
87+
const df = pl.DataFrame( {abc: [1.0, 2.5, 3.5, 5.0], def: ["d", "e", "f", "g"], xyz: [true, false, true, false] } );
88+
// Set number of displayed columns to a low value
89+
pl.Config.setTblCols(2);
90+
shape: (4, 3)
91+
┌─────┬───┬───────┐
92+
│ abc ┆ … ┆ xyz │
93+
│ --- ┆ ┆ --- │
94+
│ f64 ┆ ┆ bool │
95+
╞═════╪═══╪═══════╡
96+
│ 1.0 ┆ … ┆ true │
97+
│ 2.5 ┆ … ┆ false │
98+
│ 3.5 ┆ … ┆ true │
99+
│ 5.0 ┆ … ┆ false │
100+
└─────┴───┴───────┘
101+
*/
102+
setTblCols(): Config;
17103
setTblCols(n: number): Config;
18104

105+
// TODO: Implement these methods
19106
// set_auto_structify
20107
// set_decimal_separator
21-
// set_engine_affinity
108+
// set_engine_affinity
22109
// set_float_precision
23110
// set_fmt_float
24111
// set_fmt_str_lengths
25112
// set_fmt_table_cell_list_len
26113
// set_streaming_chunk_size
27114
// set_tbl_cell_alignment
28115
// set_tbl_cell_numeric_alignment
29-
// set_tbl_cols
30116
// set_tbl_column_data_type_inline
31117
// set_tbl_dataframe_shape_below
32118
// set_tbl_formatting
33119
// set_tbl_hide_column_data_types
34120
// set_tbl_hide_column_names
35121
// set_tbl_hide_dataframe_shape
36122
// set_tbl_hide_dtype_separator
37-
// set_tbl_rows
38-
// set_tbl_width_chars
39123
// set_thousands_separator
40124
// set_trim_decimal_zeros
41125
// set_verbose
42-
43126
}
44127

45-
/**
46-
* @ignore
47-
*/
48128
export const Config: Config = {
49-
setUtf8Tables() {
50-
process.env["POLARS_FMT_NO_UTF8"] = undefined;
51-
return this;
52-
},
53129
setAsciiTables(active?: boolean) {
54130
pli.setAsciiTables(active);
55131
return this;
56132
},
57-
setTblWidthChars(width) {
58-
process.env["POLARS_TABLE_WIDTH"] = String(width);
133+
setTblWidthChars(width?: number) {
134+
pli.setTblWidthChars(width);
59135
return this;
60136
},
61-
setTblRows(n) {
62-
process.env["POLARS_FMT_MAX_ROWS"] = String(n);
137+
setTblRows(n?: number) {
138+
pli.setTblRows(n);
63139
return this;
64140
},
65-
setTblCols(n) {
66-
process.env["POLARS_FMT_MAX_COLS"] = String(n);
141+
setTblCols(n?: number) {
142+
pli.setTblCols(n);
67143
return this;
68144
},
69145
};

src/cfg.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/// Set the `POLARS_FMT_TABLE_FORMATTING` environment variable.
32
///
43
/// - `Some(true)` => `"ASCII_FULL_CONDENSED"`
@@ -14,3 +13,36 @@ pub fn set_ascii_tables(enabled: Option<bool>) {
1413
None => std::env::remove_var(ENV_VAR),
1514
}
1615
}
16+
17+
/// Set the `POLARS_TABLE_WIDTH` environment variable.
18+
#[napi(catch_unwind)]
19+
pub fn set_tbl_width_chars(width: Option<i32>) {
20+
const ENV_VAR: &str = "POLARS_TABLE_WIDTH";
21+
22+
match width {
23+
Some(w) => std::env::set_var(ENV_VAR, w.to_string()),
24+
None => std::env::remove_var(ENV_VAR),
25+
}
26+
}
27+
28+
/// Set the `POLARS_FMT_MAX_ROWS` environment variable.
29+
#[napi(catch_unwind)]
30+
pub fn set_tbl_rows(n: Option<i32>) {
31+
const ENV_VAR: &str = "POLARS_FMT_MAX_ROWS";
32+
33+
match n {
34+
Some(w) => std::env::set_var(ENV_VAR, w.to_string()),
35+
None => std::env::remove_var(ENV_VAR),
36+
}
37+
}
38+
39+
/// Set the `POLARS_FMT_MAX_COLS` environment variable.
40+
#[napi(catch_unwind)]
41+
pub fn set_tbl_cols(n: Option<i32>) {
42+
const ENV_VAR: &str = "POLARS_FMT_MAX_COLS";
43+
44+
match n {
45+
Some(w) => std::env::set_var(ENV_VAR, w.to_string()),
46+
None => std::env::remove_var(ENV_VAR),
47+
}
48+
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub fn version() -> &'static str {
1010
VERSION
1111
}
1212

13+
pub mod cfg;
1314
pub mod conversion;
1415
pub mod dataframe;
1516
pub mod datatypes;
@@ -23,7 +24,6 @@ pub mod series;
2324
pub mod set;
2425
pub mod sql;
2526
pub mod utils;
26-
pub mod cfg;
2727

2828
pub use polars_core;
2929

0 commit comments

Comments
 (0)