dsv 0.16.1 → 0.16.2

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
  SHA256:
3
- metadata.gz: 998074c09bc9f636ccd0c0c7b6d73d78ceeda4a0efccafedcbc26c72cbec947d
4
- data.tar.gz: 28549e92a6760f7c7ac536d1a12c886aeb7f17c920e26f4b8454e9cb5a379727
3
+ metadata.gz: f51c0809df8ce80d534a86623f7936def15b4dc005fc3fa02a6df1a3a4b26f32
4
+ data.tar.gz: 29007e890ebe7230c885fcf09238ba2da51e59c948d90ab41cf406c88e61fa3d
5
5
  SHA512:
6
- metadata.gz: b91d4255ea53e6a699e5f04d69d10b2cbdd5c07439c93fa755ce9c088446f9f329a419b0fd1d4a746e7582609264ede1b4ed9a8f5f6816b6115069fc13b9fa90
7
- data.tar.gz: 87e13881fcad93d7dd7558e985ae7ef719f2f104a2eb4651192cc3a17ba9c27b99146def74b24e2ca6e3352d35ccee20507db99e9f904fc0896b078d8c01fb7a
6
+ metadata.gz: a50224c24dbe0e7e8d9f5dd03024b6d13f7d62d5067ae30be8d899931ccc44e8c9f13604f159c1b608c29b91d9ff833f9bbb7d27c616b7984bb5332308b5e381
7
+ data.tar.gz: 9d153d7278b130b0c8afb48d84ab791b65a2415ba2e881f8ed472540bc9db2143d92705492c60dee1a0afe5e1c9e44ab606717ad64e82820fa038c313b342cf6
data/CHANGELOG CHANGED
@@ -1,5 +1,18 @@
1
1
  # dsv/CHANGELOG
2
2
 
3
+ ## 20261127
4
+
5
+ 0.16.2: ~ DSV#read, ~ DSV#write: The columns selected are flattened once, not for every row.
6
+
7
+ 1. ~ parse_fields(): The columns selected are no longer flattened. Since 0.12.12 they had been, for every row read, at three objects a row where they came as an Array or from each.
8
+ 2. ~ read(), parse_row(): The columns selected are flattened once.
9
+ 3. ~ write(): The columns selected are flattened once, so as write_row is given them flat.
10
+ 4. ~ each(): read_csv is given *selected_columns, so as nothing is passed where nothing is selected.
11
+ 5. ~ test/dsv_test.rb: read and write are tested to give each row the columns selected flat, however they were given, and each to pass read_csv the columns selected.
12
+ 6. ~ TODO: The columns selected in the order selected is a job, under Column names in one place, and the jobs after it are renumbered.
13
+ 7. ~ ROADMAP.md: 0.19.0 gives the columns selected in the order selected.
14
+ 8. ~ DSV::VERSION: /0.16.1/0.16.2/
15
+
3
16
  ## 20261126
4
17
 
5
18
  0.16.1: ~ DSV.open: A file not yet there is written by name under w, w+, a and a+.
data/TODO CHANGED
@@ -14,41 +14,42 @@
14
14
  ## Column names in one place
15
15
 
16
16
  6. attributes= setting columns, attributes always worked out from columns; rows read afterwards keyed by those names.
17
+ 7. The columns selected in the order selected, in Hash rows and under as_array:, from read and each alike; positions no longer sorted.
17
18
 
18
19
  ## Path or text
19
20
 
20
- 7. .read and .open taking a path, and .parse text; a path which does not exist raising, and nothing inferred from whether a file exists.
21
+ 8. .read and .open taking a path, and .parse text; a path which does not exist raising, and nothing inferred from whether a file exists.
21
22
 
22
23
  ## Streaming
23
24
 
24
- 8. each yields each row as it is read.
25
- 9. each returns its receiver.
26
- 10. rows no longer filled by each.
27
- 11. Chunks from each_slice.
28
- 12. The source read once, start to end, with no rewind; the header from the first row read.
29
- 13. The line of an error found by seeking only where the source can.
30
- 14. A rewind left to r+ rewriting and to first_row called alone.
25
+ 9. each yields each row as it is read.
26
+ 10. each returns its receiver.
27
+ 11. rows no longer filled by each.
28
+ 12. Chunks from each_slice.
29
+ 13. The source read once, start to end, with no rewind; the header from the first row read.
30
+ 14. The line of an error found by seeking only where the source can.
31
+ 15. A rewind left to r+ rewriting and to first_row called alone.
31
32
 
32
33
  ## Checked and measured
33
34
 
34
- 15. A corpus in the tests, a day of NEMWEB's dispatch files, trimmed, and the RFC 4180 cases, each checked against stdlib CSV's rows.
35
- 16. A benchmark script against stdlib CSV, and the README's speed sentence written from its output.
36
- 17. The README showing every mode by example, and a table matching each to stdlib CSV's and Python's settings.
35
+ 16. A corpus in the tests, a day of NEMWEB's dispatch files, trimmed, and the RFC 4180 cases, each checked against stdlib CSV's rows.
36
+ 17. A benchmark script against stdlib CSV, and the README's speed sentence written from its output.
37
+ 18. The README showing every mode by example, and a table matching each to stdlib CSV's and Python's settings.
37
38
 
38
39
  ## Speed, results unchanged
39
40
 
40
- 18. A per-row fast path for the default: split, the fields bare or wrapped in two quotes unwrapped, any other row read in full.
41
- 19. as_array: returning the split fields, rather than building a Hash and then an Array from it.
42
- 20. Rows built by zip.to_h.
43
- 21. The scanner-based parser, at the tag scanner-parser-20261006, judged against the string operations used now.
41
+ 19. A per-row fast path for the default: split, the fields bare or wrapped in two quotes unwrapped, any other row read in full.
42
+ 20. as_array: returning the split fields, rather than building a Hash and then an Array from it.
43
+ 21. Rows built by zip.to_h.
44
+ 22. The scanner-based parser, at the tag scanner-parser-20261006, judged against the string operations used now.
44
45
 
45
46
  ## Features
46
47
 
47
- 22. :rfc_4180, a quote mode reading what RFC 4180 allows as :unspecified does and raising on what it forbids.
48
- 23. field_count:, :header or an Integer, off by default; a row of another width raising.
49
- 24. Writing with minimal quoting: a field quoted only where it holds a separator, a quote or a row terminator.
50
- 25. A line number in each parse error, and line_numbers:.
51
- 26. headers?, the instance-level header_row renamed, header_row kept as an alias.
52
- 27. <<, to append a row, and generate, to build a string.
53
- 28. Symbols and Strings interchangeable for column names, in a selection, a columns: list and a row's keys.
54
- 29. Duplicate headers by option: all kept as an Array, the default; indexed, a, a.1; first; last; error.
48
+ 23. :rfc_4180, a quote mode reading what RFC 4180 allows as :unspecified does and raising on what it forbids.
49
+ 24. field_count:, :header or an Integer, off by default; a row of another width raising.
50
+ 25. Writing with minimal quoting: a field quoted only where it holds a separator, a quote or a row terminator.
51
+ 26. A line number in each parse error, and line_numbers:.
52
+ 27. headers?, the instance-level header_row renamed, header_row kept as an alias.
53
+ 28. <<, to append a row, and generate, to build a string.
54
+ 29. Symbols and Strings interchangeable for column names, in a selection, a columns: list and a row's keys.
55
+ 30. Duplicate headers by option: all kept as an Array, the default; indexed, a, a.1; first; last; error.
data/lib/DSV/VERSION.rb CHANGED
@@ -2,5 +2,5 @@
2
2
  # DSV::VERSION
3
3
 
4
4
  class DSV
5
- VERSION = '0.16.1'
5
+ VERSION = '0.16.2'
6
6
  end
data/lib/dsv.rb CHANGED
@@ -152,7 +152,7 @@ class DSV
152
152
  end
153
153
 
154
154
  def read(*selected_columns, &block)
155
- selected_columns = selection(selected_columns)
155
+ selected_columns = selection(selected_columns).flatten
156
156
  if block
157
157
  parse(*selected_columns, &block)
158
158
  else
@@ -220,7 +220,7 @@ class DSV
220
220
  end
221
221
 
222
222
  def parse_row(raw_row, *selected_columns)
223
- parse_fields(split_row(raw_row) || raise(UnterminatedQuote.new(1)), *selected_columns)
223
+ parse_fields(split_row(raw_row) || raise(UnterminatedQuote.new(1)), *selected_columns.flatten)
224
224
  rescue UnexpectedRow => error
225
225
  error.line = 1
226
226
  raise
@@ -228,6 +228,7 @@ class DSV
228
228
 
229
229
  # With no columns defined, rows keyed by name supply them, and the header, from the first row's keys, a name whose value is an Array once for each of its values; rows keyed by position write positionally and have no header to write.
230
230
  def write(*selected_columns)
231
+ selected_columns.flatten!
231
232
  prepare_to_rewrite if @mode == 'r+'
232
233
  self.columns = @rows[0].flat_map{|name, value| value.is_a?(::Array) ? ::Array.new(value.size, name) : name} if blank?(columns) && @rows[0].is_a?(::Hash) && @rows[0].keys[0].is_a?(::String)
233
234
  write_header(*selected_columns) if header_row? && attributes && writes_header?
@@ -274,9 +275,9 @@ class DSV
274
275
  end
275
276
  else
276
277
  if selected_columns.empty? || @as_array
277
- read_csv(selected_columns).each{|row| yield row}
278
+ read_csv(*selected_columns).each{|row| yield row}
278
279
  else
279
- read_csv(selected_columns).each do |row|
280
+ read_csv(*selected_columns).each do |row|
280
281
  yield selected_columns.inject({}){|hash, column_name| hash[column_name] = row[column_name]; hash}
281
282
  end
282
283
  end
@@ -542,7 +543,6 @@ class DSV
542
543
  end
543
544
 
544
545
  def parse_fields(fields, *selected_columns)
545
- selected_columns.flatten!
546
546
  return fields.values_at(*positions(selected_columns)) if @as_array && !selected_columns.empty?
547
547
  parsed_row = {}
548
548
  i = -1
data/test/dsv_test.rb CHANGED
@@ -423,6 +423,16 @@ describe DSV do
423
423
  _(DSV.new(DATA, headers: true).read(['a', 'c'])).must_equal [{'a' => '1', 'c' => '3'}, {'a' => '4', 'c' => '6'}]
424
424
  end
425
425
 
426
+ it "gives each row the columns selected as one list, however they were given" do
427
+ [['a', 'c'], [['a', 'c']]].each do |selection|
428
+ csv = DSV.new(DATA, headers: true)
429
+ original = csv.method(:parse_fields)
430
+ received = []
431
+ csv.stub(:parse_fields, ->(fields, *columns){received << columns; original.call(fields, *columns)}){csv.read(*selection)}
432
+ _(received).must_equal [['a', 'c'], ['a', 'c']]
433
+ end
434
+ end
435
+
426
436
  it "selects columns by position, keyed by position even under a header row" do
427
437
  _(DSV.new("1,2,3\n4,5,6\n").read(0, 2)).must_equal [{0 => '1', 2 => '3'}, {0 => '4', 2 => '6'}]
428
438
  _(DSV.new(DATA, headers: true).read(0, 2)).must_equal [{0 => '1', 2 => '3'}, {0 => '4', 2 => '6'}]
@@ -482,6 +492,18 @@ describe DSV do
482
492
  _(DSV.new(DATA, headers: true).each.to_a).must_equal KEYED
483
493
  _(DSV.new(DATA, headers: true).each('a').to_a).must_equal [{'a' => '1'}, {'a' => '4'}]
484
494
  end
495
+
496
+ it "reads the source with the columns selected, not an Array of them" do
497
+ [true, false].each do |as_array|
498
+ [[], ['a'], ['a', 'c']].each do |selection|
499
+ csv = DSV.new(DATA, headers: true, as_array: as_array)
500
+ original = csv.method(:read_csv)
501
+ received = nil
502
+ csv.stub(:read_csv, ->(*arguments){received = arguments; original.call(*arguments)}){csv.each(*selection){}}
503
+ _(received).must_equal selection
504
+ end
505
+ end
506
+ end
485
507
  end
486
508
 
487
509
  describe "#to_a" do
@@ -781,6 +803,18 @@ describe DSV do
781
803
  _(output).must_equal "\"1\",\"3\"\n"
782
804
  end
783
805
 
806
+ it "gives each row the columns selected as one list, however they were given" do
807
+ [['a', 'c'], [['a', 'c']]].each do |selection|
808
+ received = []
809
+ written(columns: [:a, :b, :c]) do |csv|
810
+ csv.rows = [{'a' => 1, 'b' => 2, 'c' => 3}, {'a' => 4, 'b' => 5, 'c' => 6}]
811
+ original = csv.method(:write_row)
812
+ csv.stub(:write_row, ->(row, *columns){received << columns; original.call(row, *columns)}){csv.write(*selection)}
813
+ end
814
+ _(received).must_equal [['a', 'c'], ['a', 'c']]
815
+ end
816
+ end
817
+
784
818
  it "writes what it read back, so a source round-trips" do
785
819
  output = written(headers: true, columns: [:a, :b]){|csv| csv.rows = DSV.read("a,b\n1,2\n", headers: true); csv.write}
786
820
  _(DSV.read(output, headers: true)).must_equal [{'a' => '1', 'b' => '2'}]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dsv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.16.1
4
+ version: 0.16.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - thoran