typedcsv 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 50d01bbde2f4dc34fe2d698b490c2d786b8a5549
4
+ data.tar.gz: 54f8597c317508902b6709c308ba14eb45aa48da
5
+ SHA512:
6
+ metadata.gz: 51e70f6b102ff7e8b91fd36899a4ac06960564444ebbabb5925533901f5de39abf804c28ced2af41d662eef2d21a703fe0eb631ad8d29874a4d27fa0548eead0
7
+ data.tar.gz: 6a844258d5e748328b87787e901c36d8c6e3a7daf43ad18d3f02c703942f31187ab936c7498040e7f139a98b94da80ed0ff370c39ba879fa8fd448cb7ed65bf6
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.15.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at seamus@abshere.net. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in typedcsv.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Seamus Abshere
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # Typedcsv
2
+
3
+ Here's your standard untyped CSV:
4
+
5
+ ```
6
+ name,income,created_at,tags
7
+ Seamus,12301.2,2012-02-21,red;blue
8
+ ```
9
+
10
+ Now, you and I know that `12301.2` is a number and `2012-02-21` is a date and `red;blue` is a list... so let's just write that into the headers:
11
+
12
+ ```
13
+ name,income:number,created_at:date,tags:list
14
+ Seamus,12301.2,2012-02-21,red;blue
15
+ ```
16
+
17
+ Now let's parse it:
18
+
19
+ ```
20
+ Typedcsv.foreach('file.csv', headers: true) do |row|
21
+ row['income'] # will be a Float
22
+ row['created_at'] # will be a Date
23
+ row['tags'] # will be an Array
24
+ end
25
+ ```
26
+
27
+ This gem provides `Typedcsv.foreach()`, which takes exactly the same arguments as [ruby stdlib `CSV.foreach`](https://ruby-doc.org/stdlib-2.4.1/libdoc/csv/rdoc/CSV.html#method-c-foreach).
28
+
29
+ ## Types
30
+
31
+ * text (default)
32
+ * number
33
+ * list (must be semicolon-separated)
34
+ * date (must be ISO8601)
35
+ * time (must be ISO8601)
36
+
37
+ ## Benchmarks
38
+
39
+ It's about 10x slower than ruby's stdlib `CSV.foreach`:
40
+
41
+ ```
42
+ cd benchmark && ruby benchmark.rb
43
+ [...]
44
+ CSV.foreach - array mode
45
+ 2.503 (± 0.0%) i/s - 13.000 in 5.197588s
46
+ Typedcsv.foreach - array mode
47
+ 0.253 (± 0.0%) i/s - 2.000 in 7.892107s
48
+ CSV.foreach - hash mode
49
+ 1.830 (± 0.0%) i/s - 10.000 in 5.466998s
50
+ Typedcsv.foreach - hash mode
51
+ 0.226 (± 0.0%) i/s - 2.000 in 8.867616s
52
+ ```
53
+
54
+ ## Sponsor
55
+
56
+ <p><a href="https://www.faraday.io"><img src="https://s3.amazonaws.com/faraday-assets/files/img/logo.svg" alt="Faraday logo"/></a></p>
57
+
58
+ We use [`typedcsv`](https://github.com/faradayio/typedcsv) for [B2C customer intelligence at Faraday](https://www.faraday.io).
59
+
60
+ ## Copyright
61
+
62
+ Copyright 2017 Faraday
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1 @@
1
+ example.csv
data/benchmark/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'typedcsv', path: File.expand_path('../../', __FILE__)
4
+ gem 'benchmark-ips'
5
+ gem 'faker'
@@ -0,0 +1,23 @@
1
+ PATH
2
+ remote: /Users/seamus/code/typedcsv
3
+ specs:
4
+ typedcsv (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ benchmark-ips (2.7.2)
10
+ faker (1.8.4)
11
+ i18n (~> 0.5)
12
+ i18n (0.8.6)
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ benchmark-ips
19
+ faker
20
+ typedcsv!
21
+
22
+ BUNDLED WITH
23
+ 1.15.4
@@ -0,0 +1,64 @@
1
+ require 'csv'
2
+
3
+ require 'bundler/setup'
4
+ require 'benchmark/ips'
5
+ require 'typedcsv'
6
+ require 'faker'
7
+ require 'securerandom'
8
+
9
+ PATH = 'example.csv'
10
+
11
+ if File.exist?(PATH)
12
+ $stderr.puts "using existing #{PATH.inspect}"
13
+ else
14
+ $stderr.puts "generating new #{PATH.inspect}"
15
+ File.open(PATH, 'w') do |f|
16
+ f.puts %w{
17
+ name
18
+ income:number
19
+ created_at:date
20
+ zipcode:text
21
+ list:list
22
+ uuid
23
+ quote
24
+ }.to_csv
25
+ (2**15).times do
26
+ f.puts [
27
+ Faker::Name.name,
28
+ rand(2**16) + rand(),
29
+ Faker::Date.backward(900),
30
+ Faker::Address.zip_code,
31
+ rand(5).times.map { Faker::TwinPeaks.location }.to_csv(col_sep: ';').chomp,
32
+ SecureRandom.uuid,
33
+ Faker::TwinPeaks.quote
34
+ ].to_csv
35
+ end
36
+ end
37
+ end
38
+
39
+ Benchmark.ips do |x|
40
+ x.report("CSV.foreach - array mode") do
41
+ count = 0
42
+ CSV.foreach(PATH) do |row|
43
+ count += 1
44
+ end
45
+ end
46
+ x.report("Typedcsv.foreach - array mode") do
47
+ count = 0
48
+ Typedcsv.foreach(PATH) do |row|
49
+ count += 1
50
+ end
51
+ end
52
+ x.report("CSV.foreach - hash mode") do
53
+ count = 0
54
+ CSV.foreach(PATH, headers: true) do |row|
55
+ count += 1
56
+ end
57
+ end
58
+ x.report("Typedcsv.foreach - hash mode") do
59
+ count = 0
60
+ Typedcsv.foreach(PATH, headers: true) do |row|
61
+ count += 1
62
+ end
63
+ end
64
+ end
File without changes
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "typedcsv"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ class Typedcsv
2
+ VERSION = "0.1.0"
3
+ end
data/lib/typedcsv.rb ADDED
@@ -0,0 +1,92 @@
1
+ require 'typedcsv/version'
2
+
3
+ require 'csv'
4
+ require 'date'
5
+ require 'time'
6
+
7
+ class Typedcsv
8
+ def Typedcsv.foreach(*args, &blk)
9
+ typedcsv = new(*args, &blk)
10
+ if args.last.is_a?(Hash) and args.last[:headers]
11
+ typedcsv.foreach_hash
12
+ else
13
+ typedcsv.foreach_array
14
+ end
15
+ end
16
+
17
+ attr_reader :args
18
+ attr_reader :blk
19
+
20
+ def initialize(*args, &blk)
21
+ @args = args
22
+ @blk = blk
23
+ end
24
+
25
+ def foreach_hash
26
+ headers = nil
27
+ CSV.foreach(*args) do |row|
28
+ unless headers
29
+ headers = Headers.new(row.headers)
30
+ end
31
+ blk.call headers.parse_hash(row)
32
+ end
33
+ end
34
+
35
+ def foreach_array
36
+ headers = nil
37
+ CSV.foreach(*args) do |row|
38
+ unless headers
39
+ headers = Headers.new(row)
40
+ next
41
+ end
42
+ blk.call headers.parse_array(row)
43
+ end
44
+ end
45
+
46
+ class Headers
47
+ attr_reader :raw
48
+ def initialize(raw)
49
+ @raw = raw
50
+ end
51
+ def types
52
+ @types ||= raw.each_with_index.map do |raw_k, i|
53
+ k, type = raw_k.split(':', 2)
54
+ if type
55
+ [k, type, "#{k}:#{type}", i]
56
+ else
57
+ [k, 'text', k, i]
58
+ end
59
+ end
60
+ end
61
+ def parse_array(row)
62
+ types.map do |k, type, _, i|
63
+ convert type, row[i]
64
+ end
65
+ end
66
+ def parse_hash(row)
67
+ types.inject({}) do |memo, (k, type, orig_k, _)|
68
+ v = row.fetch orig_k
69
+ memo[k] = convert(type, v)
70
+ memo
71
+ end
72
+ end
73
+ private
74
+ def convert(type, v)
75
+ case type
76
+ when 'text'
77
+ # defaults to no parsing
78
+ v
79
+ when 'list'
80
+ CSV.parse_line(v, col_sep: ';')
81
+ when 'date'
82
+ Time.parse(v).to_date
83
+ when 'time'
84
+ Time.parse(v).to_date
85
+ when 'number'
86
+ v.to_f
87
+ else
88
+ v
89
+ end
90
+ end
91
+ end
92
+ end
data/typedcsv.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "typedcsv/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "typedcsv"
8
+ spec.version = Typedcsv::VERSION
9
+ spec.authors = ["Seamus Abshere"]
10
+ spec.email = ["seamus@abshere.net"]
11
+
12
+ spec.summary = %q{Thin wrapper around Ruby's stdlib CSV parser that adds typed csv support.}
13
+ spec.homepage = "https://github.com/faradayio/typedcsv-ruby"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.15"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: typedcsv
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Seamus Abshere
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-09-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - seamus@abshere.net
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - benchmark/.gitignore
71
+ - benchmark/Gemfile
72
+ - benchmark/Gemfile.lock
73
+ - benchmark/benchmark.rb
74
+ - benchmark/generate.rb
75
+ - bin/console
76
+ - bin/setup
77
+ - lib/typedcsv.rb
78
+ - lib/typedcsv/version.rb
79
+ - typedcsv.gemspec
80
+ homepage: https://github.com/faradayio/typedcsv-ruby
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.6.8
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Thin wrapper around Ruby's stdlib CSV parser that adds typed csv support.
104
+ test_files: []