dsv 0.14.2 → 0.16.0
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 +4 -4
- data/CHANGELOG +19 -0
- data/README.md +3 -2
- data/TODO +28 -33
- data/lib/DSV/VERSION.rb +1 -1
- data/lib/dsv.rb +34 -29
- data/test/dsv_test.rb +36 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 68ee37a3368b46ed10b4db2e410f307bd8dfa91a3cb4d6a4efc09cdd7d9f145d
|
|
4
|
+
data.tar.gz: 603055fe7b22a34c27afa5a21f68cbfef07c547620098cc6337e391e4254aecc
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5b31f1db141bbe95eee0c037db6ccf2c9e9c9c0d9384a089e9629567f53349a939d73ca46802b0fe9cfed8dc47207be8c31da88ce92a5ba9c6e034b85e905e5b
|
|
7
|
+
data.tar.gz: b53b03fcce52c0ae7e42255908e1ac998df4afb1412909857833f0925506efe7553c0f6ed4cea03794cc502a96449c3bf62c8dd038dbe16667e6124324401aa9
|
data/CHANGELOG
CHANGED
|
@@ -2,6 +2,25 @@
|
|
|
2
2
|
|
|
3
3
|
## 20261126
|
|
4
4
|
|
|
5
|
+
0.16.0: + DSV.filter: An alias of DSV.select.
|
|
6
|
+
|
|
7
|
+
1. + DSV.filter: An alias of select, as find_all is.
|
|
8
|
+
2. ~ test/dsv_test.rb: Selecting by filter is tested, beside select and find_all.
|
|
9
|
+
3. ~ README.md: The class-level aliases include filter.
|
|
10
|
+
4. ~ TODO: The job for filter is gone, and the rest are numbered from 1.
|
|
11
|
+
5. ~ ROADMAP.md: 0.16.0 is gone.
|
|
12
|
+
6. ~ DSV::VERSION: /0.15.0/0.16.0/
|
|
13
|
+
|
|
14
|
+
0.15.0: ~ DSV#read: Under :single, every field is read as single-quoted.
|
|
15
|
+
|
|
16
|
+
1. ~ split_row(): Under :single, :single_quoted and :single_quotes, every field is read as single-quoted, and a row with a field which isn't raises UnquotedField. They had been read as unspecified.
|
|
17
|
+
2. ~ split_wholly_quoted(), reassemble_wholly_quoted_fields(), unescaped_fields(), quoted_separator(), trailing_quotes(): Each takes the quote, of which quoted_separator() makes a separator once for each. A :double read of quoted fields takes about a sixth less time, the quote no longer being made from a literal for each field.
|
|
18
|
+
3. ~ test/dsv_test.rb: Reading under :single is tested, reading back what it writes and a mode set after construction included.
|
|
19
|
+
4. ~ README.md: Reading under :single is described.
|
|
20
|
+
5. ~ TODO: The job for :single is gone.
|
|
21
|
+
6. ~ ROADMAP.md: filter is 0.16.0, the declarations 0.17.0, without :single, and streaming 0.18.0.
|
|
22
|
+
7. ~ DSV::VERSION: /0.14.2/0.15.0/
|
|
23
|
+
|
|
5
24
|
0.14.2: ~ DSV.new: A path or text no longer has its header read twice.
|
|
6
25
|
|
|
7
26
|
1. ~ initialize(): A String source is opened by DSV::File.source or DSV::String.source, rather than taken from a DSV::File or DSV::String built and thrown away, whose own initialize had already read the header.
|
data/README.md
CHANGED
|
@@ -73,7 +73,7 @@ DSV.detect('people.csv', headers: true){|row| row['name'] == 'Ada'}
|
|
|
73
73
|
DSV.collect('people.csv', headers: true){|row| row['name'].upcase}
|
|
74
74
|
```
|
|
75
75
|
|
|
76
|
-
`foreach`, `map`, `find_all` and `find` are aliases. Each closes the file it opens. Without a block, each returns an Enumerator, which opens the file again on each pass, so `DSV.each('people.csv', headers: true).each_slice(100)` works as it would on any Enumerable.
|
|
76
|
+
`foreach`, `map`, `find_all`, `filter` and `find` are aliases. Each closes the file it opens. Without a block, each returns an Enumerator, which opens the file again on each pass, so `DSV.each('people.csv', headers: true).each_slice(100)` works as it would on any Enumerable.
|
|
77
77
|
|
|
78
78
|
### Any delimiter
|
|
79
79
|
|
|
@@ -94,9 +94,10 @@ Unspecified, quoting follows RFC 4180: a quoted field may hold the separator or
|
|
|
94
94
|
```ruby
|
|
95
95
|
DSV.read('plain.csv', headers: true, quote: :none) # no field is quoted; quotes are data
|
|
96
96
|
DSV.read('quoted.csv', headers: true, quote: :double) # every field is quoted, as DSV writes them
|
|
97
|
+
DSV.read('single.csv', headers: true, quote: :single) # every field is quoted with a single quote
|
|
97
98
|
```
|
|
98
99
|
|
|
99
|
-
A named mode is held to: under `:double` every field of every row is quoted, the header's included. A row with a field which isn't raises `DSV::UnquotedField`, whose `line` is the line its row began on and `mode` the mode in force. To read a file whose quoting varies, name no mode.
|
|
100
|
+
A named mode is held to: under `:double` every field of every row is quoted, the header's included. A row with a field which isn't raises `DSV::UnquotedField`, whose `line` is the line its row began on and `mode` the mode in force. To read a file whose quoting varies, name no mode. Under `:single` every field is single-quoted and held to as under `:double`, so a double quote is data; with no mode named, a single quote is data.
|
|
100
101
|
|
|
101
102
|
Input which isn't RFC 4180 is read to one rule. A quote opens a field only at the start of one, so a quote anywhere else, as in `12" pizza`, is data. A quoted field closes only at a quote followed by the separator or the end of the row, so one which doesn't close by the end of the source, as `"abc` or `"ab"c` doesn't, raises `DSV::UnterminatedQuote`, whose `line` is the line its row began on.
|
|
102
103
|
|
data/TODO
CHANGED
|
@@ -1,49 +1,44 @@
|
|
|
1
1
|
# TODO
|
|
2
2
|
|
|
3
|
-
## `filter`
|
|
4
|
-
|
|
5
|
-
1. filter, a class-level alias of select.
|
|
6
|
-
|
|
7
3
|
## Modes as declarations
|
|
8
4
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
8. attributes= setting columns, attributes always worked out from columns; rows read afterwards keyed by those names.
|
|
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
|
+
6. attributes= setting columns, attributes always worked out from columns; rows read afterwards keyed by those names.
|
|
16
11
|
|
|
17
12
|
## Streaming
|
|
18
13
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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.
|
|
26
21
|
|
|
27
22
|
## Checked and measured
|
|
28
23
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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.
|
|
32
27
|
|
|
33
28
|
## Speed, results unchanged
|
|
34
29
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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.
|
|
39
34
|
|
|
40
35
|
## Features
|
|
41
36
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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.
|
data/lib/DSV/VERSION.rb
CHANGED
data/lib/dsv.rb
CHANGED
|
@@ -71,6 +71,7 @@ class DSV
|
|
|
71
71
|
each(source, *args).select(&block)
|
|
72
72
|
end
|
|
73
73
|
alias_method :find_all, :select
|
|
74
|
+
alias_method :filter, :select
|
|
74
75
|
|
|
75
76
|
def reject(source, *args, &block)
|
|
76
77
|
each(source, *args).reject(&block)
|
|
@@ -392,7 +393,9 @@ class DSV
|
|
|
392
393
|
when :none, :unquoted
|
|
393
394
|
split_on(row, @column_separator)
|
|
394
395
|
when :double, :double_quoted, :double_quotes
|
|
395
|
-
split_wholly_quoted(row)
|
|
396
|
+
split_wholly_quoted(row, '"')
|
|
397
|
+
when :single, :single_quoted, :single_quotes
|
|
398
|
+
split_wholly_quoted(row, "'")
|
|
396
399
|
else
|
|
397
400
|
split_quoted(row)
|
|
398
401
|
end
|
|
@@ -428,17 +431,17 @@ class DSV
|
|
|
428
431
|
end
|
|
429
432
|
end
|
|
430
433
|
|
|
431
|
-
# The :double
|
|
432
|
-
def split_wholly_quoted(row)
|
|
433
|
-
raise UnquotedField.new(:double) unless row.start_with?(
|
|
434
|
-
fields = split_on(row.delete_prefix(
|
|
435
|
-
return reassemble_wholly_quoted_fields(row) unless fields.last && trailing_quotes(fields.last, -1).odd?
|
|
436
|
-
fields[-1] = fields.last.delete_suffix(
|
|
437
|
-
unescaped_fields(fields) || reassemble_wholly_quoted_fields(row)
|
|
434
|
+
# The contract of :double and :single, every field quoted with the quote given: the row split on quote-separator-quote, a closed last field ending in an odd run of quotes, counted from the last piece, whose opening quote the split has already taken. Read in full only where the last field seems open, as it is where it goes on to the next line, or where the split leaves a lone quote in a field, as a field holding quote-separator-quote does. A row not starting with the quote raises UnquotedField, at the cost of one byte compared. nil where the last field does not close.
|
|
435
|
+
def split_wholly_quoted(row, quote)
|
|
436
|
+
raise UnquotedField.new(quote == "'" ? :single : :double) unless row.start_with?(quote)
|
|
437
|
+
fields = split_on(row.delete_prefix(quote), quoted_separator(quote))
|
|
438
|
+
return reassemble_wholly_quoted_fields(row, quote) unless fields.last && trailing_quotes(fields.last, -1, quote.getbyte(0)).odd?
|
|
439
|
+
fields[-1] = fields.last.delete_suffix(quote)
|
|
440
|
+
unescaped_fields(fields, quote) || reassemble_wholly_quoted_fields(row, quote)
|
|
438
441
|
end
|
|
439
442
|
|
|
440
|
-
# A
|
|
441
|
-
def reassemble_wholly_quoted_fields(row)
|
|
443
|
+
# A wholly quoted row whose split left a lone quote in a field, read in full as unspecified reads one, except that a field which is not quoted raises UnquotedField. nil where a quoted field is still open after the last piece.
|
|
444
|
+
def reassemble_wholly_quoted_fields(row, quote)
|
|
442
445
|
pieces, separators = (
|
|
443
446
|
if @column_separator.is_a?(::Regexp)
|
|
444
447
|
pieces_and_separators(row, @column_separator)
|
|
@@ -448,40 +451,42 @@ class DSV
|
|
|
448
451
|
)
|
|
449
452
|
fields = []
|
|
450
453
|
buffer = nil
|
|
454
|
+
quote_byte, escaped = quote.getbyte(0), quote * 2
|
|
451
455
|
pieces.each_with_index do |piece, i|
|
|
452
|
-
if buffer && trailing_quotes(piece, -1).even?
|
|
456
|
+
if buffer && trailing_quotes(piece, -1, quote_byte).even?
|
|
453
457
|
buffer << separator_before(i, separators) << piece
|
|
454
458
|
elsif buffer
|
|
455
|
-
fields << (buffer << separator_before(i, separators) << piece.delete_suffix(
|
|
459
|
+
fields << (buffer << separator_before(i, separators) << piece.delete_suffix(quote)).gsub(escaped, quote)
|
|
456
460
|
buffer = nil
|
|
457
|
-
elsif !piece.start_with?(
|
|
458
|
-
raise UnquotedField.new(:double)
|
|
459
|
-
elsif trailing_quotes(piece, 0).even?
|
|
460
|
-
buffer = +piece.delete_prefix(
|
|
461
|
+
elsif !piece.start_with?(quote)
|
|
462
|
+
raise UnquotedField.new(quote == "'" ? :single : :double)
|
|
463
|
+
elsif trailing_quotes(piece, 0, quote_byte).even?
|
|
464
|
+
buffer = +piece.delete_prefix(quote)
|
|
461
465
|
else
|
|
462
|
-
fields << piece.delete_prefix(
|
|
466
|
+
fields << piece.delete_prefix(quote).delete_suffix(quote).gsub(escaped, quote)
|
|
463
467
|
end
|
|
464
468
|
end
|
|
465
469
|
fields unless buffer
|
|
466
470
|
end
|
|
467
471
|
|
|
468
472
|
# The fields of a row split as wholly quoted, a doubled quote read as one; nil where a field holds a lone quote, which the split alone cannot place. A field has twice the quotes it has unescaped exactly when every quote in it is one of a doubled pair.
|
|
469
|
-
def unescaped_fields(fields)
|
|
473
|
+
def unescaped_fields(fields, quote)
|
|
474
|
+
escaped = quote * 2
|
|
470
475
|
fields.collect do |field|
|
|
471
|
-
next field unless field.include?(
|
|
472
|
-
unescaped = field.gsub(
|
|
473
|
-
return nil unless field.count(
|
|
476
|
+
next field unless field.include?(quote)
|
|
477
|
+
unescaped = field.gsub(escaped, quote)
|
|
478
|
+
return nil unless field.count(quote) == 2 * unescaped.count(quote)
|
|
474
479
|
unescaped
|
|
475
480
|
end
|
|
476
481
|
end
|
|
477
482
|
|
|
478
|
-
# Quote, separator, quote: what divides the fields of a wholly quoted row.
|
|
479
|
-
def quoted_separator
|
|
480
|
-
@
|
|
483
|
+
# Quote, separator, quote: what divides the fields of a wholly quoted row, made once for each quote.
|
|
484
|
+
def quoted_separator(quote)
|
|
485
|
+
(@quoted_separators ||= {})[quote] ||= (
|
|
481
486
|
if @column_separator.is_a?(::Regexp)
|
|
482
|
-
::Regexp.new('
|
|
487
|
+
::Regexp.new(quote + '(?:' + @column_separator.source + ')' + quote, @column_separator.options)
|
|
483
488
|
else
|
|
484
|
-
|
|
489
|
+
quote + @column_separator + quote
|
|
485
490
|
end
|
|
486
491
|
)
|
|
487
492
|
end
|
|
@@ -507,10 +512,10 @@ class DSV
|
|
|
507
512
|
fields unless buffer
|
|
508
513
|
end
|
|
509
514
|
|
|
510
|
-
# How many quotes a piece ends with, counting back to but not including the byte given, so that a field's opening quote is not taken for part of its closing. Read by byte, a quote being 34 in any ASCII-compatible encoding, so that nothing is allocated per character.
|
|
511
|
-
def trailing_quotes(piece, floor)
|
|
515
|
+
# How many quotes a piece ends with, counting back to but not including the byte given, so that a field's opening quote is not taken for part of its closing. Read by byte, a double quote being 34 and a single quote 39 in any ASCII-compatible encoding, so that nothing is allocated per character.
|
|
516
|
+
def trailing_quotes(piece, floor, quote_byte = 34)
|
|
512
517
|
i = piece.bytesize - 1
|
|
513
|
-
i -= 1 while i > floor && piece.getbyte(i) ==
|
|
518
|
+
i -= 1 while i > floor && piece.getbyte(i) == quote_byte
|
|
514
519
|
piece.bytesize - 1 - i
|
|
515
520
|
end
|
|
516
521
|
|
data/test/dsv_test.rb
CHANGED
|
@@ -163,9 +163,10 @@ describe DSV do
|
|
|
163
163
|
_(DSV.map(DATA, headers: true){|row| row['a'].to_i}).must_equal [1, 4]
|
|
164
164
|
end
|
|
165
165
|
|
|
166
|
-
it "selects the rows for which the block is true, also as find_all" do
|
|
166
|
+
it "selects the rows for which the block is true, also as find_all and filter" do
|
|
167
167
|
_(DSV.select(DATA, headers: true){|row| row['a'] == '4'}).must_equal [KEYED.last]
|
|
168
168
|
_(DSV.find_all(DATA, headers: true){|row| row['a'] == '4'}).must_equal [KEYED.last]
|
|
169
|
+
_(DSV.filter(DATA, headers: true){|row| row['a'] == '4'}).must_equal [KEYED.last]
|
|
169
170
|
end
|
|
170
171
|
|
|
171
172
|
it "rejects the rows for which the block is true" do
|
|
@@ -682,6 +683,40 @@ describe DSV do
|
|
|
682
683
|
output = written(headers: true, columns: ['a', 'b']){|csv| csv.rows = [{'a' => "x\ny", 'b' => '2'}]; csv.write}
|
|
683
684
|
_(DSV.read(output, headers: true, quote: :double)).must_equal [{'a' => "x\ny", 'b' => '2'}]
|
|
684
685
|
end
|
|
686
|
+
|
|
687
|
+
it "reads under quote: :single as under :double, with a single quote" do
|
|
688
|
+
_(DSV.parse_line("'a,b','c'", quote: :single)).must_equal ['a,b', 'c']
|
|
689
|
+
_(DSV.read("'a','b'\n'it''s','2'\n", headers: true, quote: :single)).must_equal [{'a' => "it's", 'b' => '2'}]
|
|
690
|
+
_(DSV.read("'a'',''b','c'\n", quote: :single_quoted)).must_equal [{0 => "a','b", 1 => 'c'}]
|
|
691
|
+
_(DSV.read("'x\ny','2'\n", quote: 'single')).must_equal [{0 => "x\ny", 1 => '2'}]
|
|
692
|
+
_(DSV.read("'a'; 'b'\n'x; y';'2'\n", headers: true, quote: :single_quotes, column_separator: /;\s*/)).must_equal [{'a' => 'x; y', 'b' => '2'}]
|
|
693
|
+
end
|
|
694
|
+
|
|
695
|
+
it "reads a double quote as data under quote: :single, and a single quote as data with no mode named" do
|
|
696
|
+
_(DSV.parse_line("'\"a\"','b'", quote: :single)).must_equal ['"a"', 'b']
|
|
697
|
+
_(DSV.parse_line("'a','b'")).must_equal ["'a'", "'b'"]
|
|
698
|
+
end
|
|
699
|
+
|
|
700
|
+
it "raises UnquotedField on a row under quote: :single with a field not quoted, naming its line and the mode" do
|
|
701
|
+
_{DSV.parse_line("O'Brien,'x'", quote: :single)}.must_raise DSV::UnquotedField
|
|
702
|
+
_{DSV.read("'a',b,'c'\n", quote: :single)}.must_raise DSV::UnquotedField
|
|
703
|
+
_{DSV.read("\"a\",\"b\"\n", quote: :single)}.must_raise DSV::UnquotedField
|
|
704
|
+
error = _{DSV.read("'a','b'\n'y',3\n", quote: :single)}.must_raise DSV::UnquotedField
|
|
705
|
+
_(error.line).must_equal 2
|
|
706
|
+
_(error.mode).must_equal :single
|
|
707
|
+
_{DSV.read("'x','abc\n", quote: :single)}.must_raise DSV::UnterminatedQuote
|
|
708
|
+
end
|
|
709
|
+
|
|
710
|
+
it "reads back what it writes under quote: :single" do
|
|
711
|
+
output = written(headers: true, columns: ['a', 'b'], quote: :single){|csv| csv.rows = [{'a' => "it's, here", 'b' => "x\ny"}]; csv.write}
|
|
712
|
+
_(DSV.read(output, headers: true, quote: :single)).must_equal [{'a' => "it's, here", 'b' => "x\ny"}]
|
|
713
|
+
end
|
|
714
|
+
|
|
715
|
+
it "reads with the quote of a mode set after construction" do
|
|
716
|
+
csv = DSV.new("'a','b'\n")
|
|
717
|
+
csv.quote = :single
|
|
718
|
+
_(csv.read).must_equal [{0 => 'a', 1 => 'b'}]
|
|
719
|
+
end
|
|
685
720
|
end
|
|
686
721
|
|
|
687
722
|
# 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.
|