fat_table 0.8.0 → 0.9.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/fat_table/column.rb +2 -0
- data/lib/fat_table/table.rb +50 -33
- data/lib/fat_table/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0b13c85cc10ea33c6013969fb277fedfe2dabd7e195e7b71b0e43623d40d53e1
|
4
|
+
data.tar.gz: 73b16003325654444f258233abeaa96bb5aa9401441e2993a0e5d80e4df4ead8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 27b7bd5a2ab7344e16b64e3b8e66455de579a1145a40a0551f3c28be5836b439485d66a6ea2ad230f44c8f64d6fabc0014e894526539deeced85703eb7d54334
|
7
|
+
data.tar.gz: 9d6b8f4eb9c9f399963edcfd99a8b31a2b31cc9bae7a2748c72459052b54047c853600452f41934b0466cfc9e375e084da017a55153ac2cdf3571da95e410668
|
data/lib/fat_table/column.rb
CHANGED
data/lib/fat_table/table.rb
CHANGED
@@ -53,6 +53,7 @@ module FatTable
|
|
53
53
|
class Table
|
54
54
|
# An Array of FatTable::Columns that constitute the table.
|
55
55
|
attr_reader :columns
|
56
|
+
attr_reader :heads
|
56
57
|
|
57
58
|
# Headers of columns that are to be tolerant when they are built.
|
58
59
|
attr_accessor :tolerant_cols
|
@@ -95,9 +96,10 @@ module FatTable
|
|
95
96
|
# starts with 'num', 'dat', 'bool', or 'str' to specify Numeric,
|
96
97
|
# DateTime, Boolean, or String types respectively.
|
97
98
|
def initialize(*heads, **types)
|
99
|
+
@heads = heads.flatten.map(&:as_sym)
|
100
|
+
@types = types
|
98
101
|
@columns = []
|
99
102
|
@tolerant_cols = []
|
100
|
-
@headers = []
|
101
103
|
# Check for the special 'omni' key
|
102
104
|
@omni_type = 'NilClass'
|
103
105
|
@omni_tol = false
|
@@ -110,9 +112,14 @@ module FatTable
|
|
110
112
|
types.delete(:omni)
|
111
113
|
types.delete('omni')
|
112
114
|
end
|
113
|
-
heads += types.keys
|
114
|
-
heads.uniq.each do |h|
|
115
|
-
|
115
|
+
# heads += types.keys
|
116
|
+
(heads.flatten + types.keys).uniq.each do |h|
|
117
|
+
if types[h]
|
118
|
+
typ, tol = Table.typ_tol(types[h])
|
119
|
+
else
|
120
|
+
typ = @omni_type
|
121
|
+
tol = @omni_tol
|
122
|
+
end
|
116
123
|
@tolerant_cols << h.to_s.as_sym if tol
|
117
124
|
@columns << Column.new(header: h.to_s.sub(/~\s*\z/, ''), type: typ,
|
118
125
|
tolerant: tol)
|
@@ -122,18 +129,23 @@ module FatTable
|
|
122
129
|
|
123
130
|
# :category: Constructors
|
124
131
|
|
125
|
-
# Return an
|
126
|
-
#
|
127
|
-
#
|
128
|
-
#
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
132
|
+
# Return an new table based on this Table but with empty columns named by
|
133
|
+
# the result_cols parameter, by default the this Table's columns. If any
|
134
|
+
# of the result_cols have the same name as an existing column, inherit
|
135
|
+
# that column's type and tolerance. Also, set any instance variables that
|
136
|
+
# might have been set by a subclass instance.
|
137
|
+
def empty_dup(result_cols = nil)
|
138
|
+
result_cols ||= heads
|
139
|
+
result_types = types.select { |k,_v| result_cols.include?(k) }
|
140
|
+
result = Table.new(result_cols, **result_types)
|
141
|
+
tolerant_cols.each do |h|
|
142
|
+
result.tolerant_cols << h
|
143
|
+
result.column(h).tolerant = true
|
144
|
+
end
|
145
|
+
(instance_variables - result.instance_variables).each do |v|
|
146
|
+
result.instance_variable_set(v, instance_variable_get(v))
|
147
|
+
end
|
148
|
+
result
|
137
149
|
end
|
138
150
|
|
139
151
|
# :category: Constructors
|
@@ -264,7 +276,8 @@ module FatTable
|
|
264
276
|
# can respond to #to_h. If an array element is a nil, mark it as a group
|
265
277
|
# boundary in the Table.
|
266
278
|
def from_array_of_hashes(hashes, hlines: false, **types)
|
267
|
-
|
279
|
+
heads = hashes.first.keys
|
280
|
+
result = new(*heads, **types)
|
268
281
|
hashes.each do |hsh|
|
269
282
|
if hsh.nil?
|
270
283
|
unless hlines
|
@@ -293,7 +306,6 @@ module FatTable
|
|
293
306
|
# indicated with nil elements in the outer array as expected by this
|
294
307
|
# method when hlines is set true.
|
295
308
|
def from_array_of_arrays(rows, hlines: false, **types)
|
296
|
-
result = new(**types)
|
297
309
|
headers = []
|
298
310
|
if !hlines
|
299
311
|
# Take the first row as headers
|
@@ -312,6 +324,7 @@ module FatTable
|
|
312
324
|
headers = (1..rows[0].size).to_a.map { |k| "col_#{k}".as_sym }
|
313
325
|
first_data_row = 0
|
314
326
|
end
|
327
|
+
result = new(*headers, **types)
|
315
328
|
rows[first_data_row..-1].each do |row|
|
316
329
|
if row.nil?
|
317
330
|
unless hlines
|
@@ -419,6 +432,7 @@ module FatTable
|
|
419
432
|
def force_string!(*keys)
|
420
433
|
keys.each do |h|
|
421
434
|
raise UserError, "force_string!: #{h} not a column in table" unless column(h)
|
435
|
+
|
422
436
|
column(h).force_string!
|
423
437
|
end
|
424
438
|
self
|
@@ -845,6 +859,7 @@ module FatTable
|
|
845
859
|
unless expr.is_a?(String)
|
846
860
|
raise "must call FatTable::Table\#order_with with a single string expression"
|
847
861
|
end
|
862
|
+
|
848
863
|
rev = false
|
849
864
|
if expr.match?(/\s*!\s*\z/)
|
850
865
|
rev = true
|
@@ -957,8 +972,15 @@ module FatTable
|
|
957
972
|
before: before_hook,
|
958
973
|
after: after_hook)
|
959
974
|
# Compute the new Table from this Table
|
960
|
-
|
975
|
+
result_cols =
|
976
|
+
if cols.include?(:omni)
|
977
|
+
(headers + new_cols.keys - [:omni])
|
978
|
+
else
|
979
|
+
(cols + new_cols.keys)
|
980
|
+
end
|
981
|
+
result = empty_dup(result_cols)
|
961
982
|
normalize_boundaries
|
983
|
+
|
962
984
|
rows.each_with_index do |old_row, old_k|
|
963
985
|
# Set the group number in the before hook and run the hook with the
|
964
986
|
# local variables set to the row before the new row is evaluated.
|
@@ -1028,15 +1050,6 @@ module FatTable
|
|
1028
1050
|
def where(expr)
|
1029
1051
|
expr = expr.to_s
|
1030
1052
|
result = empty_dup
|
1031
|
-
headers.each do |h|
|
1032
|
-
col =
|
1033
|
-
if tolerant_col?(h)
|
1034
|
-
Column.new(header: h, tolerant: true)
|
1035
|
-
else
|
1036
|
-
Column.new(header: h)
|
1037
|
-
end
|
1038
|
-
result.add_column(col)
|
1039
|
-
end
|
1040
1053
|
ev = Evaluator.new(ivars: { row: 0, group: 0 })
|
1041
1054
|
rows.each_with_index do |row, k|
|
1042
1055
|
grp = row_index_to_group_index(k)
|
@@ -1094,10 +1107,13 @@ module FatTable
|
|
1094
1107
|
# boundaries of the constituent tables. Preserves and adjusts the group
|
1095
1108
|
# boundaries of the constituent table.
|
1096
1109
|
def union_all(other)
|
1097
|
-
set_operation(
|
1098
|
-
|
1099
|
-
|
1100
|
-
|
1110
|
+
set_operation(
|
1111
|
+
other,
|
1112
|
+
:+,
|
1113
|
+
distinct: false,
|
1114
|
+
add_boundaries: true,
|
1115
|
+
inherit_boundaries: true
|
1116
|
+
)
|
1101
1117
|
end
|
1102
1118
|
|
1103
1119
|
# :category: Operators
|
@@ -1489,7 +1505,8 @@ module FatTable
|
|
1489
1505
|
groups = sorted_tab.rows.group_by do |r|
|
1490
1506
|
group_cols.map { |k| r[k] }
|
1491
1507
|
end
|
1492
|
-
|
1508
|
+
grp_types = types.select { |k, _v| group_cols.include?(k) }
|
1509
|
+
result = Table.new(*group_cols, **grp_types)
|
1493
1510
|
groups.each_pair do |_vals, grp_rows|
|
1494
1511
|
result << row_from_group(grp_rows, group_cols, agg_cols)
|
1495
1512
|
end
|
data/lib/fat_table/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fat_table
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.9.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel E. Doherty
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-05-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|