fat_core 1.5.2 → 1.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4617dd301a8a4f6ea796c27cfff91364f72b79f2
4
- data.tar.gz: e6eb1b4057994c9672712b28189066af2ebc56d8
3
+ metadata.gz: 2d398b7ceb368a1c99d20a78b6b223b7d6d0fee1
4
+ data.tar.gz: 3107bfa35da91b30a2001b56f85daef907ce9789
5
5
  SHA512:
6
- metadata.gz: 478b2f046e8cfc211eaed97bcd20ddf80b9cbb7c38e022931ceaeade08ba91d39f94e4ad085145426b7789a3795648112149cc0e53256bce17c0cc6241fbee2c
7
- data.tar.gz: 32a4dbf0406779b323a76f61319f9553b9442d8a5ebce0c8567720b3134c9894087b6f22d38720d08f0070884f11eb74d8655ec771b51c4ac8f8e47f6442975a
6
+ metadata.gz: 6c982073829b3209c0a03dfb9f16e30b0d6aaa321fc7720de5dac5a9c6bb36db7c9560087a53cee6a4fefa06273f317d8df25e7ac0a90d60efca87ee876e5ab1
7
+ data.tar.gz: 090803a0545f56bb24d7fdda0d27c5c21ff27b9eaa73ef69e3190ac5b74d60a02ffac6a2bf16b649e64547f19edabfa9ce44d4ab6172167e1814de3b272855b1
@@ -183,7 +183,8 @@ module FatCore
183
183
  ## preceding the hline.
184
184
  ##
185
185
  ## The #order_by method resets the boundaries then adds boundaries at the
186
- ## last row of each group as a boundary.
186
+ ## last row of each group of rows on which the sort keys were equal as a
187
+ ## boundary.
187
188
  ##
188
189
  ## The #union_all (but not #union since it deletes duplicates) method adds
189
190
  ## a boundary between the constituent tables. #union_all also preserves any
@@ -195,10 +196,15 @@ module FatCore
195
196
  ## without change, since it only selects columns for the output and deletes
196
197
  ## no rows.
197
198
  ##
199
+ ## Perhaps surprisingly, the #group_by method does /not/ result in any
200
+ ## groups in the output table since the result of #group_by is to reduce
201
+ ## all groups it finds into a single row, and having a group for each row
202
+ ## of the output table would have no use.
203
+ ##
198
204
  ## All the other table-transforming methods reset the boundaries in the new
199
- ## table. For example, #order_by and #where re-arrange and delete rows, so
200
- ## the old boundaries would make no sense anyway. Likewise, #union,
201
- ## #intersection, #except, and #join reset the boundaries to their default.
205
+ ## table. For example, #where re-arranges and deletes rows, so the old
206
+ ## boundaries would make no sense anyway. Likewise, #union, #intersection,
207
+ ## #except, and #join reset the boundaries to their default.
202
208
  ## ###########################################################################
203
209
 
204
210
  public
@@ -251,6 +257,15 @@ module FatCore
251
257
  @boundaries += bounds.map { |k| k + shift }
252
258
  end
253
259
 
260
+ # Return the group number to which row k belongs. Groups, from the user's
261
+ # point of view are indexed starting at 1.
262
+ def row_index_to_group_index(k)
263
+ boundaries.each_with_index do |b_last, g_num|
264
+ return (g_num + 1) if k <= b_last
265
+ end
266
+ 1
267
+ end
268
+
254
269
  def group_rows(k)
255
270
  normalize_boundaries
256
271
  return [] unless k < boundaries.size
@@ -289,6 +304,7 @@ module FatCore
289
304
  new_tab.mark_boundary(k - 1) if last_key && key != last_key
290
305
  last_key = key
291
306
  end
307
+ new_tab.normalize_boundaries
292
308
  new_tab
293
309
  end
294
310
 
@@ -306,8 +322,10 @@ module FatCore
306
322
  # well. The output table preserves any groups present in the input table.
307
323
  def select(*cols, **new_cols)
308
324
  result = Table.new
309
- ev = Evaluator.new(vars: { row: 0 }, before: '@row += 1')
310
- rows.each do |old_row|
325
+ normalize_boundaries
326
+ ev = Evaluator.new(vars: { row: 0, group: 1 },
327
+ before: '@row = __row; @group = __group')
328
+ rows.each_with_index do |old_row, old_k|
311
329
  new_row = {}
312
330
  cols.each do |k|
313
331
  h = k.as_sym
@@ -317,6 +335,8 @@ module FatCore
317
335
  new_cols.each_pair do |key, val|
318
336
  key = key.as_sym
319
337
  vars = old_row.merge(new_row)
338
+ vars[:__row] = old_k + 1
339
+ vars[:__group] = row_index_to_group_index(old_k)
320
340
  case val
321
341
  when Symbol
322
342
  raise "Column '#{val}' in select does not exist" unless vars.keys.include?(val)
@@ -330,6 +350,7 @@ module FatCore
330
350
  result << new_row
331
351
  end
332
352
  result.boundaries = boundaries
353
+ result.normalize_boundaries
333
354
  result
334
355
  end
335
356
 
@@ -338,10 +359,15 @@ module FatCore
338
359
  def where(expr)
339
360
  expr = expr.to_s
340
361
  result = Table.new
341
- ev = Evaluator.new(vars: { row: 0 }, before: '@row += 1')
342
- rows.each do |row|
362
+ ev = Evaluator.new(vars: { row: 0 },
363
+ before: '@row = __row; @group = __group')
364
+ rows.each_with_index do |row, k|
365
+ vars = row
366
+ vars[:__row] = k + 1
367
+ vars[:__group] = row_index_to_group_index(k)
343
368
  result << row if ev.evaluate(expr, vars: row)
344
369
  end
370
+ result.normalize_boundaries
345
371
  result
346
372
  end
347
373
 
@@ -452,6 +478,7 @@ module FatCore
452
478
  other.normalize_boundaries
453
479
  result.append_boundaries(other.boundaries, shift: size)
454
480
  end
481
+ result.normalize_boundaries
455
482
  distinct ? result.distinct : result
456
483
  end
457
484
 
@@ -563,6 +590,7 @@ module FatCore
563
590
  end
564
591
  end
565
592
  end
593
+ result.normalize_boundaries
566
594
  result
567
595
  end
568
596
 
@@ -621,6 +649,7 @@ module FatCore
621
649
  groups.each_pair do |_vals, grp_rows|
622
650
  result << row_from_group(grp_rows, group_cols, agg_cols)
623
651
  end
652
+ result.normalize_boundaries
624
653
  result
625
654
  end
626
655
 
@@ -1,7 +1,7 @@
1
1
  module FatCore
2
2
  MAJOR = 1
3
3
  MINOR = 5
4
- PATCH = 2
4
+ PATCH = 3
5
5
 
6
6
  VERSION = [MAJOR, MINOR, PATCH].compact.join('.')
7
7
  end
@@ -671,6 +671,25 @@ EOS
671
671
  expect(tab2[:s].items).to eq([26450449, 169744, 3316041])
672
672
  expect(tab2[:c].items).to eq([12492, 25648, 7552])
673
673
  end
674
+
675
+ it 'should have access to @row and @group vars in evaluating' do
676
+ aoh = [
677
+ { a: '5', 'Two words' => '20', s: '5_143', c: '3123' },
678
+ { a: '4', 'Two words' => '5', s: 412, c: 6412 },
679
+ { a: '7', 'Two words' => '8', s: '$1721', c: '$1_888' },
680
+ { a: '5', 'Two words' => '20', s: '4_143', c: '4123' },
681
+ { a: '4', 'Two words' => '5', s: 512, c: 5412 },
682
+ { a: '7', 'Two words' => '8', s: '$1621', c: '$2_888' },
683
+ { a: '5', 'Two words' => '20', s: '3_143', c: '5123' },
684
+ { a: '4', 'Two words' => '5', s: 412, c: 4412 },
685
+ { a: '7', 'Two words' => '8', s: '$1521', c: '$3_888' }
686
+ ]
687
+ tab = Table.new(aoh).order_by(:a, :two_words)
688
+ tab2 = tab.select(:a, :two_words, number: '@row', group: '@group')
689
+ expect(tab2.headers).to eq [:a, :two_words, :number, :group]
690
+ expect(tab2[:number].items).to eq([1, 2, 3, 4, 5, 6, 7, 8, 9])
691
+ expect(tab2[:group].items).to eq([1, 1, 1, 2, 2, 2, 3, 3, 3])
692
+ end
674
693
  end
675
694
 
676
695
  describe 'where' do
@@ -680,7 +699,7 @@ EOS
680
699
  expect(tab2[:date].max).to be < Date.parse('2006-06-01')
681
700
  end
682
701
 
683
- it 'should select by boolean columns' do
702
+ it 'should where by boolean columns' do
684
703
  tab =
685
704
  [['Ref', 'Date', 'Code', 'Raw', 'Shares', 'Price', 'Info', 'Bool'],
686
705
  nil,
@@ -708,6 +727,29 @@ EOS
708
727
  tab2 = tab.where('info =~ /xxxx/')
709
728
  expect(tab2.rows.size).to eq(0)
710
729
  end
730
+
731
+ it 'where select by row and group' do
732
+ tab =
733
+ [['Ref', 'Date', 'Code', 'Raw', 'Shares', 'Price', 'Info', 'Bool'],
734
+ nil,
735
+ [1, '2013-05-02', 'P', 795_546.20, 795_546.2, 1.1850, 'ZMPEF1', 'T'],
736
+ [2, '2013-05-02', 'P', 118_186.40, 118_186.4, 11.8500, 'ZMPEF1', 'T'],
737
+ [7, '2013-05-20', 'S', 12_000.00, 5046.00, 28.2804, 'ZMEAC', 'F'],
738
+ [8, '2013-05-20', 'S', 85_000.00, 35_742.50, 28.3224, 'ZMEAC', 'T'],
739
+ [9, '2013-05-20', 'S', 33_302.00, 14_003.49, 28.6383, 'ZMEAC', 'T'],
740
+ [10, '2013-05-23', 'S', 8000.00, 3364.00, 27.1083, 'ZMEAC', 'T'],
741
+ [11, '2013-05-23', 'S', 23_054.00, 9694.21, 26.8015, 'ZMEAC', 'F'],
742
+ [12, '2013-05-23', 'S', 39_906.00, 16_780.47, 25.1749, 'ZMEAC', 'T'],
743
+ [13, '2013-05-29', 'S', 13_459.00, 5659.51, 24.7464, 'ZMEAC', 'T'],
744
+ [14, '2013-05-29', 'S', 15_700.00, 6601.85, 24.7790, 'ZMEAC', 'F'],
745
+ [15, '2013-05-29', 'S', 15_900.00, 6685.95, 24.5802, 'ZMEAC', 'T'],
746
+ [16, '2013-05-30', 'S', 6_679.00, 2808.52, 25.0471, 'ZMEAC', 'T']]
747
+ tab = Table.new(tab).order_by(:date, :code)
748
+ tab2 = tab.where('@row > 10')
749
+ expect(tab2.rows.size).to eq(2)
750
+ tab2 = tab.where('@group == 3')
751
+ expect(tab2.rows.size).to eq(3)
752
+ end
711
753
  end
712
754
 
713
755
  describe 'group_by' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fat_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.2
4
+ version: 1.5.3
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: 2017-03-07 00:00:00.000000000 Z
11
+ date: 2017-03-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: simplecov