dsv.rb 0.12.13 → 0.12.15
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 +26 -0
- data/README.md +1 -1
- data/TODO +37 -41
- data/lib/DSV/VERSION.rb +1 -1
- data/lib/dsv.rb +40 -29
- data/test/dsv_test.rb +12 -0
- 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: 17e552f03171a7acc7b504ca028f8e861356fd3db7e9c632cdbb599f71ee82b0
|
|
4
|
+
data.tar.gz: 757077fc667f36bfc8b89f3988f08e229ceb6185d8803f6c13e78878754fbd27
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8783be7839a75cd0cdc4b437116c608ccae9f318968530551ea6d5c11827f833ef8d588e8da4ff593237953dbc7e3c500f7593cc98836d6d1e98314eb4e02312
|
|
7
|
+
data.tar.gz: 7ad81b76e4fa176c16d241db9c02ecb1b1b1ca5df8c4ed3a62e90d91da12a77aaf435ad0a7ae6e9a64a72e5760784c6a9eb85689a6f680e1ba392b1e0e41d9d1
|
data/CHANGELOG
CHANGED
|
@@ -1,5 +1,31 @@
|
|
|
1
1
|
# dsv/CHANGELOG
|
|
2
2
|
|
|
3
|
+
## 20261120
|
|
4
|
+
|
|
5
|
+
0.12.15: normalised_mode, row_renderer and write_row are laid out as SimpleCSV 0.11.5 has them.
|
|
6
|
+
|
|
7
|
+
1. ~ normalised_mode(): Each when and its result are joined by ;, not then.
|
|
8
|
+
2. ~ write_row(): The values are assigned from an if wrapped in parentheses, rather than one hanging from the assignment.
|
|
9
|
+
3. ~ row_renderer(): Each quote mode has a branch of its own, giving its renderer.
|
|
10
|
+
4. + quoted_row_renderer(), + unquoted_row_renderer(): The renderers, private. Each lambda holds its quote and separator, since literals inside it would be allocated for every field.
|
|
11
|
+
5. ~ TODO: - 1, 2. The fallback under :double is undone by 0.13.0's declarations, not by a patch back to 0.12.4, whose reading misread rows silently. :unspecified, not :mixed, names the default. Jobs are added: :unquoted as the name of the mode reading no quotes; the reader chosen once, brought forward from 1.x; and .read and .parse taking a path and text apart, into which the String under a write mode taken as a path is folded. :single moves to 0.13.0, once :double raises, since no release has read it.
|
|
12
|
+
6. ~ ROADMAP.md: The design philosophy opens with fail fast, otherwise go fast, and the default is the one reading which interprets. The layout is done, :double back to 0.12.4's reading is no longer listed under 0.12.x, and :single moves to 0.13.0. :unspecified names the default. 0.13.0 lists :unquoted, the reader chosen once and the source given as a path or text, and 2.0 is gone.
|
|
13
|
+
7. ~ PROPOSED.md: The version rule is no longer asked of :single, an addition. :unspecified replaces :mixed in the examples. Path or text asks only how DSV.new is told which a String is.
|
|
14
|
+
8. ~ lib/DSV/VERSION.rb: /0.12.14/0.12.15/.
|
|
15
|
+
|
|
16
|
+
## 20261108
|
|
17
|
+
|
|
18
|
+
0.12.14: A space as the column separator splits on each space.
|
|
19
|
+
|
|
20
|
+
1. ~ split_on(): A single space is split on as / /. String#split had taken ' ' as runs of whitespace, dropping any leading run, so empty fields were lost and a tab split as a space.
|
|
21
|
+
2. ~ split_quoted(): A String separator is split on by split_on().
|
|
22
|
+
3. ~ test/dsv_test.rb: A space separator is tested, under each quote mode, and with a tab.
|
|
23
|
+
4. ~ README.md: A String separator is split on as given.
|
|
24
|
+
5. ~ TODO: One job to a line, each marked with its release, the reasons moved to ROADMAP.md and what is undecided to PROPOSED.md. The space separator is done, and the string predicates were done in 0.12.0. The layout of normalised_mode, row_renderer and write_row, and the renaming of split_quoted() and split_double_quoted(), are added.
|
|
25
|
+
6. ~ ROADMAP.md: Each release is stated as what it does and why; what is undecided is moved to PROPOSED.md.
|
|
26
|
+
7. + PROPOSED.md: What is undecided, under a heading for each theme or change.
|
|
27
|
+
8. ~ lib/DSV/VERSION.rb: /0.12.13/0.12.14/.
|
|
28
|
+
|
|
3
29
|
## 20261103
|
|
4
30
|
|
|
5
31
|
0.12.13: reading honours quote:, given as a String or to DSV.parse_line.
|
data/README.md
CHANGED
|
@@ -85,7 +85,7 @@ DSV.read('people.txt', headers: true, column_separator: /,\s*/)
|
|
|
85
85
|
DSV.read('people.csv', headers: true, row_separator: "\r\n")
|
|
86
86
|
```
|
|
87
87
|
|
|
88
|
-
`col_sep:` and `row_sep:` are accepted as spellings of those. Writing takes the same two options, so a file round-trips through whatever it was read with. A Regexp says what may be read as a separator, not what to write, so writing with one raises `ArgumentError`; give a String to write.
|
|
88
|
+
A String is split on exactly as given: a space splits on each space, two in a row leaving an empty field between them, rather than on runs of whitespace as `String#split(' ')` does. `col_sep:` and `row_sep:` are accepted as spellings of those. Writing takes the same two options, so a file round-trips through whatever it was read with. A Regexp says what may be read as a separator, not what to write, so writing with one raises `ArgumentError`; give a String to write.
|
|
89
89
|
|
|
90
90
|
### Quoting
|
|
91
91
|
|
data/TODO
CHANGED
|
@@ -1,43 +1,39 @@
|
|
|
1
1
|
# TODO
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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.
|
|
3
|
+
1. filter, a class-level alias of select. (0.12.x)
|
|
4
|
+
2. The internals private. (0.12.x)
|
|
5
|
+
3. split_quoted() renamed quoted_fields(), and split_double_quoted() double_quoted_fields(). (0.12.x)
|
|
6
|
+
4. An unknown quote mode, file mode or option raises ArgumentError. (0.13.0)
|
|
7
|
+
5. :unspecified, the name of the default. (0.13.0)
|
|
8
|
+
6. :unquoted, the name of the mode reading no quotes, :none kept as an alias. (0.13.0)
|
|
9
|
+
7. The reader chosen once, when the quote mode is set, as row_renderer is for writing. (0.13.0)
|
|
10
|
+
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. (0.13.0)
|
|
11
|
+
9. Under :double, a row not starting with a quote raises, the header included. (0.13.0)
|
|
12
|
+
10. Under :double, a bare field in a row read in full raises. (0.13.0)
|
|
13
|
+
11. :single read as :double is, with a single quote, once :double raises. (0.13.0)
|
|
14
|
+
12. each yields each row as it is read. (0.14.0)
|
|
15
|
+
13. each returns its receiver. (0.14.0)
|
|
16
|
+
14. rows no longer filled by each. (0.14.0)
|
|
17
|
+
15. Chunks from each_slice. (0.14.0)
|
|
18
|
+
16. The source read once, start to end, with no rewind; the header from the first row read. (0.14.0)
|
|
19
|
+
17. The line of an error found by seeking only where the source can. (0.14.0)
|
|
20
|
+
18. A rewind left to r+ rewriting and to first_row called alone. (0.14.0)
|
|
21
|
+
19. 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. (1.0)
|
|
22
|
+
20. A benchmark script against stdlib CSV. (1.0)
|
|
23
|
+
21. README: the speed sentence written from the benchmark's output. (1.0)
|
|
24
|
+
22. README: every mode by example. (1.0)
|
|
25
|
+
23. README: a table matching each mode to stdlib CSV's and Python's settings. (1.0)
|
|
26
|
+
24. A per-row fast path for the default: split, the fields bare or wrapped in two quotes unwrapped, any other row read in full. (1.x)
|
|
27
|
+
25. as_array: returning the split fields, rather than building a Hash and then an Array from it. (1.x)
|
|
28
|
+
26. Rows built by zip.to_h. (1.x)
|
|
29
|
+
27. The scanner-based parser, at the tag scanner-parser-20261006, judged against the string operations used now. (1.x)
|
|
30
|
+
28. :rfc_4180, a quote mode reading what RFC 4180 allows as :unspecified does and raising on what it forbids. (1.x)
|
|
31
|
+
29. field_count:, :header or an Integer, off by default: a row of another width raises. (1.x)
|
|
32
|
+
30. Writing with minimal quoting: a field quoted only where it holds a separator, a quote or a row terminator. (1.x)
|
|
33
|
+
31. A line number in each parse error, found only when the error is raised, where the source can seek. (1.x)
|
|
34
|
+
32. line_numbers:, off by default, counting lines as they are read, for any source, a pipe included. (1.x)
|
|
35
|
+
33. headers?, the instance-level header_row renamed, header_row kept as an alias. (1.x)
|
|
36
|
+
34. <<, to append a row. (1.x)
|
|
37
|
+
35. generate, to build a string. (1.x)
|
|
38
|
+
36. Symbols and Strings interchangeable for column names, in a selection, a columns: list and a row's keys. (1.x)
|
|
39
|
+
37. Duplicate headers by option: all kept as an Array, the default; indexed, a, a.1; first; last; error. (1.x)
|
data/lib/DSV/VERSION.rb
CHANGED
data/lib/dsv.rb
CHANGED
|
@@ -22,11 +22,11 @@ class DSV
|
|
|
22
22
|
# A mode as Ruby's File spells it, from any of the spellings the library accepts, a Symbol included; unspecified is r.
|
|
23
23
|
def normalised_mode(mode)
|
|
24
24
|
case mode.to_s
|
|
25
|
-
when 'r', 'r+', 'w', 'w+', 'a', 'a+'
|
|
26
|
-
when 'read_only', 'read-only', 'readonly'
|
|
27
|
-
when 'rw', 'read_write', 'read-write', 'readwrite'
|
|
28
|
-
when 'write_only', 'write-only', 'writeonly'
|
|
29
|
-
when 'append'
|
|
25
|
+
when 'r', 'r+', 'w', 'w+', 'a', 'a+'; mode.to_s
|
|
26
|
+
when 'read_only', 'read-only', 'readonly'; 'r'
|
|
27
|
+
when 'rw', 'read_write', 'read-write', 'readwrite'; 'r+'
|
|
28
|
+
when 'write_only', 'write-only', 'writeonly'; 'w'
|
|
29
|
+
when 'append'; 'a'
|
|
30
30
|
else 'r'
|
|
31
31
|
end
|
|
32
32
|
end
|
|
@@ -281,9 +281,13 @@ class DSV
|
|
|
281
281
|
end
|
|
282
282
|
end
|
|
283
283
|
|
|
284
|
-
# A row split on a separator, String or Regexp; a Regexp's matches divide it, since String#split would also return any groups it captures.
|
|
284
|
+
# A row split on a separator, String or Regexp; a Regexp's matches divide it, since String#split would also return any groups it captures. A single space is split on as / /, since String#split takes ' ' to mean runs of whitespace, any leading run dropped.
|
|
285
285
|
def split_on(row, separator)
|
|
286
|
-
|
|
286
|
+
case separator
|
|
287
|
+
when ::Regexp; pieces_and_separators(row, separator).first
|
|
288
|
+
when ' '; row.split(/ /, -1)
|
|
289
|
+
else row.split(separator, -1)
|
|
290
|
+
end
|
|
287
291
|
end
|
|
288
292
|
|
|
289
293
|
# A row's pieces between a Regexp's matches, with the text of each match, so that a quoted field may be joined back with the separator it held.
|
|
@@ -303,7 +307,7 @@ class DSV
|
|
|
303
307
|
if @column_separator.is_a?(::Regexp)
|
|
304
308
|
reassemble_quoted_fields(*pieces_and_separators(row, @column_separator))
|
|
305
309
|
else
|
|
306
|
-
reassemble_quoted_fields(row
|
|
310
|
+
reassemble_quoted_fields(split_on(row, @column_separator))
|
|
307
311
|
end
|
|
308
312
|
end
|
|
309
313
|
|
|
@@ -450,13 +454,15 @@ end
|
|
|
450
454
|
# A nil value is an empty field, so the columns after it keep their places; with no columns defined a row's values are written in the row's own order.
|
|
451
455
|
def write_row(row, *selected_columns)
|
|
452
456
|
selected_columns.flatten!
|
|
453
|
-
values =
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
457
|
+
values = (
|
|
458
|
+
if !selected_columns.empty?
|
|
459
|
+
selected_columns.collect{|column| row[column]}
|
|
460
|
+
elsif attributes
|
|
461
|
+
values_in_order(row)
|
|
462
|
+
else
|
|
463
|
+
row.values
|
|
464
|
+
end
|
|
465
|
+
)
|
|
460
466
|
write_values(values)
|
|
461
467
|
end
|
|
462
468
|
alias_method :write_csv_row, :write_row
|
|
@@ -474,20 +480,14 @@ end
|
|
|
474
480
|
|
|
475
481
|
# Chosen once, at the first write, from the quote mode: how a row of values is rendered, with the instance's separators, a quote inside a quoted value doubled as RFC 4180 has it. The spacey modes put a space after each separator, as Array#to_csv_row did.
|
|
476
482
|
def row_renderer
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
joiner = spacey ? writable_column_separator + ' ' : writable_column_separator
|
|
486
|
-
if quote
|
|
487
|
-
doubled = quote * 2
|
|
488
|
-
->(values){values.collect{|value| quote + value.to_s.gsub(quote, doubled) + quote}.join(joiner) + @row_separator}
|
|
489
|
-
else
|
|
490
|
-
->(values){values.join(joiner) + @row_separator}
|
|
483
|
+
separator = writable_column_separator
|
|
484
|
+
case @quote&.to_sym
|
|
485
|
+
when :none, :unquoted; unquoted_row_renderer(separator)
|
|
486
|
+
when :spacey_none, :spacey_unquoted; unquoted_row_renderer(separator + ' ')
|
|
487
|
+
when :single; quoted_row_renderer("'", separator)
|
|
488
|
+
when :spacey_single; quoted_row_renderer("'", separator + ' ')
|
|
489
|
+
when :spacey_double; quoted_row_renderer('"', separator + ' ')
|
|
490
|
+
else quoted_row_renderer('"', separator)
|
|
491
491
|
end
|
|
492
492
|
end
|
|
493
493
|
|
|
@@ -563,4 +563,15 @@ end
|
|
|
563
563
|
end
|
|
564
564
|
end
|
|
565
565
|
|
|
566
|
+
private
|
|
567
|
+
|
|
568
|
+
def quoted_row_renderer(quote, separator)
|
|
569
|
+
doubled_quote = quote * 2
|
|
570
|
+
->(values){values.collect{|value| quote + value.to_s.gsub(quote, doubled_quote) + quote}.join(separator) + @row_separator}
|
|
571
|
+
end
|
|
572
|
+
|
|
573
|
+
def unquoted_row_renderer(separator)
|
|
574
|
+
->(values){values.join(separator) + @row_separator}
|
|
575
|
+
end
|
|
576
|
+
|
|
566
577
|
end # class DSV
|
data/test/dsv_test.rb
CHANGED
|
@@ -517,6 +517,18 @@ describe DSV do
|
|
|
517
517
|
_(DSV.read("a::b\n1::2\n", headers: true, column_separator: '::')).must_equal [{'a' => '1', 'b' => '2'}]
|
|
518
518
|
end
|
|
519
519
|
|
|
520
|
+
it "splits on each space where the separator is a space, an empty field between two and at either end" do
|
|
521
|
+
_(DSV.read(" a b \n", quote: :none, column_separator: ' ')).must_equal [{0 => '', 1 => 'a', 2 => '', 3 => 'b', 4 => ''}]
|
|
522
|
+
_(DSV.read("a b\n", column_separator: ' ')).must_equal [{0 => 'a', 1 => '', 2 => 'b'}]
|
|
523
|
+
_(DSV.read("\"x y\" 2\n", column_separator: ' ')).must_equal [{0 => 'x y', 1 => '', 2 => '2'}]
|
|
524
|
+
_(DSV.read("\"a\" \"\" \"b\"\n", quote: :double, column_separator: ' ')).must_equal [{0 => 'a', 1 => '', 2 => 'b'}]
|
|
525
|
+
_(DSV.read("a b\n", quote: :double, column_separator: ' ')).must_equal [{0 => 'a', 1 => '', 2 => 'b'}]
|
|
526
|
+
end
|
|
527
|
+
|
|
528
|
+
it "does not split on a tab where the separator is a space" do
|
|
529
|
+
_(DSV.read("a\tb c\n", column_separator: ' ')).must_equal [{0 => "a\tb", 1 => 'c'}]
|
|
530
|
+
end
|
|
531
|
+
|
|
520
532
|
it "reads a regular expression separator" do
|
|
521
533
|
_(DSV.read("a, b\n1,2\n", headers: true, column_separator: /,\s*/)).must_equal [{'a' => '1', 'b' => '2'}]
|
|
522
534
|
end
|