dsv 0.16.0 → 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: 68ee37a3368b46ed10b4db2e410f307bd8dfa91a3cb4d6a4efc09cdd7d9f145d
4
- data.tar.gz: 603055fe7b22a34c27afa5a21f68cbfef07c547620098cc6337e391e4254aecc
3
+ metadata.gz: f51c0809df8ce80d534a86623f7936def15b4dc005fc3fa02a6df1a3a4b26f32
4
+ data.tar.gz: 29007e890ebe7230c885fcf09238ba2da51e59c948d90ab41cf406c88e61fa3d
5
5
  SHA512:
6
- metadata.gz: 5b31f1db141bbe95eee0c037db6ccf2c9e9c9c0d9384a089e9629567f53349a939d73ca46802b0fe9cfed8dc47207be8c31da88ce92a5ba9c6e034b85e905e5b
7
- data.tar.gz: b53b03fcce52c0ae7e42255908e1ac998df4afb1412909857833f0925506efe7553c0f6ed4cea03794cc502a96449c3bf62c8dd038dbe16667e6124324401aa9
6
+ metadata.gz: a50224c24dbe0e7e8d9f5dd03024b6d13f7d62d5067ae30be8d899931ccc44e8c9f13604f159c1b608c29b91d9ff833f9bbb7d27c616b7984bb5332308b5e381
7
+ data.tar.gz: 9d153d7278b130b0c8afb48d84ab791b65a2415ba2e881f8ed472540bc9db2143d92705492c60dee1a0afe5e1c9e44ab606717ad64e82820fa038c313b342cf6
data/CHANGELOG CHANGED
@@ -1,7 +1,31 @@
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
 
18
+ 0.16.1: ~ DSV.open: A file not yet there is written by name under w, w+, a and a+.
19
+
20
+ 1. ~ DSV.source_type: Under w, w+, a and a+, a String is a path. A file not yet there had been taken as text, and the rows written to it lost in a StringIO.
21
+ 2. ~ initialize(): The mode is given to source_type.
22
+ 3. ~ test/dsv_test.rb: Writing a new file by name under w, w+, a and a+ is tested, where it was skipped, and so is source_type under each mode.
23
+ 4. ~ README.md: Under w, w+, a or a+, a String is a path.
24
+ 5. ~ TODO: Modes as declarations is split into Modes by name, Nothing ignored, Column names in one place, and Path or text. The spacey modes reading what they write is a job, and the jobs are renumbered.
25
+ 6. ~ ROADMAP.md: 0.17.0 is modes by name, 0.18.0 nothing ignored, 0.19.0 column names in one place, 0.20.0 path or text, and streaming 0.21.0.
26
+ 7. ~ PROPOSED.md: Path or text asks whether a String can be text under a mode which writes.
27
+ 8. ~ DSV::VERSION: /0.16.0/0.16.1/
28
+
5
29
  0.16.0: + DSV.filter: An alias of DSV.select.
6
30
 
7
31
  1. + DSV.filter: An alias of select, as find_all is.
data/README.md CHANGED
@@ -47,7 +47,7 @@ DSV.read("1,2,3\n4,5,6\n", as_array: true)
47
47
  # => [["1", "2", "3"], ["4", "5", "6"]]
48
48
  ```
49
49
 
50
- A String that names an existing file is read as a file; any other String is read as text.
50
+ A String that names an existing file is read as a file; any other String is read as text. Under `w`, `w+`, `a` or `a+` a String is always a path, so that a file not yet there can be written by name.
51
51
 
52
52
  ### Selecting columns
53
53
 
data/TODO CHANGED
@@ -1,44 +1,55 @@
1
1
  # TODO
2
2
 
3
- ## Modes as declarations
3
+ ## Modes by name
4
+
5
+ 1. :unspecified, the name of the default, which quote returns where no mode is given.
6
+ 2. :unquoted, the name of the mode reading no quotes, :none kept as an alias.
7
+ 3. The reader chosen once, when the quote mode is set, as row_renderer is for writing, the two from one list of names and their aliases.
8
+ 4. The spacey modes read as their unspaced modes are, with a space after each separator.
9
+
10
+ ## Nothing ignored
11
+
12
+ 5. An unknown quote mode, file mode or option raises ArgumentError, as does an argument which new does not take.
13
+
14
+ ## Column names in one place
4
15
 
5
- 1. An unknown quote mode, file mode or option raises ArgumentError.
6
- 2. :unspecified, the name of the default.
7
- 3. :unquoted, the name of the mode reading no quotes, :none kept as an alias.
8
- 4. The reader chosen once, when the quote mode is set, as row_renderer is for writing.
9
- 5. .read and .open taking a path, and .parse text; a path which does not exist raising, and nothing inferred from whether a file exists.
10
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.
18
+
19
+ ## Path or text
20
+
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.
11
22
 
12
23
  ## Streaming
13
24
 
14
- 7. each yields each row as it is read.
15
- 8. each returns its receiver.
16
- 9. rows no longer filled by each.
17
- 10. Chunks from each_slice.
18
- 11. The source read once, start to end, with no rewind; the header from the first row read.
19
- 12. The line of an error found by seeking only where the source can.
20
- 13. 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.
21
32
 
22
33
  ## Checked and measured
23
34
 
24
- 14. 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.
25
- 15. A benchmark script against stdlib CSV, and the README's speed sentence written from its output.
26
- 16. 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.
27
38
 
28
39
  ## Speed, results unchanged
29
40
 
30
- 17. A per-row fast path for the default: split, the fields bare or wrapped in two quotes unwrapped, any other row read in full.
31
- 18. as_array: returning the split fields, rather than building a Hash and then an Array from it.
32
- 19. Rows built by zip.to_h.
33
- 20. 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.
34
45
 
35
46
  ## Features
36
47
 
37
- 21. :rfc_4180, a quote mode reading what RFC 4180 allows as :unspecified does and raising on what it forbids.
38
- 22. field_count:, :header or an Integer, off by default; a row of another width raising.
39
- 23. Writing with minimal quoting: a field quoted only where it holds a separator, a quote or a row terminator.
40
- 24. A line number in each parse error, and line_numbers:.
41
- 25. headers?, the instance-level header_row renamed, header_row kept as an alias.
42
- 26. <<, to append a row, and generate, to build a string.
43
- 27. Symbols and Strings interchangeable for column names, in a selection, a columns: list and a row's keys.
44
- 28. 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.0'
5
+ VERSION = '0.16.2'
6
6
  end
data/lib/dsv.rb CHANGED
@@ -32,8 +32,9 @@ class DSV
32
32
  end
33
33
  end
34
34
 
35
- def source_type(source)
36
- if ::File.exist?(source)
35
+ # A String is a path under a mode which writes, so that a file not yet there can be written by name, and otherwise where it names a file; any other String is text.
36
+ def source_type(source, mode = nil)
37
+ if ['w', 'w+', 'a', 'a+'].include?(normalised_mode(mode)) || ::File.exist?(source)
37
38
  DSV::File
38
39
  else
39
40
  DSV::String
@@ -151,7 +152,7 @@ class DSV
151
152
  end
152
153
 
153
154
  def read(*selected_columns, &block)
154
- selected_columns = selection(selected_columns)
155
+ selected_columns = selection(selected_columns).flatten
155
156
  if block
156
157
  parse(*selected_columns, &block)
157
158
  else
@@ -219,7 +220,7 @@ class DSV
219
220
  end
220
221
 
221
222
  def parse_row(raw_row, *selected_columns)
222
- 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)
223
224
  rescue UnexpectedRow => error
224
225
  error.line = 1
225
226
  raise
@@ -227,6 +228,7 @@ class DSV
227
228
 
228
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.
229
230
  def write(*selected_columns)
231
+ selected_columns.flatten!
230
232
  prepare_to_rewrite if @mode == 'r+'
231
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)
232
234
  write_header(*selected_columns) if header_row? && attributes && writes_header?
@@ -273,9 +275,9 @@ class DSV
273
275
  end
274
276
  else
275
277
  if selected_columns.empty? || @as_array
276
- read_csv(selected_columns).each{|row| yield row}
278
+ read_csv(*selected_columns).each{|row| yield row}
277
279
  else
278
- read_csv(selected_columns).each do |row|
280
+ read_csv(*selected_columns).each do |row|
279
281
  yield selected_columns.inject({}){|hash, column_name| hash[column_name] = row[column_name]; hash}
280
282
  end
281
283
  end
@@ -335,7 +337,7 @@ class DSV
335
337
  options = DSV.extract_options(args)
336
338
  @source = (
337
339
  if source.is_a?(::String)
338
- DSV.source_type(source).source(source, options)
340
+ DSV.source_type(source, options[:mode]).source(source, options)
339
341
  else
340
342
  source
341
343
  end
@@ -541,7 +543,6 @@ class DSV
541
543
  end
542
544
 
543
545
  def parse_fields(fields, *selected_columns)
544
- selected_columns.flatten!
545
546
  return fields.values_at(*positions(selected_columns)) if @as_array && !selected_columns.empty?
546
547
  parsed_row = {}
547
548
  i = -1
data/test/dsv_test.rb CHANGED
@@ -359,6 +359,11 @@ describe DSV do
359
359
  with_file{|path| _(DSV.source_type(path)).must_equal DSV::File}
360
360
  _(DSV.source_type(DATA)).must_equal DSV::String
361
361
  end
362
+
363
+ it "is DSV::File for any String under a mode which writes" do
364
+ ['w', 'w+', 'a', 'a+', :append, 'write_only'].each{|mode| _(DSV.source_type('new.csv', mode)).must_equal DSV::File}
365
+ ['r', 'r+', nil].each{|mode| _(DSV.source_type('new.csv', mode)).must_equal DSV::String}
366
+ end
362
367
  end
363
368
 
364
369
  describe ".new" do
@@ -418,6 +423,16 @@ describe DSV do
418
423
  _(DSV.new(DATA, headers: true).read(['a', 'c'])).must_equal [{'a' => '1', 'c' => '3'}, {'a' => '4', 'c' => '6'}]
419
424
  end
420
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
+
421
436
  it "selects columns by position, keyed by position even under a header row" do
422
437
  _(DSV.new("1,2,3\n4,5,6\n").read(0, 2)).must_equal [{0 => '1', 2 => '3'}, {0 => '4', 2 => '6'}]
423
438
  _(DSV.new(DATA, headers: true).read(0, 2)).must_equal [{0 => '1', 2 => '3'}, {0 => '4', 2 => '6'}]
@@ -477,6 +492,18 @@ describe DSV do
477
492
  _(DSV.new(DATA, headers: true).each.to_a).must_equal KEYED
478
493
  _(DSV.new(DATA, headers: true).each('a').to_a).must_equal [{'a' => '1'}, {'a' => '4'}]
479
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
480
507
  end
481
508
 
482
509
  describe "#to_a" do
@@ -776,6 +803,18 @@ describe DSV do
776
803
  _(output).must_equal "\"1\",\"3\"\n"
777
804
  end
778
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
+
779
818
  it "writes what it read back, so a source round-trips" do
780
819
  output = written(headers: true, columns: [:a, :b]){|csv| csv.rows = DSV.read("a,b\n1,2\n", headers: true); csv.write}
781
820
  _(DSV.read(output, headers: true)).must_equal [{'a' => '1', 'b' => '2'}]
@@ -914,12 +953,13 @@ describe DSV do
914
953
  end
915
954
  end
916
955
 
917
- it "writes a new file by name under a write mode" do
918
- skip "Decision point 4: Finding 14, a path that does not exist yet is taken as text; the note leans to a write mode making it a path"
919
- with_file do |path, directory|
920
- new_path = File.join(directory, 'new.csv')
921
- DSV.open(new_path, mode: 'w', headers: true, columns: [:a, :b], quote: :none){|csv| csv.rows = [{'a' => 1, 'b' => 2}]; csv.write}
922
- _(File.read(new_path)).must_equal "a,b\n1,2\n"
956
+ it "writes a new file by name under w, w+, a and a+" do
957
+ ['w', 'w+', 'a', 'a+'].each do |mode|
958
+ with_file do |path, directory|
959
+ new_path = File.join(directory, 'new.csv')
960
+ DSV.open(new_path, mode: mode, headers: true, columns: [:a, :b], quote: :none){|csv| csv.rows = [{'a' => 1, 'b' => 2}]; csv.write}
961
+ _(File.read(new_path)).must_equal "a,b\n1,2\n"
962
+ end
923
963
  end
924
964
  end
925
965
 
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.0
4
+ version: 0.16.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - thoran