Skip to content

Instantly share code, notes, and snippets.

@6ewis
Last active January 3, 2016 16:18
Show Gist options
  • Save 6ewis/3f553236634203a4ce94 to your computer and use it in GitHub Desktop.
Save 6ewis/3f553236634203a4ce94 to your computer and use it in GitHub Desktop.
line 108 and 109 . the ouput has some blanks line. why? line 85 does not output the unique customers
require 'csv'
require 'YAML'
class Customers
attr_reader :data, :cust_id, :elect_or_gas, :disconnect_doc, :move_in_date, :move_out_date, :bill_year, :bill_month, :span_days, :meter_read_date, :meter_read_type, :consumption, :exception_code
def initialize *values
@cust_id,
@elect_or_gas,
@disconnect_doc,
@move_in_date,
@move_out_date,
@bill_year,
@bill_month,
@span_days,
@meter_read_date,
@meter_read_type,
@consumption,
@exception_code, = values
@data = values
end
end
class Parse
attr_accessor :customers
def initialize filename
self.customers = []
deserialized_users filename
end
def deserialized_users filename
CSV.foreach(filename, col_sep: "|") do |row|
next if row.empty?
cust_id,
elect_or_gas,
disconnect_doc,
move_in_date,
move_out_date,
bill_year,
bill_month,
span_days,
meter_read_date,
meter_read_type,
consumption,
exception_code = row
#sanitized_values(cust_id, elect_or_gas, disconnect_doc, move_in_date, move_out_date, bill_year, bill_month, span_days, meter_read_date, meter_read_type, consumption, exception_code = row)
customers << Customers.new(*row)
end
customers
end
def serialized_users
File.open("./serialized_users.yaml", "w") do |yaml_file|
customers.each do |c|
yaml_file.puts YAML::dump([c.cust_id, c.elect_or_gas, c.disconnect_doc, c.move_in_date, c.move_out_date, c.bill_year, c.bill_month, c.span_days, c.meter_read_date, c.meter_read_type, c.consumption, c.exception_code])
end
end
end
#STATISTICS
def unique_customers
customers.map { |customer| customer.cust_id}.uniq
end
def customers_with_electricity
customers.select { |customer| customer.elect_or_gas == "1" && customer.elect_or_gas != "2" }
end
def customers_with_gas
customers.select { |customer| customer.elect_or_gas != "1" && customer.elect_or_gas == "2" }
end
#REPORT
def show_all_data
print "\n\nALL CUSTOMERS AND DATA\n\n"
puts customers.map { |customer| customer.data.map { |item| item.to_s.center 15 }.join '|' }
end
def show_unique_customers
print "\n\nUNIQUE CUSTOMERS\n\n"
puts customers.map { |customer| customer.data.map { |item| item.to_s.center 15 }.join '|' if unique_customers.include? customer.cust_id}
puts "\nNumber of unique customers: #{unique_customers.count}"
end
def show_customers_with_electricity
print "\n\nCUSTOMERS WITH ELECTRICITY ONLY\n\n"
puts customers.map { |customer| customer.data.map { |item| item.to_s.center 15 }.join '|' if customers_with_electricity.include? customer}
puts "\nNumber of customers with electricity: #{customers_with_electricity.count}"
end
def show_customers_with_gas
print "\n\nCUSTOMER WITH GAS ONLY\n\n"
puts customers.map { |customer| customer.data.map { |item| item.to_s.center 15 }.join '|' if customers_with_gas.include? customer}
puts "\nNumber of customers with gas: #{customers_with_gas.count}"
end
end
p = Parse.new "./pulse_data.txt"
p.serialized_users
p.show_all_data
p.show_unique_customers
p.show_customers_with_gas
p.show_customers_with_electricity
######################OUTPUT
ALL CUSTOMERS AND DATA
CustID | ElecOrGas |Disconnect Doc | Move In Date | Move Out Date | Bill Year | BillMonth | Span Days |Meter Read Date|Meter Read Type| Consumption |Exception Code
008000601 | 1 | N | 20080529 | 99991231 | 2011 | 6 | 33 | 20110606 | A | 28320 |
108000601 | 2 | N | 20080529 | 99991231 | 2011 | 7 | 29 | 20110705 | A | 13760 |
108500601 | 2 | N | 20080529 | 99991231 | 2011 | 8 | 30 | 20110804 | A | 16240 |
108000601 | 1 | N | 20080529 | 99991231 | 2011 | 9 | 28 | 20110901 | A | 12560 |
108000601 | 2 | N | 20080529 | 99991231 | 2011 | 10 | 33 | 20111004 | A | 12400 |
108000601 | 1 | N | 20080529 | 99991231 | 2011 | 11 | 28 | 20111101 | A | 9440 |
108030601 | 2 | N | 20080529 | 99991231 | 2011 | 12 | 34 | 20111205 | A | 12160 |
108000601 | 1 | N | 20080529 | 99991231 | 2012 | 1 | 32 | 20120106 | A | 11360 |
108020601 | 1 | N | 20080529 | 99991231 | 2012 | 2 | 31 | 20120206 | A | 10480 |
UNIQUE CUSTOMERS
CustID | ElecOrGas |Disconnect Doc | Move In Date | Move Out Date | Bill Year | BillMonth | Span Days |Meter Read Date|Meter Read Type| Consumption |Exception Code
008000601 | 1 | N | 20080529 | 99991231 | 2011 | 6 | 33 | 20110606 | A | 28320 |
108000601 | 2 | N | 20080529 | 99991231 | 2011 | 7 | 29 | 20110705 | A | 13760 |
108500601 | 2 | N | 20080529 | 99991231 | 2011 | 8 | 30 | 20110804 | A | 16240 |
108000601 | 1 | N | 20080529 | 99991231 | 2011 | 9 | 28 | 20110901 | A | 12560 |
108000601 | 2 | N | 20080529 | 99991231 | 2011 | 10 | 33 | 20111004 | A | 12400 |
108000601 | 1 | N | 20080529 | 99991231 | 2011 | 11 | 28 | 20111101 | A | 9440 |
108030601 | 2 | N | 20080529 | 99991231 | 2011 | 12 | 34 | 20111205 | A | 12160 |
108000601 | 1 | N | 20080529 | 99991231 | 2012 | 1 | 32 | 20120106 | A | 11360 |
108020601 | 1 | N | 20080529 | 99991231 | 2012 | 2 | 31 | 20120206 | A | 10480 |
Number of unique customers: 6
CUSTOMER WITH GAS ONLY
108000601 | 2 | N | 20080529 | 99991231 | 2011 | 7 | 29 | 20110705 | A | 13760 |
108500601 | 2 | N | 20080529 | 99991231 | 2011 | 8 | 30 | 20110804 | A | 16240 |
108000601 | 2 | N | 20080529 | 99991231 | 2011 | 10 | 33 | 20111004 | A | 12400 |
108030601 | 2 | N | 20080529 | 99991231 | 2011 | 12 | 34 | 20111205 | A | 12160 |
Number of customers with gas: 4
CUSTOMERS WITH ELECTRICITY ONLY
008000601 | 1 | N | 20080529 | 99991231 | 2011 | 6 | 33 | 20110606 | A | 28320 |
108000601 | 1 | N | 20080529 | 99991231 | 2011 | 9 | 28 | 20110901 | A | 12560 |
108000601 | 1 | N | 20080529 | 99991231 | 2011 | 11 | 28 | 20111101 | A | 9440 |
108000601 | 1 | N | 20080529 | 99991231 | 2012 | 1 | 32 | 20120106 | A | 11360 |
108020601 | 1 | N | 20080529 | 99991231 | 2012 | 2 | 31 | 20120206 | A | 10480 |
Number of customers with electricity: 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment