From 75a162b90ac3bdc5ee94c1d83acb37e887f3b2aa Mon Sep 17 00:00:00 2001 From: kojix2 <2xijok@gmail.com> Date: Sat, 30 May 2020 12:58:33 +0900 Subject: [PATCH] Add support for options to Daru module (#506) * Add support for options to Daru module #476 #502 * Use the max_rows option on the terminal * fix typo in table formatter --- lib/daru.rb | 2 ++ lib/daru/configuration.rb | 36 ++++++++++++++++++++++++++++++++++++ lib/daru/dataframe.rb | 9 ++++++--- lib/daru/formatters/table.rb | 7 ++----- 4 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 lib/daru/configuration.rb diff --git a/lib/daru.rb b/lib/daru.rb index 1cd3273a5..6961d7531 100644 --- a/lib/daru.rb +++ b/lib/daru.rb @@ -5,6 +5,7 @@ def jruby? # :nocov: module Daru + DAYS_OF_WEEK = { 'SUN' => 0, 'MON' => 1, @@ -102,6 +103,7 @@ def error msg require 'daru/index/categorical_index.rb' require 'daru/helpers/array.rb' +require 'daru/configuration.rb' require 'daru/vector.rb' require 'daru/dataframe.rb' require 'daru/monkeys.rb' diff --git a/lib/daru/configuration.rb b/lib/daru/configuration.rb new file mode 100644 index 000000000..d3b618c3f --- /dev/null +++ b/lib/daru/configuration.rb @@ -0,0 +1,36 @@ +module Daru + # Defines constants and methods related to configuration + module Configuration + + INSPECT_OPTIONS_KEYS = [ + :max_rows, + + # Terminal + :spacing, + ] + + # Jupyter + DEFAULT_MAX_ROWS = 30 + + # Terminal + DEFAULT_SPACING = 10 + + attr_accessor(*INSPECT_OPTIONS_KEYS) + + def configure + yield self + end + + def self.extended(base) + base.reset_options + end + + def reset_options + self.max_rows = DEFAULT_MAX_ROWS + + self.spacing = DEFAULT_SPACING + end + end + + extend Configuration +end diff --git a/lib/daru/dataframe.rb b/lib/daru/dataframe.rb index beda5bc8a..1e2ea5b76 100644 --- a/lib/daru/dataframe.rb +++ b/lib/daru/dataframe.rb @@ -12,6 +12,8 @@ class DataFrame # rubocop:disable Metrics/ClassLength # TODO: Remove this line but its causing erros due to unkown reason Daru.has_nyaplot? + attr_accessor(*Configuration::INSPECT_OPTIONS_KEYS) + extend Gem::Deprecate class << self @@ -2120,7 +2122,7 @@ def to_h end # Convert to html for IRuby. - def to_html(threshold=30) + def to_html(threshold = Daru.max_rows) table_thead = to_html_thead table_tbody = to_html_tbody(threshold) path = if index.is_a?(MultiIndex) @@ -2141,7 +2143,8 @@ def to_html_thead ERB.new(File.read(table_thead_path).strip).result(binding) end - def to_html_tbody(threshold=30) + def to_html_tbody(threshold = Daru.max_rows) + threshold ||= @size table_tbody_path = if index.is_a?(MultiIndex) File.expand_path('../iruby/templates/dataframe_mi_tbody.html.erb', __FILE__) @@ -2258,7 +2261,7 @@ def transpose end # Pretty print in a nice table format for the command line (irb/pry/iruby) - def inspect spacing=10, threshold=15 + def inspect spacing=Daru.spacing, threshold=Daru.max_rows name_part = @name ? ": #{@name} " : '' "#<#{self.class}#{name_part}(#{nrows}x#{ncols})>\n" + diff --git a/lib/daru/formatters/table.rb b/lib/daru/formatters/table.rb index 72366ddc0..e277f3a14 100644 --- a/lib/daru/formatters/table.rb +++ b/lib/daru/formatters/table.rb @@ -13,13 +13,10 @@ def initialize(data, headers, row_headers) @row_headers = [''] * @data.to_a.size if @row_headers.empty? end - DEFAULT_SPACING = 10 - DEFAULT_THRESHOLD = 15 - def format threshold=nil, spacing=nil - rows = build_rows(threshold || DEFAULT_THRESHOLD) + rows = build_rows(threshold || Daru.max_rows) - formatter = construct_formatter rows, spacing || DEFAULT_SPACING + formatter = construct_formatter rows, spacing || Daru.spacing rows.map { |r| formatter % r }.join("\n") end