Skip to content

Commit f0fa2b9

Browse files
committed
Adding config options
1 parent 6b11d4e commit f0fa2b9

File tree

3 files changed

+159
-32
lines changed

3 files changed

+159
-32
lines changed

__tests__/config.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,44 @@ describe("config", () => {
7474
pl.Config.setTblCols(-1);
7575
expect(df.toString().length).toEqual(233);
7676
});
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+
});
77117
});

polars/cfg.ts

Lines changed: 87 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export interface Config {
1010
* @example
1111
* const df = pl.DataFrame({abc: [1.0, 2.5, 5.0], xyz: [true, false, true]})
1212
pl.Config.setAsciiTables(true)
13-
console.log(df.toString());
13+
1414
# shape: (3, 2) shape: (3, 2)
1515
# ┌─────┬───────┐ +-----+-------+
1616
# │ abc ┆ xyz │ | abc | xyz |
@@ -63,9 +63,9 @@ export interface Config {
6363
* @param n - Number of rows to display; if `n < 0` (eg: -1), display all rows (DataFrame) and all elements (Series).
6464
*
6565
* @example
66-
const df = pl.DataFrame( {abc: [1.0, 2.5, 3.5, 5.0], xyz: [True, False, True, False]} );
66+
const df = pl.DataFrame({abc: [1.0, 2.5, 5.0], xyz: [true, false, true]})
6767
pl.Config.setTblRows(2);
68-
console.log(df.toString());
68+
6969
shape: (4, 2)
7070
┌─────┬───────┐
7171
│ abc ┆ xyz │
@@ -87,6 +87,7 @@ export interface Config {
8787
const df = pl.DataFrame( {abc: [1.0, 2.5, 3.5, 5.0], def: ["d", "e", "f", "g"], xyz: [true, false, true, false] } );
8888
// Set number of displayed columns to a low value
8989
pl.Config.setTblCols(2);
90+
9091
shape: (4, 3)
9192
┌─────┬───┬───────┐
9293
│ abc ┆ … ┆ xyz │
@@ -99,9 +100,65 @@ export interface Config {
99100
│ 5.0 ┆ … ┆ false │
100101
└─────┴───┴───────┘
101102
*/
102-
setTblCols(): Config;
103-
setTblCols(n: number): Config;
103+
setTblCols(n?: number): Config;
104104

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;
105162
// TODO: Implement these methods
106163
// set_auto_structify
107164
// set_decimal_separator
@@ -113,16 +170,12 @@ export interface Config {
113170
// set_streaming_chunk_size
114171
// set_tbl_cell_alignment
115172
// set_tbl_cell_numeric_alignment
116-
// set_tbl_column_data_type_inline
117173
// set_tbl_dataframe_shape_below
118174
// set_tbl_formatting
119-
// set_tbl_hide_column_data_types
120175
// set_tbl_hide_column_names
121176
// set_tbl_hide_dataframe_shape
122177
// set_tbl_hide_dtype_separator
123-
// set_thousands_separator
124178
// set_trim_decimal_zeros
125-
// set_verbose
126179
}
127180

128181
export const Config: Config = {
@@ -142,4 +195,29 @@ export const Config: Config = {
142195
pli.setTblCols(n);
143196
return this;
144197
},
198+
setTblColumnDataTypeInline(active?: boolean) {
199+
pli.setTblColumnDataTypeInline(active ? 1 : 0);
200+
return this;
201+
},
202+
setTblHideColumnDataTypes(active?: boolean) {
203+
pli.setTblHideColumnDataTypes(active ? 1 : 0);
204+
return this;
205+
},
206+
setVerbose(active?: boolean) {
207+
pli.setVerbose(active ? 1 : 0);
208+
return this;
209+
},
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+
}
221+
return this;
222+
},
145223
};

src/cfg.rs

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,44 @@ pub fn set_ascii_tables(enabled: Option<bool>) {
1414
}
1515
}
1616

17-
/// Set the `POLARS_TABLE_WIDTH` environment variable.
1817
#[napi(catch_unwind)]
19-
pub fn set_tbl_width_chars(width: Option<i32>) {
20-
const ENV_VAR: &str = "POLARS_TABLE_WIDTH";
18+
pub fn set_thousands_separator(sep: Option<String>) {
19+
use polars_core::fmt::set_thousands_separator;
2120

22-
match width {
23-
Some(w) => std::env::set_var(ENV_VAR, w.to_string()),
24-
None => std::env::remove_var(ENV_VAR),
25-
}
21+
let sep = sep.map_or(None, |q| if q.is_empty() { None } else { q.chars().next() });
22+
23+
set_thousands_separator(sep);
2624
}
2725

28-
/// Set the `POLARS_FMT_MAX_ROWS` environment variable.
2926
#[napi(catch_unwind)]
30-
pub fn set_tbl_rows(n: Option<i32>) {
31-
const ENV_VAR: &str = "POLARS_FMT_MAX_ROWS";
27+
pub fn set_decimal_separator(sep: Option<String>) {
28+
use polars_core::fmt::set_decimal_separator;
3229

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-
}
30+
let sep = sep.map_or(None, |q| if q.is_empty() { None } else { q.chars().next() });
3831

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";
32+
set_decimal_separator(sep);
33+
}
4334

44-
match n {
45-
Some(w) => std::env::set_var(ENV_VAR, w.to_string()),
46-
None => std::env::remove_var(ENV_VAR),
47-
}
35+
macro_rules! set_var {
36+
($name:ident, $var_name:expr) => {
37+
#[napi(catch_unwind)]
38+
pub fn $name(n: Option<i32>) {
39+
match n {
40+
Some(w) => std::env::set_var($var_name, w.to_string()),
41+
None => std::env::remove_var($var_name),
42+
}
43+
}
44+
};
4845
}
46+
set_var!(set_verbose, "POLARS_VERBOSE");
47+
set_var!(set_tbl_rows, "POLARS_FMT_MAX_ROWS");
48+
set_var!(set_tbl_cols, "POLARS_FMT_MAX_COLS");
49+
set_var!(set_tbl_width_chars, "POLARS_TABLE_WIDTH");
50+
set_var!(
51+
set_tbl_column_data_type_inline,
52+
"POLARS_FMT_TABLE_INLINE_COLUMN_DATA_TYPE"
53+
);
54+
set_var!(
55+
set_tbl_hide_column_data_types,
56+
"POLARS_FMT_TABLE_HIDE_COLUMN_DATA_TYPES"
57+
);

0 commit comments

Comments
 (0)