Skip to content

Commit

Permalink
Add support for options to Daru module (#506)
Browse files Browse the repository at this point in the history
* Add support for options to Daru module

#476

#502

* Use the max_rows option on the terminal

* fix typo in table formatter
  • Loading branch information
kojix2 authored May 30, 2020
1 parent ba5992c commit 75a162b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
2 changes: 2 additions & 0 deletions lib/daru.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ def jruby?
# :nocov:

module Daru

DAYS_OF_WEEK = {
'SUN' => 0,
'MON' => 1,
Expand Down Expand Up @@ -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'
Expand Down
36 changes: 36 additions & 0 deletions lib/daru/configuration.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 6 additions & 3 deletions lib/daru/dataframe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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__)
Expand Down Expand Up @@ -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" +
Expand Down
7 changes: 2 additions & 5 deletions lib/daru/formatters/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 75a162b

Please sign in to comment.