dsv 0.16.1 → 0.16.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
  SHA256:
3
- metadata.gz: 998074c09bc9f636ccd0c0c7b6d73d78ceeda4a0efccafedcbc26c72cbec947d
4
- data.tar.gz: 28549e92a6760f7c7ac536d1a12c886aeb7f17c920e26f4b8454e9cb5a379727
3
+ metadata.gz: 954b11c8db584823303128861f9bd63437970544ecd2a517f48496ca2693491c
4
+ data.tar.gz: bea25558be05247647f04723c323ce94a564009a4899443ef16eca9c770b67cf
5
5
  SHA512:
6
- metadata.gz: b91d4255ea53e6a699e5f04d69d10b2cbdd5c07439c93fa755ce9c088446f9f329a419b0fd1d4a746e7582609264ede1b4ed9a8f5f6816b6115069fc13b9fa90
7
- data.tar.gz: 87e13881fcad93d7dd7558e985ae7ef719f2f104a2eb4651192cc3a17ba9c27b99146def74b24e2ca6e3352d35ccee20507db99e9f904fc0896b078d8c01fb7a
6
+ metadata.gz: 599452dcb3e0330e041a4cc0ce015245ee2dc8cfa7ea71931f3cf326b079009ae94f3b3e107853704b8645e4260eec073bad19f70033b1913a2bb5b7186c2fe1
7
+ data.tar.gz: ca4f6bf54c866aae5532467dce0072fe25ac8908c591238e9e702f27392ebe36358bf26dca5b59d9c86fa5f4809d532bef1dbaef325ce2d1e7598c4f19bbb4e9
data/CHANGELOG CHANGED
@@ -1,5 +1,27 @@
1
1
  # dsv/CHANGELOG
2
2
 
3
+ ## 20261128
4
+
5
+ 0.16.3: ~ DSV: Its String literals are frozen, so as reading makes half as many objects.
6
+
7
+ 1. ~ DSV, DSV::File, DSV::String, DSV::Error, DSV::UnexpectedRow, DSV::UnquotedField, DSV::UnterminatedQuote, DSV::VERSION: Each file declares frozen_string_literal: true. A literal had been a new String each time it was reached, which in reading was several times for every quoted field.
8
+ 2. ~ test/dsv_test.rb: The default row separator and mode are tested to be frozen, and quoted fields to be read without a new copy of the quote character each time it is used.
9
+ 3. ~ test/helper.rb: + require 'objspace'
10
+ 4. ~ DSV::VERSION: /0.16.2/0.16.3/
11
+
12
+ ## 20261127
13
+
14
+ 0.16.2: ~ DSV#read, ~ DSV#write: The columns selected are flattened once, not for every row.
15
+
16
+ 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.
17
+ 2. ~ read(), parse_row(): The columns selected are flattened once.
18
+ 3. ~ write(): The columns selected are flattened once, so as write_row is given them flat.
19
+ 4. ~ each(): read_csv is given *selected_columns, so as nothing is passed where nothing is selected.
20
+ 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.
21
+ 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.
22
+ 7. ~ ROADMAP.md: 0.19.0 gives the columns selected in the order selected.
23
+ 8. ~ DSV::VERSION: /0.16.1/0.16.2/
24
+
3
25
  ## 20261126
4
26
 
5
27
  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/Error.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  # DSV/Error.rb
2
2
  # DSV::Error
3
3
 
4
+ # frozen_string_literal: true
5
+
4
6
  class DSV
5
7
  # The base of what dsv raises about the data, so that one rescue catches all of it.
6
8
  class Error < ::StandardError
data/lib/DSV/File.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  # DSV/File.rb
2
2
  # DSV::File
3
3
 
4
+ # frozen_string_literal: true
5
+
4
6
  require_relative '../dsv'
5
7
 
6
8
  class DSV
data/lib/DSV/String.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  # DSV/String.rb
2
2
  # DSV::String
3
3
 
4
+ # frozen_string_literal: true
5
+
4
6
  require_relative '../dsv'
5
7
 
6
8
  class DSV
@@ -1,6 +1,8 @@
1
1
  # DSV/UnexpectedRow.rb
2
2
  # DSV::UnexpectedRow
3
3
 
4
+ # frozen_string_literal: true
5
+
4
6
  class DSV
5
7
  # A row which contradicts its declared quote mode; mode is the mode in force, and line the one on which the row began, given by the reader once it knows it.
6
8
  class UnexpectedRow < Error
@@ -1,6 +1,8 @@
1
1
  # DSV/UnquotedField.rb
2
2
  # DSV::UnquotedField
3
3
 
4
+ # frozen_string_literal: true
5
+
4
6
  class DSV
5
7
  # A field with no quotes, under a quote mode which declares every field quoted.
6
8
  class UnquotedField < UnexpectedRow
@@ -1,6 +1,8 @@
1
1
  # DSV/UnterminatedQuote.rb
2
2
  # DSV::UnterminatedQuote
3
3
 
4
+ # frozen_string_literal: true
5
+
4
6
  class DSV
5
7
  # A quoted field which does not close by the end of the source; line is the one on which its row began.
6
8
  class UnterminatedQuote < Error
data/lib/DSV/VERSION.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  # DSV/VERSION.rb
2
2
  # DSV::VERSION
3
3
 
4
+ # frozen_string_literal: true
5
+
4
6
  class DSV
5
- VERSION = '0.16.1'
7
+ VERSION = '0.16.3'
6
8
  end
data/lib/dsv.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  # dsv.rb
2
2
  # DSV
3
3
 
4
+ # frozen_string_literal: true
5
+
4
6
  # Description: Delimiter-separated values: reading and writing CSV and its relatives, tabulated data to and from files and strings, with any delimiter on either side.
5
7
 
6
8
  require 'stringio'
@@ -152,7 +154,7 @@ class DSV
152
154
  end
153
155
 
154
156
  def read(*selected_columns, &block)
155
- selected_columns = selection(selected_columns)
157
+ selected_columns = selection(selected_columns).flatten
156
158
  if block
157
159
  parse(*selected_columns, &block)
158
160
  else
@@ -220,7 +222,7 @@ class DSV
220
222
  end
221
223
 
222
224
  def parse_row(raw_row, *selected_columns)
223
- parse_fields(split_row(raw_row) || raise(UnterminatedQuote.new(1)), *selected_columns)
225
+ parse_fields(split_row(raw_row) || raise(UnterminatedQuote.new(1)), *selected_columns.flatten)
224
226
  rescue UnexpectedRow => error
225
227
  error.line = 1
226
228
  raise
@@ -228,6 +230,7 @@ class DSV
228
230
 
229
231
  # 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
232
  def write(*selected_columns)
233
+ selected_columns.flatten!
231
234
  prepare_to_rewrite if @mode == 'r+'
232
235
  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
236
  write_header(*selected_columns) if header_row? && attributes && writes_header?
@@ -274,9 +277,9 @@ class DSV
274
277
  end
275
278
  else
276
279
  if selected_columns.empty? || @as_array
277
- read_csv(selected_columns).each{|row| yield row}
280
+ read_csv(*selected_columns).each{|row| yield row}
278
281
  else
279
- read_csv(selected_columns).each do |row|
282
+ read_csv(*selected_columns).each do |row|
280
283
  yield selected_columns.inject({}){|hash, column_name| hash[column_name] = row[column_name]; hash}
281
284
  end
282
285
  end
@@ -542,7 +545,6 @@ class DSV
542
545
  end
543
546
 
544
547
  def parse_fields(fields, *selected_columns)
545
- selected_columns.flatten!
546
548
  return fields.values_at(*positions(selected_columns)) if @as_array && !selected_columns.empty?
547
549
  parsed_row = {}
548
550
  i = -1
data/test/dsv_test.rb CHANGED
@@ -396,6 +396,12 @@ describe DSV do
396
396
  _(DSV.new(DATA).quote).must_be_nil
397
397
  end
398
398
 
399
+ it "gives its own defaults frozen" do
400
+ csv = DSV.new("a,b\n")
401
+ _(csv.row_separator).must_be :frozen?
402
+ _(csv.mode).must_be :frozen?
403
+ end
404
+
399
405
  it "has its interface public and its internals private" do
400
406
  csv = DSV.new(DATA, headers: true)
401
407
  [:close, :read, :read_header, :parse, :columns, :columns=, :repeated_names, :parse_row, :write, :write_header, :write_row, :each, :attributes, :attributes=, :header_row?, :first_row, :to_a].each{|name| _(csv.public_methods).must_include name}
@@ -423,6 +429,16 @@ describe DSV do
423
429
  _(DSV.new(DATA, headers: true).read(['a', 'c'])).must_equal [{'a' => '1', 'c' => '3'}, {'a' => '4', 'c' => '6'}]
424
430
  end
425
431
 
432
+ it "gives each row the columns selected as one list, however they were given" do
433
+ [['a', 'c'], [['a', 'c']]].each do |selection|
434
+ csv = DSV.new(DATA, headers: true)
435
+ original = csv.method(:parse_fields)
436
+ received = []
437
+ csv.stub(:parse_fields, ->(fields, *columns){received << columns; original.call(fields, *columns)}){csv.read(*selection)}
438
+ _(received).must_equal [['a', 'c'], ['a', 'c']]
439
+ end
440
+ end
441
+
426
442
  it "selects columns by position, keyed by position even under a header row" do
427
443
  _(DSV.new("1,2,3\n4,5,6\n").read(0, 2)).must_equal [{0 => '1', 2 => '3'}, {0 => '4', 2 => '6'}]
428
444
  _(DSV.new(DATA, headers: true).read(0, 2)).must_equal [{0 => '1', 2 => '3'}, {0 => '4', 2 => '6'}]
@@ -482,6 +498,18 @@ describe DSV do
482
498
  _(DSV.new(DATA, headers: true).each.to_a).must_equal KEYED
483
499
  _(DSV.new(DATA, headers: true).each('a').to_a).must_equal [{'a' => '1'}, {'a' => '4'}]
484
500
  end
501
+
502
+ it "reads the source with the columns selected, not an Array of them" do
503
+ [true, false].each do |as_array|
504
+ [[], ['a'], ['a', 'c']].each do |selection|
505
+ csv = DSV.new(DATA, headers: true, as_array: as_array)
506
+ original = csv.method(:read_csv)
507
+ received = nil
508
+ csv.stub(:read_csv, ->(*arguments){received = arguments; original.call(*arguments)}){csv.each(*selection){}}
509
+ _(received).must_equal selection
510
+ end
511
+ end
512
+ end
485
513
  end
486
514
 
487
515
  describe "#to_a" do
@@ -722,6 +750,21 @@ describe DSV do
722
750
  csv.quote = :single
723
751
  _(csv.read).must_equal [{0 => 'a', 1 => 'b'}]
724
752
  end
753
+
754
+ it "reads quoted fields without making a new copy of the quote character each time it is used" do
755
+ csv = DSV.new(%Q{"a","b"\n"c","d"\n})
756
+ quotes = []
757
+ GC.disable
758
+ begin
759
+ ObjectSpace.trace_object_allocations do
760
+ csv.read
761
+ ObjectSpace.each_object(::String){|s| quotes << s if ['"', '""'].include?(s) && ObjectSpace.allocation_sourcefile(s).to_s.end_with?('dsv.rb')}
762
+ end
763
+ ensure
764
+ GC.enable
765
+ end
766
+ _(quotes).must_be_empty
767
+ end
725
768
  end
726
769
 
727
770
  # Input which is not RFC 4180, read to the default rule: a quote is structure only where it opens a field, and a field it opens which does not close raises UnterminatedQuote.
@@ -781,6 +824,18 @@ describe DSV do
781
824
  _(output).must_equal "\"1\",\"3\"\n"
782
825
  end
783
826
 
827
+ it "gives each row the columns selected as one list, however they were given" do
828
+ [['a', 'c'], [['a', 'c']]].each do |selection|
829
+ received = []
830
+ written(columns: [:a, :b, :c]) do |csv|
831
+ csv.rows = [{'a' => 1, 'b' => 2, 'c' => 3}, {'a' => 4, 'b' => 5, 'c' => 6}]
832
+ original = csv.method(:write_row)
833
+ csv.stub(:write_row, ->(row, *columns){received << columns; original.call(row, *columns)}){csv.write(*selection)}
834
+ end
835
+ _(received).must_equal [['a', 'c'], ['a', 'c']]
836
+ end
837
+ end
838
+
784
839
  it "writes what it read back, so a source round-trips" do
785
840
  output = written(headers: true, columns: [:a, :b]){|csv| csv.rows = DSV.read("a,b\n1,2\n", headers: true); csv.write}
786
841
  _(DSV.read(output, headers: true)).must_equal [{'a' => '1', 'b' => '2'}]
data/test/helper.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'minitest/autorun'
4
4
  require 'minitest/mock'
5
+ require 'objspace'
5
6
  require 'stringio'
6
7
  require 'tmpdir'
7
8
 
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.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - thoran