csv_generator_reader 0.1

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
+ SHA256:
3
+ metadata.gz: 78631b6ca32c31432b82a4930770ceab52ff6f10e2eb590d1eac70993a69c897
4
+ data.tar.gz: 6e925b753f417c2871b6ac0e04f1373bf54c1cc785b4c6203fa9905bb6f46c8a
5
+ SHA512:
6
+ metadata.gz: b93e760d19c9a776278eaabd8a67a79c8412328a3e7089333365cc1de225f05a6cbe32b0627e9d73397548fcb0fd49bc3824ff577f6c95731cf8aa5c4df8ae03
7
+ data.tar.gz: 68c045f61e3fee9e1f2a6e2b4730c225ab0a361af720bfd19a79f28ae7b65d7d7fd9f9858ddc82c8cd4d80a04edb042fef00946e592cab172011402dc51328f8
data/lib/Libro.rb ADDED
@@ -0,0 +1,9 @@
1
+ class Libro
2
+ attr_accessor :date, :isbn, :price
3
+
4
+ def initialize(date, isbn, price)
5
+ @date = date
6
+ @isbn = isbn
7
+ @price = Float(price)
8
+ end
9
+ end
@@ -0,0 +1,54 @@
1
+ require 'csv'
2
+ require_relative 'Libro'
3
+ require_relative 'helpers' # benchmark
4
+
5
+ class Generador
6
+ attr_accessor :nombre_archivo, :cantidad
7
+
8
+ def initialize(nombre, cantidad)
9
+ @nombre_archivo = nombre
10
+ @cantidad = cantidad
11
+ end
12
+
13
+ def isbn_gen
14
+ "#{rand.to_s[2..4]}-#{rand.to_s[2..2]}-#{rand.to_s[2..8]}-#{rand.to_s[2..2]}-#{rand.to_s[2..2]}"
15
+ end
16
+
17
+ def date_gen
18
+ from = 0.0
19
+ to = Time.now
20
+ Time.at(from + rand * (to.to_f - from.to_f)).strftime('%d/%m/%Y')
21
+ end
22
+
23
+ def price_gen
24
+ rand(0.0..99.99).truncate(2)
25
+ end
26
+
27
+ def file_gen
28
+ CSV.open(@nombre_archivo, 'w+', headers:['Date', 'ISBN', 'Amount'], write_headers: true) do |archivo|
29
+ (@cantidad * 1_000_000).times do
30
+ archivo << [date_gen.to_s, isbn_gen.to_s, price_gen]
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ archivo = ARGV[1]
37
+ cantidad = ARGV[0].to_i
38
+ nuevo_archivo = Generador.new(archivo, cantidad)
39
+
40
+ if cantidad
41
+ if /^.*\.csv/.match(archivo)
42
+ print_memory_usage do
43
+ print_time_spent do
44
+ puts "Generando archivo #{archivo} con #{cantidad} millones de registros"
45
+ nuevo_archivo.file_gen
46
+ puts 'COMPLETADO'
47
+ end
48
+ end
49
+ else
50
+ puts "ERROR SINTAXIS #{archivo}: ruby cvs_generator [cant * 1M] [nombre_de_archivo.csv]"
51
+ end
52
+ else
53
+ puts "ERROR SINTAXIS cantidad: #{cantidad}: ruby cvs_generator [cant * 1M] [nombre_de_archivo.csv]"
54
+ end
data/lib/csv_reader.rb ADDED
@@ -0,0 +1,32 @@
1
+ require 'csv'
2
+ require_relative 'Libro'
3
+
4
+ class Archivo
5
+ attr_accessor :archivo
6
+
7
+ def initialize(csv_file_name)
8
+ @libro_del_archivo = Enumerator.new do |y|
9
+ archivo = CSV.foreach(csv_file_name, headers: true).each
10
+ loop do
11
+ row = archivo.next
12
+ y << Libro.new(row[0], row[1], row[2])
13
+ end
14
+ end
15
+ end
16
+
17
+ def total_value_in_stock
18
+ sum = 0.0
19
+ loop do
20
+ sum += @libro_del_archivo.next.price
21
+ end
22
+ sum.truncate(2)
23
+ end
24
+
25
+ def cant_repetidos
26
+ repeticiones = Hash.new(0)
27
+ loop do
28
+ repeticiones[@libro_del_archivo.next.isbn] += 1
29
+ end
30
+ repeticiones
31
+ end
32
+ end
data/lib/helpers.rb ADDED
@@ -0,0 +1,17 @@
1
+ require 'benchmark'
2
+
3
+ def print_memory_usage
4
+ memory_before = `ps -o rss= -p #{Process.pid}`.to_i
5
+ yield
6
+ memory_after = `ps -o rss= -p #{Process.pid}`.to_i
7
+
8
+ puts "Memoria: #{((memory_after - memory_before) / 1024.0).round(2)} MB"
9
+ end
10
+
11
+ def print_time_spent
12
+ time = Benchmark.realtime do
13
+ yield
14
+ end
15
+
16
+ puts "Tiempo: #{time.round(2)}"
17
+ end
@@ -0,0 +1,16 @@
1
+ require 'csv'
2
+ require_relative 'csv_reader'
3
+ require_relative 'Libro'
4
+ require_relative 'helpers' # benchmark
5
+
6
+ archivo = ARGV[0]
7
+ reader = Archivo.new(archivo)
8
+ puts "Processing #{archivo}"
9
+ print_memory_usage do
10
+ print_time_spent do
11
+ puts "Total value = #{reader.total_value_in_stock}"
12
+ # cant_repetidos deshabilitado ya que se utiliza un archivo generado que dificilmente tenga repetidos
13
+ # puts reader.cant_repetidos
14
+ end
15
+ end
16
+
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: csv_generator_reader
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Sebastian Perez Adan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-05-12 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: csv_generator takes as input parameters the number of file entries in
14
+ millions and the name of the file to generate. csv_reader takes input files generated
15
+ with csv_generator
16
+ email:
17
+ - sebapa@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - lib/Libro.rb
23
+ - lib/csv_generator.rb
24
+ - lib/csv_reader.rb
25
+ - lib/helpers.rb
26
+ - lib/stock_stats.rb
27
+ homepage: https://github.com/sebapa/csv_reader
28
+ licenses:
29
+ - GPL-3.0
30
+ metadata: {}
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubygems_version: 3.3.13
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: CSV file generator and reader of the generated files
50
+ test_files: []