daru-io 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.rspec +2 -0
  4. data/.rspec_formatter.rb +24 -0
  5. data/.rubocop.yml +109 -0
  6. data/.travis.yml +30 -0
  7. data/.yardopts +2 -0
  8. data/CODE_OF_CONDUCT.md +46 -0
  9. data/CONTRIBUTING.md +65 -0
  10. data/Gemfile +20 -0
  11. data/Guardfile +7 -0
  12. data/LICENSE.md +21 -0
  13. data/README.md +654 -0
  14. data/Rakefile +12 -0
  15. data/daru-io.gemspec +39 -0
  16. data/lib/daru/io.rb +3 -0
  17. data/lib/daru/io/base.rb +45 -0
  18. data/lib/daru/io/exporters.rb +1 -0
  19. data/lib/daru/io/exporters/avro.rb +96 -0
  20. data/lib/daru/io/exporters/base.rb +54 -0
  21. data/lib/daru/io/exporters/csv.rb +103 -0
  22. data/lib/daru/io/exporters/excel.rb +148 -0
  23. data/lib/daru/io/exporters/json.rb +570 -0
  24. data/lib/daru/io/exporters/r_data.rb +66 -0
  25. data/lib/daru/io/exporters/rds.rb +79 -0
  26. data/lib/daru/io/exporters/sql.rb +55 -0
  27. data/lib/daru/io/importers.rb +1 -0
  28. data/lib/daru/io/importers/active_record.rb +75 -0
  29. data/lib/daru/io/importers/avro.rb +54 -0
  30. data/lib/daru/io/importers/base.rb +62 -0
  31. data/lib/daru/io/importers/csv.rb +190 -0
  32. data/lib/daru/io/importers/excel.rb +99 -0
  33. data/lib/daru/io/importers/excelx.rb +138 -0
  34. data/lib/daru/io/importers/html.rb +144 -0
  35. data/lib/daru/io/importers/json.rb +152 -0
  36. data/lib/daru/io/importers/mongo.rb +139 -0
  37. data/lib/daru/io/importers/plaintext.rb +97 -0
  38. data/lib/daru/io/importers/r_data.rb +74 -0
  39. data/lib/daru/io/importers/rds.rb +67 -0
  40. data/lib/daru/io/importers/redis.rb +135 -0
  41. data/lib/daru/io/importers/sql.rb +127 -0
  42. data/lib/daru/io/link.rb +80 -0
  43. data/lib/daru/io/version.rb +5 -0
  44. metadata +269 -0
@@ -0,0 +1,127 @@
1
+ require 'daru/io/importers/base'
2
+
3
+ module Daru
4
+ module IO
5
+ module Importers
6
+ # SQL Importer Class, that extends `from_sql` and `read_sql` methods to `Daru::DataFrame`
7
+ class SQL < Base
8
+ Daru::DataFrame.register_io_module :from_sql, self
9
+ Daru::DataFrame.register_io_module :read_sql, self
10
+
11
+ # Checks for required gem dependencies of SQL Importer
12
+ def initialize
13
+ optional_gem 'dbd-sqlite3', requires: 'dbd/SQLite3'
14
+ optional_gem 'activerecord', '~> 4.0', requires: 'active_record'
15
+ optional_gem 'dbi'
16
+ optional_gem 'sqlite3'
17
+ end
18
+
19
+ # Loads from a DBI connection
20
+ #
21
+ # @!method self.from(dbh)
22
+ #
23
+ # @param dbh [DBI::DatabaseHandle] A DBI connection.
24
+ #
25
+ # @return [Daru::IO::Importers::SQL]
26
+ #
27
+ # @example Importing from a DBI connection
28
+ # instance = Daru::IO::Importers::SQL.from(DBI.connect("DBI:Mysql:database:localhost", "user", "password"))
29
+ def from(dbh)
30
+ @dbh = dbh
31
+ self
32
+ end
33
+
34
+ # Reads from a sqlite.db file
35
+ #
36
+ # @!method self.read(path)
37
+ #
38
+ # @param path [String] Path to a SQlite3 database file.
39
+ #
40
+ # @return [Daru::IO::Importers::SQL]
41
+ #
42
+ # @example Reading from a sqlite.db file
43
+ # instance = Daru::IO::Importers::SQL.read('path/to/sqlite.db')
44
+ def read(path)
45
+ @dbh = attempt_sqlite3_connection(path) if Pathname(path).exist?
46
+ self
47
+ end
48
+
49
+ # Imports a `Daru::DataFrame` from SQL Importer instance
50
+ #
51
+ # @param query [String] The query to be executed
52
+ #
53
+ # @return [Daru::DataFrame]
54
+ #
55
+ # @example Importing with a SQL query
56
+ # df = instance.call("SELECT * FROM test")
57
+ #
58
+ # #=> #<Daru::DataFrame(2x3)>
59
+ # # id name age
60
+ # # 0 1 Homer 20
61
+ # # 1 2 Marge 30
62
+ def call(query)
63
+ @query = query
64
+ @conn, @adapter = choose_adapter(@dbh, @query)
65
+ df_hash = result_hash
66
+ Daru::DataFrame.new(df_hash).tap(&:update)
67
+ end
68
+
69
+ private
70
+
71
+ def attempt_sqlite3_connection(db)
72
+ DBI.connect("DBI:SQLite3:#{db}")
73
+ rescue SQLite3::NotADatabaseException
74
+ raise ArgumentError, "Expected #{db} to point to a SQLite3 database"
75
+ end
76
+
77
+ def choose_adapter(db, query)
78
+ query = String.try_convert(query) or
79
+ raise ArgumentError, "Query must be a string, #{query.class} received"
80
+
81
+ case db
82
+ when DBI::DatabaseHandle
83
+ [db, :dbi]
84
+ when ::ActiveRecord::ConnectionAdapters::AbstractAdapter
85
+ [db, :activerecord]
86
+ else
87
+ raise ArgumentError, "Unknown database adapter type #{db.class}"
88
+ end
89
+ end
90
+
91
+ def column_names
92
+ case @adapter
93
+ when :dbi
94
+ result.column_names
95
+ when :activerecord
96
+ result.columns
97
+ end
98
+ end
99
+
100
+ def result
101
+ case @adapter
102
+ when :dbi
103
+ @conn.execute(@query)
104
+ when :activerecord
105
+ @conn.exec_query(@query)
106
+ end
107
+ end
108
+
109
+ def result_hash
110
+ column_names
111
+ .map(&:to_sym)
112
+ .zip(rows.transpose)
113
+ .to_h
114
+ end
115
+
116
+ def rows
117
+ case @adapter
118
+ when :dbi
119
+ result.to_a.map(&:to_a)
120
+ when :activerecord
121
+ result.cast_values
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,80 @@
1
+ module Daru
2
+ class DataFrame
3
+ class << self
4
+ # Links `Daru::DataFrame` Import / Export methods to corresponding
5
+ # `Daru::IO` Importer / Exporter classes. Here is the list of linkages:
6
+ #
7
+ # #### Importers
8
+ #
9
+ # | `Daru::DataFrame` method | `Daru::IO::Importers` class |
10
+ # | ----------------------------------- | -----------------------------------------|
11
+ # | `Daru::DataFrame.from_activerecord` | {Daru::IO::Importers::ActiveRecord#from} |
12
+ # | `Daru::DataFrame.read_avro` | {Daru::IO::Importers::Avro#read} |
13
+ # | `Daru::DataFrame.read_csv` | {Daru::IO::Importers::CSV#read} |
14
+ # | `Daru::DataFrame.read_excel` | {Daru::IO::Importers::Excel#read}, |
15
+ # | | {Daru::IO::Importers::Excelx#read} |
16
+ # | `Daru::DataFrame.read_html` | {Daru::IO::Importers::HTML#read} |
17
+ # | `Daru::DataFrame.from_json` | {Daru::IO::Importers::JSON#from} |
18
+ # | `Daru::DataFrame.read_json` | {Daru::IO::Importers::JSON#read} |
19
+ # | `Daru::DataFrame.from_mongo` | {Daru::IO::Importers::Mongo#from} |
20
+ # | `Daru::DataFrame.read_plaintext` | {Daru::IO::Importers::Plaintext#read} |
21
+ # | `Daru::DataFrame.read_rdata` | {Daru::IO::Importers::RData#read} |
22
+ # | `Daru::DataFrame.read_rds` | {Daru::IO::Importers::RDS#read} |
23
+ # | `Daru::DataFrame.from_redis` | {Daru::IO::Importers::Redis#from} |
24
+ # | `Daru::DataFrame.from_sql` | {Daru::IO::Importers::SQL#from} |
25
+ #
26
+ # #### Exporters
27
+ #
28
+ # | `Daru::DataFrame` instance method | `Daru::IO::Exporters` class |
29
+ # | --------------------------------- | -----------------------------------|
30
+ # | `Daru::DataFrame.to_avro_string` | {Daru::IO::Exporters::Avro#to_s} |
31
+ # | `Daru::DataFrame.write_avro` | {Daru::IO::Exporters::Avro#write} |
32
+ # | `Daru::DataFrame.to_csv_string` | {Daru::IO::Exporters::CSV#to_s} |
33
+ # | `Daru::DataFrame.write_csv` | {Daru::IO::Exporters::CSV#write} |
34
+ # | `Daru::DataFrame.to_excel_string` | {Daru::IO::Exporters::Excel#to_s} |
35
+ # | `Daru::DataFrame.write_excel` | {Daru::IO::Exporters::Excel#write} |
36
+ # | `Daru::DataFrame.to_json` | {Daru::IO::Exporters::JSON#to} |
37
+ # | `Daru::DataFrame.to_json_string` | {Daru::IO::Exporters::JSON#to_s} |
38
+ # | `Daru::DataFrame.write_json` | {Daru::IO::Exporters::JSON#write} |
39
+ # | `Daru::DataFrame.to_rds_string` | {Daru::IO::Exporters::RDS#to_s} |
40
+ # | `Daru::DataFrame.write_rds` | {Daru::IO::Exporters::RDS#write} |
41
+ # | `Daru::DataFrame.to_sql` | {Daru::IO::Exporters::SQL#to} |
42
+ #
43
+ # @param function [Symbol] Functon name to be monkey-patched into +Daru::DataFrame+
44
+ # @param instance [Class] The Daru-IO class to be linked to monkey-patched function
45
+ #
46
+ # @return A `Daru::DataFrame` class method in case of Importer, and instance
47
+ # variable method in case of Exporter.
48
+ def register_io_module(function, instance=nil, &block)
49
+ return define_singleton_method(function, &block) if block_given?
50
+
51
+ case function.to_s
52
+ when /\Ato_.*_string\Z/, /\Ato_/, /\Awrite_/ then register_exporter(function, instance)
53
+ when /\Afrom_/, /\Aread_/ then register_importer(function, instance)
54
+ else raise ArgumentError, 'Invalid function name given to monkey-patch into Daru::DataFrame.'
55
+ end
56
+ end
57
+
58
+ private
59
+
60
+ def register_exporter(function, instance)
61
+ define_method(function) do |*args, &io_block|
62
+ case function.to_s
63
+ when /\Ato_.*_string\Z/ then instance.new(self, *args, &io_block).to_s
64
+ when /\Ato_/ then instance.new(self, *args, &io_block).to
65
+ when /Awrite_/ then instance.new(self, *args[1..-1], &io_block).write(*args[0])
66
+ end
67
+ end
68
+ end
69
+
70
+ def register_importer(function, instance)
71
+ define_singleton_method(function) do |*args, &io_block|
72
+ case function.to_s
73
+ when /\Afrom_/ then instance.new.from(*args[0]).call(*args[1..-1], &io_block)
74
+ when /\Aread_/ then instance.new.read(*args[0]).call(*args[1..-1], &io_block)
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,5 @@
1
+ module Daru
2
+ module IO
3
+ VERSION = '0.1.0'.freeze
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,269 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: daru-io
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Athitya Kumar
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-09-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: daru
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.5
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.5
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.15'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.15'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: redcarpet
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec-its
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: 0.40.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: 0.40.0
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop-rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rubygems-tasks
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: simplecov
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: webmock
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: yard
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ - !ruby/object:Gem::Dependency
182
+ name: guard-rspec
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
195
+ description: |2
196
+ Daru-IO is a plugin-gem to Daru gem, which stands for Data Analysis in RUby. Daru-IO extends support for many Import and Export methods of Daru::DataFrame. This gem is intended to help Rubyists who are into Data Analysis or Web Development, by serving as a general purpose conversion library, while also making it incredibly easy to getting started on analyzing data with daru.
197
+ email:
198
+ - athityakumar@gmail.com
199
+ executables: []
200
+ extensions: []
201
+ extra_rdoc_files: []
202
+ files:
203
+ - ".gitignore"
204
+ - ".rspec"
205
+ - ".rspec_formatter.rb"
206
+ - ".rubocop.yml"
207
+ - ".travis.yml"
208
+ - ".yardopts"
209
+ - CODE_OF_CONDUCT.md
210
+ - CONTRIBUTING.md
211
+ - Gemfile
212
+ - Guardfile
213
+ - LICENSE.md
214
+ - README.md
215
+ - Rakefile
216
+ - daru-io.gemspec
217
+ - lib/daru/io.rb
218
+ - lib/daru/io/base.rb
219
+ - lib/daru/io/exporters.rb
220
+ - lib/daru/io/exporters/avro.rb
221
+ - lib/daru/io/exporters/base.rb
222
+ - lib/daru/io/exporters/csv.rb
223
+ - lib/daru/io/exporters/excel.rb
224
+ - lib/daru/io/exporters/json.rb
225
+ - lib/daru/io/exporters/r_data.rb
226
+ - lib/daru/io/exporters/rds.rb
227
+ - lib/daru/io/exporters/sql.rb
228
+ - lib/daru/io/importers.rb
229
+ - lib/daru/io/importers/active_record.rb
230
+ - lib/daru/io/importers/avro.rb
231
+ - lib/daru/io/importers/base.rb
232
+ - lib/daru/io/importers/csv.rb
233
+ - lib/daru/io/importers/excel.rb
234
+ - lib/daru/io/importers/excelx.rb
235
+ - lib/daru/io/importers/html.rb
236
+ - lib/daru/io/importers/json.rb
237
+ - lib/daru/io/importers/mongo.rb
238
+ - lib/daru/io/importers/plaintext.rb
239
+ - lib/daru/io/importers/r_data.rb
240
+ - lib/daru/io/importers/rds.rb
241
+ - lib/daru/io/importers/redis.rb
242
+ - lib/daru/io/importers/sql.rb
243
+ - lib/daru/io/link.rb
244
+ - lib/daru/io/version.rb
245
+ homepage: https://github.com/athityakumar/daru-io
246
+ licenses:
247
+ - MIT
248
+ metadata: {}
249
+ post_install_message:
250
+ rdoc_options: []
251
+ require_paths:
252
+ - lib
253
+ required_ruby_version: !ruby/object:Gem::Requirement
254
+ requirements:
255
+ - - ">="
256
+ - !ruby/object:Gem::Version
257
+ version: '0'
258
+ required_rubygems_version: !ruby/object:Gem::Requirement
259
+ requirements:
260
+ - - ">="
261
+ - !ruby/object:Gem::Version
262
+ version: '0'
263
+ requirements: []
264
+ rubyforge_project:
265
+ rubygems_version: 2.6.12
266
+ signing_key:
267
+ specification_version: 4
268
+ summary: Daru-IO is a plugin-gem to Daru gem, which stands for Data Analysis in RUby.
269
+ test_files: []