dsv.rb 0.12.11 → 0.12.13

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: 13e03215e0e2cb47c8d465d89adf834c4b42f07ba960c55be3f507302c8bc144
4
- data.tar.gz: 2364b5deb731a84f61184e45f1067a197eed32cd021ceabf1d9698c78cb3e23e
3
+ metadata.gz: 0ca4abca7cb6bd9076317aadd692522f0c182e5b59a3fb446f03914de15f9cb4
4
+ data.tar.gz: e1000934e68b46f82f268963ef9658f45d64f7aeb53a91e4601ce5de45995608
5
5
  SHA512:
6
- metadata.gz: a4c04c466523f340457d5b05a071912de80ad9bfd11fbbe40228106e5b2105df441f769b5267bf438e506e33ba63f04f59ca129c67f253ad15601b2296e85221
7
- data.tar.gz: 272a5e72d38bb666d791f096b313cbc8eaa11d9a6d34eedefd67785d38016df1b62d38c21565216e269dc8389392d0dd868f299ab15b9ea84494d8fb40519fca
6
+ metadata.gz: b6869a34fc41967008c873dd888a34fa82f08791ab180a7271a4c68f0c1ee7d6f0e1fdf79a0729711afa4fe6e3c812a4877ff47f5cba6fc5843c03ef5ee7279c
7
+ data.tar.gz: fead5bc4cb84b50a9e0698442fc4f7964e31a6cfd3ea4bacfea16c32780180711d57ffe77c9be7556001f3302a5413da52b78b75bf4a3eb8fda5b541a4981d13
data/CHANGELOG CHANGED
@@ -2,6 +2,23 @@
2
2
 
3
3
  ## 20261103
4
4
 
5
+ 0.12.13: reading honours quote:, given as a String or to DSV.parse_line.
6
+
7
+ 1. ~ split_row(): reads a quote mode given as a String as its Symbol, as row_renderer() does. A String mode had been read as unspecified.
8
+ 2. ~ DSV.parse_line: passes quote: on, rather than always reading as unspecified; its options to DSV.new written key: value, not with hashrockets.
9
+ 3. ~ test/dsv_test.rb: + quote: given as a String on read; + parse_line with quote:.
10
+ 4. ~ TODO: - 1.
11
+ 5. ~ lib/DSV/VERSION.rb: /0.12.12/0.12.13/.
12
+
13
+ 0.12.12: as_array: with a column selection gives the columns selected.
14
+
15
+ 1. ~ parse_fields(): under as_array:, a selection gives the selected fields; + positions(). Each column not selected had come back as nil, and a selection by position had lost its values.
16
+ 2. ~ DSV#each: under as_array:, selects by position. A selection by name had raised TypeError.
17
+ 3. ~ test/dsv_test.rb: + selections under as_array:, by name, by position, by a repeated name, and from rows already read.
18
+ 4. ~ README.md: + as_array:, and a selection under it.
19
+ 5. ~ TODO: - 1.
20
+ 6. ~ lib/DSV/VERSION.rb: /0.12.11/0.12.12/.
21
+
5
22
  0.12.11: the class methods close the files they open, and return an Enumerator without a block.
6
23
 
7
24
  1. ~ DSV.each, .read, .write, .header_row, .first_row, .attributes, .columns: + opened(), so that a path or text given is closed after the call, an early exit included; an IO given is left open. Each had left its file open.
data/README.md CHANGED
@@ -40,6 +40,13 @@ DSV.read("1,2,3\n4,5,6\n")
40
40
  # => [{0 => "1", 1 => "2", 2 => "3"}, {0 => "4", 1 => "5", 2 => "6"}]
41
41
  ```
42
42
 
43
+ With `as_array: true` each row is an Array of its values in column order, the form stdlib CSV gives:
44
+
45
+ ```ruby
46
+ DSV.read("1,2,3\n4,5,6\n", as_array: true)
47
+ # => [["1", "2", "3"], ["4", "5", "6"]]
48
+ ```
49
+
43
50
  A String that names an existing file is read as a file; any other String is read as text.
44
51
 
45
52
  ### Selecting columns
@@ -53,6 +60,8 @@ DSV.read('people.csv', 'name', headers: true)
53
60
  DSV.read('people.csv', headers: true, selected_columns: ['name'])
54
61
  ```
55
62
 
63
+ Under `as_array:` the Arrays hold the columns selected, in column order.
64
+
56
65
  ### Enumerating from the class
57
66
 
58
67
  The Enumerable calls are available on a source directly, each taking the same options:
data/TODO CHANGED
@@ -2,44 +2,42 @@
2
2
 
3
3
  ## 0.12.x, fixes
4
4
 
5
- 1. as_array: with a column selection: each('name') no longer raises TypeError, and read('name', as_array: true) gives only the columns selected. as_array: in the README, as the way to get Arrays as CSV gives them.
6
- 2. Reading honours quote: given as a String, as writing does; DSV.parse_line honours quote:.
7
- 3. A space as the column separator splits on each space, not as String#split(' ') does.
8
- 4. :double back to 0.12.4's reading, undoing 0.12.5 item 2: a row not starting with a quote split plainly, one starting with a quote split on quote-separator-quote, and one which leaves a lone quote in a field read in full as wholly quoted. The README back to 0.12.3's wording.
9
- 5. :single read, as :double is with a single quote.
10
- 6. filter, a class-level alias of select.
11
- 7. A String under a write mode taken as a path, so that a file not yet there can be written by name.
12
- 8. The internals private.
5
+ 1. A space as the column separator splits on each space, not as String#split(' ') does.
6
+ 2. :double back to 0.12.4's reading, undoing 0.12.5 item 2: a row not starting with a quote split plainly, one starting with a quote split on quote-separator-quote, and one which leaves a lone quote in a field read in full as wholly quoted. The README back to 0.12.3's wording.
7
+ 3. :single read, as :double is with a single quote.
8
+ 4. filter, a class-level alias of select.
9
+ 5. A String under a write mode taken as a path, so that a file not yet there can be written by name.
10
+ 6. The internals private.
13
11
 
14
12
  ## 0.13.0, modes as declarations
15
13
 
16
- 9. An unknown quote mode, file mode or option raises ArgumentError, where each is ignored today.
17
- 10. :mixed, the name of the default.
18
- 11. A mode raises on a contradiction which costs nothing to see, every row alike, header included: under :double, a row not starting with a quote, and a bare field in a row read in full. Declaring :mixed reads such a row.
14
+ 7. An unknown quote mode, file mode or option raises ArgumentError, where each is ignored today.
15
+ 8. :mixed, the name of the default.
16
+ 9. A mode raises on a contradiction which costs nothing to see, every row alike, header included: under :double, a row not starting with a quote, and a bare field in a row read in full. Declaring :mixed reads such a row.
19
17
 
20
18
  ## 0.14.0, streaming
21
19
 
22
- 12. each yields each row as it is read and returns its receiver, so that detect and break stop reading and memory does not grow with the file; rows is no longer filled by each. Chunks come from each_slice. A source read once, start to end, with no rewind, so that a pipe or $stdin can be read: the header from the first row read, and the line of an error found by seeking only where the source can; a rewind left to r+ rewriting and first_row called alone.
20
+ 10. each yields each row as it is read and returns its receiver, so that detect and break stop reading and memory does not grow with the file; rows is no longer filled by each. Chunks come from each_slice. A source read once, start to end, with no rewind, so that a pipe or $stdin can be read: the header from the first row read, and the line of an error found by seeking only where the source can; a rewind left to r+ rewriting and first_row called alone.
23
21
 
24
22
  ## 1.0
25
23
 
26
- 13. 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.
27
- 14. A benchmark script against stdlib CSV, and the README's speed sentence written from its output.
28
- 15. README: every mode by example, and a table matching each to stdlib CSV's and Python's settings.
24
+ 11. 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
+ 12. A benchmark script against stdlib CSV, and the README's speed sentence written from its output.
26
+ 13. README: every mode by example, and a table matching each to stdlib CSV's and Python's settings.
29
27
 
30
28
  ## 1.x, speed, results unchanged
31
29
 
32
- 16. A per-row fast path for the default: split, the fields bare or wrapped in two quotes unwrapped, any other row read in full.
33
- 17. as_array: returning the split fields, rather than building a Hash and then an Array from it.
34
- 18. The splitter chosen once, at construction, as row_renderer is for writing.
35
- 19. Two string predicates in place of four regex matches per piece; rows built by zip.to_h; the scanner-based parser, at the tag scanner-parser-20261006, judged against the rest.
30
+ 14. 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
+ 15. as_array: returning the split fields, rather than building a Hash and then an Array from it.
32
+ 16. The splitter chosen once, at construction, as row_renderer is for writing.
33
+ 17. Two string predicates in place of four regex matches per piece; rows built by zip.to_h; the scanner-based parser, at the tag scanner-parser-20261006, judged against the rest.
36
34
 
37
35
  ## 1.x, features
38
36
 
39
- 20. :rfc_4180, a quote mode reading what RFC 4180 allows as :mixed does and raising on what it forbids.
40
- 21. field_count:, :header or an Integer, off by default: a row of another width raises.
41
- 22. Writing: minimal quoting, a field quoted only where it holds a separator, a quote or a row terminator; a row terminator inside a field kept, quoted, or folded to a space; the encoding declared and a transcode offered with any loss marked.
42
- 23. A line number in each parse error, found only when the error is raised, as UnterminatedQuote finds its, where the source can seek. line_numbers:, off by default, counts lines as it reads instead, for any source a pipe included: the read loop chosen once, at construction, as row_renderer is for writing, so that without it no row pays for the count.
43
- 24. The instance-level header_row renamed headers?, kept as an alias; << to append a row and generate to build a string.
44
- 25. Symbols and Strings interchangeable for column names, in a selection, a columns: list and a row's keys.
45
- 26. The duplicate-header modes as an option: keep all as an Array, the default; index, a, a.1; first; last; error.
37
+ 18. :rfc_4180, a quote mode reading what RFC 4180 allows as :mixed does and raising on what it forbids.
38
+ 19. field_count:, :header or an Integer, off by default: a row of another width raises.
39
+ 20. Writing: minimal quoting, a field quoted only where it holds a separator, a quote or a row terminator; a row terminator inside a field kept, quoted, or folded to a space; the encoding declared and a transcode offered with any loss marked.
40
+ 21. A line number in each parse error, found only when the error is raised, as UnterminatedQuote finds its, where the source can seek. line_numbers:, off by default, counts lines as it reads instead, for any source a pipe included: the read loop chosen once, at construction, as row_renderer is for writing, so that without it no row pays for the count.
41
+ 22. The instance-level header_row renamed headers?, kept as an alias; << to append a row and generate to build a string.
42
+ 23. Symbols and Strings interchangeable for column names, in a selection, a columns: list and a row's keys.
43
+ 24. The duplicate-header modes as an option: keep all as an Array, the default; index, 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.12.11'
5
+ VERSION = '0.12.13'
6
6
  end
data/lib/dsv.rb CHANGED
@@ -125,7 +125,7 @@ class DSV
125
125
  options = DSV.extract_options(args)
126
126
  row_separator = options[:row_separator] || options[:row_sep] || "\n"
127
127
  column_separator = options[:column_separator] || options[:col_sep] || ','
128
- sc = DSV.new(raw_row, :quote => nil, :as_array => true, :row_separator => row_separator, :column_separator => column_separator)
128
+ sc = DSV.new(raw_row, quote: options[:quote], as_array: true, row_separator: row_separator, column_separator: column_separator)
129
129
  sc.parse_row(raw_row)
130
130
  end
131
131
 
@@ -271,7 +271,7 @@ class DSV
271
271
  # One row into its fields, by the quote mode: :none splits; :double splits a row as it promises, wholly quoted or wholly unquoted, and one that is neither as unspecified would; unspecified splits and reassembles a quoted field that held the separator, a doubled quote inside it reading as one. nil where a quoted field in the row is still open at its end. Direct string operations throughout; the scanner-based parser is on the branch scanner-parser.
272
272
  def split_row(raw_row)
273
273
  row = raw_row.chomp(@row_separator)
274
- case @quote
274
+ case @quote&.to_sym
275
275
  when :none, :unquoted
276
276
  split_on(row, @column_separator)
277
277
  when :double, :double_quoted, :double_quotes
@@ -388,6 +388,8 @@ class DSV
388
388
  end
389
389
 
390
390
  def parse_fields(fields, *selected_columns)
391
+ selected_columns.flatten!
392
+ return fields.values_at(*positions(selected_columns)) if @as_array && !selected_columns.empty?
391
393
  parsed_row = {}
392
394
  i = -1
393
395
  if selected_columns.empty?
@@ -399,7 +401,6 @@ class DSV
399
401
  fields.each{|column_value| store_field(parsed_row, attributes[i += 1], column_value)}
400
402
  end
401
403
  else
402
- selected_columns.flatten!
403
404
  case selected_columns[0]
404
405
  when Integer
405
406
  fields.each{|column_value| parsed_row[i] = column_value unless !selected_columns.include?(i += 1)}
@@ -418,6 +419,11 @@ class DSV
418
419
  end
419
420
  end
420
421
 
422
+ # The positions of the columns selected, in column order, a repeated name giving each of its own.
423
+ def positions(selected_columns)
424
+ selected_columns.flat_map{|column| column.is_a?(::Integer) ? column : Array((columns || {})[column])}.sort
425
+ end
426
+
421
427
  # With no columns defined, rows keyed by name supply them, and the header, from the first row's keys; rows keyed by position write positionally and have no header to write.
422
428
  def write(*selected_columns)
423
429
  prepare_to_rewrite if @mode == 'r+'
@@ -492,14 +498,16 @@ class DSV
492
498
  if @rows[0]
493
499
  if selected_columns.empty?
494
500
  @rows.each{|row| yield row}
501
+ elsif @as_array
502
+ @rows.each{|row| yield row.values_at(*positions(selected_columns))}
495
503
  else
496
504
  @rows.each do |row|
497
505
  yield selected_columns.inject({}){|hash, column_name| hash[column_name] = row[column_name]; hash}
498
506
  end
499
507
  end
500
508
  else
501
- if selected_columns.empty?
502
- read_csv.each{|row| yield row}
509
+ if selected_columns.empty? || @as_array
510
+ read_csv(selected_columns).each{|row| yield row}
503
511
  else
504
512
  read_csv(selected_columns).each do |row|
505
513
  yield selected_columns.inject({}){|hash, column_name| hash[column_name] = row[column_name]; hash}
data/test/dsv_test.rb CHANGED
@@ -118,6 +118,15 @@ describe DSV do
118
118
  _(yielded(:each, DATA, 'a', headers: true)).must_equal [{'a' => '1'}, {'a' => '4'}]
119
119
  _(yielded(:parse, DATA, 'b', headers: true)).must_equal [{'b' => '2'}, {'b' => '5'}]
120
120
  end
121
+
122
+ it "gives only the columns selected under as_array:, in column order" do
123
+ _(DSV.read(DATA, 'a', headers: true, as_array: true)).must_equal [['1'], ['4']]
124
+ _(DSV.read(DATA, ['c', 'a'], headers: true, as_array: true)).must_equal [['1', '3'], ['4', '6']]
125
+ _(DSV.read(DATA, headers: true, as_array: true, selected_columns: ['b'])).must_equal [['2'], ['5']]
126
+ _(DSV.read("1,2,3\n4,5,6\n", 1, as_array: true)).must_equal [['2'], ['5']]
127
+ _(DSV.read("a,a,b\n1,2,3\n", 'a', headers: true, as_array: true)).must_equal [['1', '2']]
128
+ _(yielded(:each, DATA, 'a', headers: true, as_array: true)).must_equal [['1'], ['4']]
129
+ end
121
130
  end
122
131
 
123
132
  describe ".parse" do
@@ -253,6 +262,12 @@ describe DSV do
253
262
  _(DSV.parse_line("1,2,3\r\n", row_sep: "\r\n")).must_equal ['1', '2', '3']
254
263
  end
255
264
 
265
+ it "takes quote:" do
266
+ _(DSV.parse_line("\"a\",b\n", quote: :none)).must_equal ['"a"', 'b']
267
+ _(DSV.parse_line("\"a\",b\n", quote: 'none')).must_equal ['"a"', 'b']
268
+ _(DSV.parse_line("\"a,b\",\"c\"\n", quote: :double)).must_equal ['a,b', 'c']
269
+ end
270
+
256
271
  it "leaves the caller's string as it was" do
257
272
  line = "1,2,3\n"
258
273
  DSV.parse_line(line)
@@ -415,6 +430,12 @@ describe DSV do
415
430
  end
416
431
 
417
432
  describe "#each" do
433
+ it "selects from rows already read under as_array:, in column order" do
434
+ csv = DSV.new(DATA, headers: true, as_array: true)
435
+ csv.read
436
+ _(csv.each(['c', 'a']).to_a).must_equal [['1', '3'], ['4', '6']]
437
+ end
438
+
418
439
  it "yields each row, reading the source first if it has not been read" do
419
440
  rows = []
420
441
  DSV.new(DATA, headers: true).each{|row| rows << row}
@@ -549,6 +570,11 @@ describe DSV do
549
570
  _(DSV.read("a,b\n\"x\",\"2\"\n", headers: true, quote: :none)).must_equal [{'a' => '"x"', 'b' => '"2"'}]
550
571
  end
551
572
 
573
+ it "reads a quote mode given as a String as its Symbol, as writing does" do
574
+ _(DSV.read("a,b\n\"x\",\"2\"\n", headers: true, quote: 'none')).must_equal [{'a' => '"x"', 'b' => '"2"'}]
575
+ _(DSV.read("\"a\",\"b\"\n\"x\",\"2\"\n", headers: true, quote: 'double')).must_equal [{'a' => 'x', 'b' => '2'}]
576
+ end
577
+
552
578
  it "strips the quotes under quote: :double, a row being wholly quoted or, as a header row often is, wholly unquoted" do
553
579
  _(DSV.read("a,b\n\"x\",\"2\"\n", headers: true, quote: :double)).must_equal [{'a' => 'x', 'b' => '2'}]
554
580
  _(DSV.read("\"a\",\"b\"\n\"say \"\"hi\"\"\",\"2\"\n", headers: true, quote: :double)).must_equal [{'a' => 'say "hi"', 'b' => '2'}]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dsv.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.11
4
+ version: 0.12.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - thoran