Skip to content

Commit 164617f

Browse files
authored
bug: Fixing pl.Config (#361)
Fixing `pl.Config` by setting envs in Rust since Nodejs `process.env` does not get passed to Rust. --------- Co-authored-by: Bidek56 <[email protected]>
1 parent 2431104 commit 164617f

File tree

4 files changed

+377
-20
lines changed

4 files changed

+377
-20
lines changed

__tests__/config.test.ts

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import pl from "@polars";
2+
3+
describe("config", () => {
4+
pl.Config.setAsciiTables(true);
5+
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+
});
13+
14+
const asciiTable = `shape: (2, 4)
15+
+-----+-----+------+------------+
16+
| int | str | bool | list |
17+
| --- | --- | --- | --- |
18+
| f64 | str | bool | list[f64] |
19+
+===============================+
20+
| 1.0 | a | true | [1.0, 2.0] |
21+
| 2.0 | b | null | [3.0] |
22+
+-----+-----+------+------------+`;
23+
24+
pl.Config.setAsciiTables(true);
25+
expect(df.toString()).toEqual(asciiTable);
26+
27+
pl.Config.setAsciiTables(false);
28+
29+
const utf8Table = `shape: (2, 4)
30+
┌─────┬─────┬──────┬────────────┐
31+
│ int ┆ str ┆ bool ┆ list │
32+
│ --- ┆ --- ┆ --- ┆ --- │
33+
│ f64 ┆ str ┆ bool ┆ list[f64] │
34+
╞═════╪═════╪══════╪════════════╡
35+
│ 1.0 ┆ a ┆ true ┆ [1.0, 2.0] │
36+
│ 2.0 ┆ b ┆ null ┆ [3.0] │
37+
└─────┴─────┴──────┴────────────┘`;
38+
39+
expect(df.toString()).toEqual(utf8Table);
40+
pl.Config.setAsciiTables();
41+
expect(df.toString()).toEqual(utf8Table);
42+
});
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+
test("setTblColumnDataTypeInline", () => {
78+
const df = pl.DataFrame({
79+
abc: [1.0, 2.5, 3.5, 5.0],
80+
def: ["d", "e", "f", "g"],
81+
xyz: [true, false, true, false],
82+
});
83+
pl.Config.setTblColumnDataTypeInline(true);
84+
expect(df.toString().length).toEqual(325);
85+
pl.Config.setTblColumnDataTypeInline();
86+
expect(df.toString().length).toEqual(233);
87+
});
88+
test("setTblHideColumnDataTypes", () => {
89+
const df = pl.DataFrame({
90+
abc: [1.0, 2.5, 3.5, 5.0],
91+
def: ["d", "e", "f", "g"],
92+
xyz: [true, false, true, false],
93+
});
94+
pl.Config.setTblHideColumnDataTypes(true);
95+
expect(df.toString().length).toEqual(189);
96+
pl.Config.setTblHideColumnDataTypes();
97+
expect(df.toString().length).toEqual(233);
98+
});
99+
test("setVerbose", () => {
100+
pl.Config.setVerbose(true);
101+
pl.Config.setVerbose(false);
102+
});
103+
test("setThousandsSeparator", () => {
104+
const df = pl.DataFrame({
105+
x: [1234567, -987654, 10101],
106+
y: [1234.5, 100000.0, -7654321.25],
107+
});
108+
pl.Config.setThousandsSeparator(true);
109+
expect(df.toString().length).toEqual(292);
110+
pl.Config.setThousandsSeparator("x");
111+
expect(df.toString().length).toEqual(292);
112+
const fn = () => pl.Config.setThousandsSeparator("xx");
113+
expect(fn).toThrow(TypeError);
114+
pl.Config.setThousandsSeparator();
115+
expect(df.toString().length).toEqual(256);
116+
});
117+
});

polars/cfg.ts

Lines changed: 202 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,223 @@
1+
import pli from "./internals/polars_internal";
2+
13
/**
24
* Configure polars; offers options for table formatting and more.
35
*/
46
export interface Config {
5-
/** Use utf8 characters to print tables */
6-
setUtf8Tables(): Config;
7-
/** 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+
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+
*/
825
setAsciiTables(): Config;
9-
/** Set the number of character used to draw the table */
26+
setAsciiTables(active: boolean): Config;
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;
1061
setTblWidthChars(width: number): Config;
11-
/** 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, 5.0], xyz: [true, false, true]})
67+
pl.Config.setTblRows(2);
68+
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;
1281
setTblRows(n: number): Config;
13-
/** Set the number of columns used to print tables */
14-
setTblCols(n: number): Config;
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+
91+
shape: (4, 3)
92+
┌─────┬───┬───────┐
93+
│ abc ┆ … ┆ xyz │
94+
│ --- ┆ ┆ --- │
95+
│ f64 ┆ ┆ bool │
96+
╞═════╪═══╪═══════╡
97+
│ 1.0 ┆ … ┆ true │
98+
│ 2.5 ┆ … ┆ false │
99+
│ 3.5 ┆ … ┆ true │
100+
│ 5.0 ┆ … ┆ false │
101+
└─────┴───┴───────┘
102+
*/
103+
setTblCols(n?: number): Config;
104+
105+
/**
106+
* Display the data type next to the column name (to the right, in parentheses).
107+
* @param active - true / false Default - true
108+
*
109+
* @example
110+
--------
111+
const df = pl.DataFrame({abc: [1.0, 2.5, 5.0], xyz: [true, false, true]})
112+
pl.Config.setTblColumnDataTypeInline(true)
113+
114+
# shape: (3, 2) shape: (3, 2)
115+
# ┌─────┬───────┐ ┌───────────┬────────────┐
116+
# │ abc ┆ xyz │ │ abc (f64) ┆ xyz (bool) │
117+
# │ --- ┆ --- │ ╞═══════════╪════════════╡
118+
# │ f64 ┆ bool │ │ 1.0 ┆ true │
119+
# ╞═════╪═══════╡ >> │ 2.5 ┆ false │
120+
# │ 1.0 ┆ true │ │ 5.0 ┆ true │
121+
# │ 2.5 ┆ false │ └───────────┴────────────┘
122+
# │ 5.0 ┆ true │
123+
# └─────┴───────┘
124+
*/
125+
126+
setTblColumnDataTypeInline(active?: boolean): Config;
127+
128+
/**
129+
* Hide table column data types (i64, f64, str etc.).
130+
* @param active - true / false Default - true
131+
132+
* @example
133+
const df = pl.DataFrame({abc: [1.0, 2.5, 5.0], xyz: [true, false, true]})
134+
pl.Config.setTblHideColumnDataTypes(true)
135+
136+
# shape: (3, 2) shape: (3, 2)
137+
# ┌─────┬───────┐ ┌─────┬───────┐
138+
# │ abc ┆ xyz │ │ abc ┆ xyz │
139+
# │ --- ┆ --- │ ╞═════╪═══════╡
140+
# │ f64 ┆ bool │ │ 1.0 ┆ true │
141+
# ╞═════╪═══════╡ >> │ 2.5 ┆ false │
142+
# │ 1.0 ┆ true │ │ 5.0 ┆ true │
143+
# │ 2.5 ┆ false │ └─────┴───────┘
144+
# │ 5.0 ┆ true │
145+
# └─────┴───────┘
146+
*/
147+
setTblHideColumnDataTypes(active?: boolean): Config;
148+
149+
/**
150+
* Enable additional verbose/debug logging.
151+
* @param active - true / false Default - true
152+
*/
153+
setVerbose(active?: boolean): Config;
154+
155+
/**
156+
* Set the thousands grouping separator character.
157+
* @param separator : string | bool
158+
Set True to use the default "," (thousands) and "." (decimal) separators.
159+
Can also set a custom char, or set ``None`` to omit the separator.
160+
*/
161+
setThousandsSeparator(separator?: string | boolean): Config;
162+
// TODO: Implement these methods
163+
// set_auto_structify
164+
// set_decimal_separator
165+
// set_engine_affinity
166+
// set_float_precision
167+
// set_fmt_float
168+
// set_fmt_str_lengths
169+
// set_fmt_table_cell_list_len
170+
// set_streaming_chunk_size
171+
// set_tbl_cell_alignment
172+
// set_tbl_cell_numeric_alignment
173+
// set_tbl_dataframe_shape_below
174+
// set_tbl_formatting
175+
// set_tbl_hide_column_names
176+
// set_tbl_hide_dataframe_shape
177+
// set_tbl_hide_dtype_separator
178+
// set_trim_decimal_zeros
15179
}
16180

17-
/**
18-
* @ignore
19-
*/
20181
export const Config: Config = {
21-
setUtf8Tables() {
22-
process.env["POLARS_FMT_NO_UTF8"] = undefined;
182+
setAsciiTables(active?: boolean) {
183+
pli.setAsciiTables(active);
184+
return this;
185+
},
186+
setTblWidthChars(width?: number) {
187+
pli.setTblWidthChars(width);
188+
return this;
189+
},
190+
setTblRows(n?: number) {
191+
pli.setTblRows(n);
192+
return this;
193+
},
194+
setTblCols(n?: number) {
195+
pli.setTblCols(n);
23196
return this;
24197
},
25-
setAsciiTables() {
26-
process.env["POLARS_FMT_NO_UTF8"] = "1";
198+
setTblColumnDataTypeInline(active?: boolean) {
199+
pli.setTblColumnDataTypeInline(active ? 1 : 0);
27200
return this;
28201
},
29-
setTblWidthChars(width) {
30-
process.env["POLARS_TABLE_WIDTH"] = String(width);
202+
setTblHideColumnDataTypes(active?: boolean) {
203+
pli.setTblHideColumnDataTypes(active ? 1 : 0);
31204
return this;
32205
},
33-
setTblRows(n) {
34-
process.env["POLARS_FMT_MAX_ROWS"] = String(n);
206+
setVerbose(active?: boolean) {
207+
pli.setVerbose(active ? 1 : 0);
35208
return this;
36209
},
37-
setTblCols(n) {
38-
process.env["POLARS_FMT_MAX_COLS"] = String(n);
210+
setThousandsSeparator(separator?: string | boolean) {
211+
if (typeof separator === "boolean" && separator) {
212+
pli.setDecimalSeparator(".");
213+
pli.setThousandsSeparator(",");
214+
} else if (typeof separator === "string") {
215+
if (separator.length > 1)
216+
throw new TypeError("separator must be a single character;");
217+
pli.setThousandsSeparator(separator);
218+
} else {
219+
pli.setThousandsSeparator();
220+
}
39221
return this;
40222
},
41223
};

0 commit comments

Comments
 (0)