Class: Polars::Config
- Inherits:
-
Object
- Object
- Polars::Config
- Defined in:
- lib/polars/config.rb
Overview
Configure polars; offers options for table formatting and more.
Constant Summary collapse
- POLARS_CFG_ENV_VARS =
[ "POLARS_ACTIVATE_DECIMAL", "POLARS_AUTO_STRUCTIFY", "POLARS_FMT_MAX_COLS", "POLARS_FMT_MAX_ROWS", "POLARS_FMT_STR_LEN", "POLARS_FMT_TABLE_CELL_ALIGNMENT", "POLARS_FMT_TABLE_DATAFRAME_SHAPE_BELOW", "POLARS_FMT_TABLE_FORMATTING", "POLARS_FMT_TABLE_HIDE_COLUMN_DATA_TYPES", "POLARS_FMT_TABLE_HIDE_COLUMN_NAMES", "POLARS_FMT_TABLE_HIDE_COLUMN_SEPARATOR", "POLARS_FMT_TABLE_HIDE_DATAFRAME_SHAPE_INFORMATION", "POLARS_FMT_TABLE_INLINE_COLUMN_DATA_TYPE", "POLARS_FMT_TABLE_ROUNDED_CORNERS", "POLARS_STREAMING_CHUNK_SIZE", "POLARS_TABLE_WIDTH", "POLARS_VERBOSE" ]
- POLARS_CFG_DIRECT_VARS =
{"set_fmt_float" => Plr.method(:get_float_fmt)}
Class Method Summary collapse
-
.activate_decimals(active = true) ⇒ Config
Activate
Decimal
data types. -
.load(cfg) ⇒ Config
Load and set previously saved (or shared) Config options from json/file.
-
.restore_defaults ⇒ Config
Reset all polars Config settings to their default state.
-
.save ⇒ Config
Save the current set of Config options as a json string or file.
-
.set_ascii_tables(active = true) ⇒ Config
Use ASCII characters to display table outlines (set False to revert to UTF8).
-
.set_auto_structify(active = true) ⇒ Config
Allow multi-output expressions to be automatically turned into Structs.
-
.set_fmt_float(fmt = "mixed") ⇒ Config
Control how floating point values are displayed.
-
.set_fmt_str_lengths(n) ⇒ Config
Set the number of characters used to display string values.
-
.set_streaming_chunk_size(size) ⇒ Config
Overwrite chunk size used in
streaming
engine. -
.set_tbl_cell_alignment(format) ⇒ Config
Set table cell alignment.
-
.set_tbl_cols(n) ⇒ Config
Set the number of columns that are visible when displaying tables.
-
.set_tbl_column_data_type_inline(active = true) ⇒ Config
Moves the data type inline with the column name (to the right, in parentheses).
-
.set_tbl_dataframe_shape_below(active = true) ⇒ Config
Print the dataframe shape below the dataframe when displaying tables.
-
.set_tbl_formatting(format = nil, rounded_corners: false) ⇒ Config
Set table formatting style.
-
.set_tbl_hide_column_data_types(active = true) ⇒ Config
Hide table column data types (i64, f64, str etc.).
-
.set_tbl_hide_column_names(active = true) ⇒ Config
Hide table column names.
-
.set_tbl_hide_dataframe_shape(active = true) ⇒ Config
Hide the shape information of the dataframe when displaying tables.
-
.set_tbl_hide_dtype_separator(active = true) ⇒ Config
Hide the '---' separator between the column names and column types.
-
.set_tbl_rows(n) ⇒ Config
Set the max number of rows used to draw the table (both Dataframe and Series).
-
.set_tbl_width_chars(width) ⇒ Config
Set the number of characters used to draw the table.
-
.set_verbose(active = true) ⇒ Config
Enable additional verbose/debug logging.
-
.state(if_set: false, env_only: false) ⇒ Object
Show the current state of all Config variables as a dict.
Instance Method Summary collapse
-
#initialize(restore_defaults: false, **options) {|self.class| ... } ⇒ Config
constructor
Initialize a Config object instance for context manager usage.
Constructor Details
#initialize(restore_defaults: false, **options) {|self.class| ... } ⇒ Config
Initialize a Config object instance for context manager usage.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/polars/config.rb', line 27 def initialize(restore_defaults: false, **) @original_state = self.class.save if restore_defaults self.class.restore_defaults end .each do |opt, value| opt = "set_#{opt}" unless opt.to_s.start_with?("set_") if !self.class.respond_to?(opt) raise ArgumentError, "Config has no #{opt} option" end self.class.public_send(opt, value) end yield self.class self.class.restore_defaults.load(@original_state) @original_state = "" end |
Class Method Details
.activate_decimals(active = true) ⇒ Config
Activate Decimal
data types.
This is a temporary setting that will be removed later once the
Decimal
type stabilize. This will happens without it being
considered a breaking change.
Currently, Decimal
types are in an alpha state.
116 117 118 119 120 121 122 123 |
# File 'lib/polars/config.rb', line 116 def self.activate_decimals(active = true) if !active ENV.delete("POLARS_ACTIVATE_DECIMAL") else ENV["POLARS_ACTIVATE_DECIMAL"] = "1" end self end |
.load(cfg) ⇒ Config
Load and set previously saved (or shared) Config options from json/file.
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/polars/config.rb', line 51 def self.load(cfg) = JSON.parse(cfg) ENV.merge!(["environment"]) .fetch("fetch", {}).each do |cfg_methodname, value| if POLARS_CFG_DIRECT_VARS.key?(cfg_methodname) public_send(cfg_methodname, value) end end self end |
.restore_defaults ⇒ Config
Reset all polars Config settings to their default state.
65 66 67 68 69 70 71 |
# File 'lib/polars/config.rb', line 65 def self.restore_defaults POLARS_CFG_ENV_VARS.each do |var| ENV.delete(var) end set_fmt_float self end |
.save ⇒ Config
Save the current set of Config options as a json string or file.
76 77 78 79 80 81 |
# File 'lib/polars/config.rb', line 76 def self.save environment_vars = POLARS_CFG_ENV_VARS.sort.select { |k| ENV.key?(k) }.to_h { |k| [k, ENV[k]] } direct_vars = POLARS_CFG_DIRECT_VARS.to_h { |cfg_methodname, get_value| [cfg_methodname, get_value.call] } = JSON.generate({environment: environment_vars, direct: direct_vars}) end |
.set_ascii_tables(active = true) ⇒ Config
Use ASCII characters to display table outlines (set False to revert to UTF8).
145 146 147 148 149 |
# File 'lib/polars/config.rb', line 145 def self.set_ascii_tables(active = true) fmt = active ? "ASCII_FULL_CONDENSED" : "UTF8_FULL_CONDENSED" ENV["POLARS_FMT_TABLE_FORMATTING"] = fmt self end |
.set_auto_structify(active = true) ⇒ Config
Allow multi-output expressions to be automatically turned into Structs.
154 155 156 157 |
# File 'lib/polars/config.rb', line 154 def self.set_auto_structify(active = true) ENV["POLARS_AUTO_STRUCTIFY"] = active ? "1" : "0" self end |
.set_fmt_float(fmt = "mixed") ⇒ Config
Control how floating point values are displayed.
165 166 167 168 |
# File 'lib/polars/config.rb', line 165 def self.set_fmt_float(fmt = "mixed") Plr.set_float_fmt(fmt) self end |
.set_fmt_str_lengths(n) ⇒ Config
Set the number of characters used to display string values.
199 200 201 202 203 204 205 206 |
# File 'lib/polars/config.rb', line 199 def self.set_fmt_str_lengths(n) if n <= 0 raise ArgumentError, "number of characters must be > 0" end ENV["POLARS_FMT_STR_LEN"] = n.to_s self end |
.set_streaming_chunk_size(size) ⇒ Config
Overwrite chunk size used in streaming
engine.
By default, the chunk size is determined by the schema and size of the thread pool. For some datasets (esp. when you have large string elements) this can be too optimistic and lead to Out of Memory errors.
220 221 222 223 224 225 226 227 |
# File 'lib/polars/config.rb', line 220 def self.set_streaming_chunk_size(size) if size < 1 raise ArgumentError, "number of rows per chunk must be >= 1" end ENV["POLARS_STREAMING_CHUNK_SIZE"] = size.to_s self end |
.set_tbl_cell_alignment(format) ⇒ Config
Set table cell alignment.
256 257 258 259 |
# File 'lib/polars/config.rb', line 256 def self.set_tbl_cell_alignment(format) ENV["POLARS_FMT_TABLE_CELL_ALIGNMENT"] = format self end |
.set_tbl_cols(n) ⇒ Config
Set the number of columns that are visible when displaying tables.
283 284 285 286 |
# File 'lib/polars/config.rb', line 283 def self.set_tbl_cols(n) ENV["POLARS_FMT_MAX_COLS"] = n.to_s self end |
.set_tbl_column_data_type_inline(active = true) ⇒ Config
Moves the data type inline with the column name (to the right, in parentheses).
306 307 308 309 |
# File 'lib/polars/config.rb', line 306 def self.set_tbl_column_data_type_inline(active = true) ENV["POLARS_FMT_TABLE_INLINE_COLUMN_DATA_TYPE"] = active ? "1" : "0" self end |
.set_tbl_dataframe_shape_below(active = true) ⇒ Config
Print the dataframe shape below the dataframe when displaying tables.
331 332 333 334 |
# File 'lib/polars/config.rb', line 331 def self.set_tbl_dataframe_shape_below(active = true) ENV["POLARS_FMT_TABLE_DATAFRAME_SHAPE_BELOW"] = active ? "1" : "0" self end |
.set_tbl_formatting(format = nil, rounded_corners: false) ⇒ Config
The UTF8 styles all use one or more of the semigraphic box-drawing characters found in the Unicode Box Drawing block, which are not ASCII compatible: https://en.wikipedia.org/wiki/Box-drawing_character#Box_Drawing
Set table formatting style.
378 379 380 381 382 383 384 |
# File 'lib/polars/config.rb', line 378 def self.set_tbl_formatting(format = nil, rounded_corners: false) if format ENV["POLARS_FMT_TABLE_FORMATTING"] = format end ENV["POLARS_FMT_TABLE_ROUNDED_CORNERS"] = rounded_corners ? "1" : "0" self end |
.set_tbl_hide_column_data_types(active = true) ⇒ Config
Hide table column data types (i64, f64, str etc.).
404 405 406 407 |
# File 'lib/polars/config.rb', line 404 def self.set_tbl_hide_column_data_types(active = true) ENV["POLARS_FMT_TABLE_HIDE_COLUMN_DATA_TYPES"] = active ? "1" : "0" self end |
.set_tbl_hide_column_names(active = true) ⇒ Config
Hide table column names.
427 428 429 430 |
# File 'lib/polars/config.rb', line 427 def self.set_tbl_hide_column_names(active = true) ENV["POLARS_FMT_TABLE_HIDE_COLUMN_NAMES"] = active ? "1" : "0" self end |
.set_tbl_hide_dataframe_shape(active = true) ⇒ Config
Hide the shape information of the dataframe when displaying tables.
475 476 477 478 |
# File 'lib/polars/config.rb', line 475 def self.set_tbl_hide_dataframe_shape(active = true) ENV["POLARS_FMT_TABLE_HIDE_DATAFRAME_SHAPE_INFORMATION"] = active ? "1" : "0" self end |
.set_tbl_hide_dtype_separator(active = true) ⇒ Config
Hide the '---' separator between the column names and column types.
451 452 453 454 |
# File 'lib/polars/config.rb', line 451 def self.set_tbl_hide_dtype_separator(active = true) ENV["POLARS_FMT_TABLE_HIDE_COLUMN_SEPARATOR"] = active ? "1" : "0" self end |
.set_tbl_rows(n) ⇒ Config
Set the max number of rows used to draw the table (both Dataframe and Series).
506 507 508 509 |
# File 'lib/polars/config.rb', line 506 def self.set_tbl_rows(n) ENV["POLARS_FMT_MAX_ROWS"] = n.to_s self end |
.set_tbl_width_chars(width) ⇒ Config
Set the number of characters used to draw the table.
517 518 519 520 |
# File 'lib/polars/config.rb', line 517 def self.set_tbl_width_chars(width) ENV["POLARS_TABLE_WIDTH"] = width.to_s self end |
.set_verbose(active = true) ⇒ Config
Enable additional verbose/debug logging.
525 526 527 528 |
# File 'lib/polars/config.rb', line 525 def self.set_verbose(active = true) ENV["POLARS_VERBOSE"] = active ? "1" : "0" self end |
.state(if_set: false, env_only: false) ⇒ Object
Show the current state of all Config variables as a dict.
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/polars/config.rb', line 94 def self.state(if_set: false, env_only: false) config_state = POLARS_CFG_ENV_VARS.sort .select { |var| !if_set || !ENV[var].nil? } .to_h { |var| [var, ENV[var]] } if !env_only POLARS_CFG_DIRECT_VARS.each do |cfg_methodname, get_value| config_state[cfg_methodname] = get_value.call end end config_state end |