Skip to content

Instantly share code, notes, and snippets.

/badcode Secret

Created January 13, 2015 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/be4ba6a867e9c9858ac9 to your computer and use it in GitHub Desktop.
Save anonymous/be4ba6a867e9c9858ac9 to your computer and use it in GitHub Desktop.
# An example of bad code, that should really be refactored.
def tap_count_ok?(csv_file)
# could put additional csv processing in here - currently just confirming tap count.
csv_data = CSV.read(csv_file)
# count taps by counting how many of the first column are digits
tap_count = 0
csv_data.each_index do |row|
tap_count += 1 if csv_data[row][0] =~ /\d+/
end
if tap_count == 27
27
elsif tap_count == 24
24
else
#puts "#{csv_file} did not have 24 or 27 taps - can not process"
false
end
end
def pull_zxyz_data_from_csv(csv_file)
tap_count = tap_count_ok?(csv_file)
csv_mod_time = File.mtime(csv_file)
csv_data = CSV.read(csv_file)
# the taps
z_x_taps = []
z_y_taps = []
z_z_taps = []
xy_x_taps = []
xy_y_taps = []
xy_z_taps = []
if csv_file.downcase.include?('dr1_data')
z_tap_rows = (2..9).to_a + (18..25).to_a
xy_tap_rows = (10..17).to_a
x_columns = [1,4]
y_columns = [2,5]
z_columns = [3,6]
elsif csv_file.downcase.include?('dr2_data')
z_tap_rows = (3..10).to_a + (19..26).to_a
xy_tap_rows = (11..18).to_a
x_columns = [1,2]
y_columns = [4,5]
z_columns = [7,8]
else
# default to DR2, because that's the future.
z_tap_rows = (3..10).to_a + (19..26).to_a
xy_tap_rows = (11..18).to_a
x_columns = [1,2]
y_columns = [4,5]
z_columns = [7,8]
end
# Z Tap data
# for all of the z rows, collect the absolute values of x,y,z max and min
(z_tap_rows).each do |row|
x_columns.each do |column|
z_x_taps << csv_data[row][column].to_f.abs
end
y_columns.each do |column|
z_y_taps << csv_data[row][column].to_f.abs
end
z_columns.each do |column|
z_z_taps << csv_data[row][column].to_f.abs
end
end
# XY Tap Data
# for all of the xy rows, collect the absolute values of x,y,z max and min
(xy_tap_rows).each do |row|
x_columns.each do |column|
xy_x_taps << csv_data[row][column].to_f.abs
end
y_columns.each do |column|
xy_y_taps << csv_data[row][column].to_f.abs
end
z_columns.each do |column|
xy_z_taps << csv_data[row][column].to_f.abs
end
end
wafer = csv_file.split('/').last.split('.').first
"#{wafer},#{csv_mod_time},#{tap_count},#{z_x_taps.max},#{z_x_taps.min},#{z_y_taps.max},#{z_y_taps.min}," \
"#{z_z_taps.max},#{z_z_taps.min},#{xy_x_taps.max},#{xy_x_taps.min},#{xy_y_taps.max}," \
"#{xy_y_taps.min},#{xy_z_taps.max},#{xy_z_taps.min},"
end
def pull_measurement_data_from_csv(csv_file)
csv_data = CSV.read(csv_file)
#Measurement Data - no ranges, just individual values
meas2_x_max = csv_data[27][10]
meas2_x_rms = csv_data[27][11]
meas2_x_area = csv_data[27][12]
meas3_x_max = csv_data[27][19]
meas3_x_rms = csv_data[27][20]
meas3_x_area = csv_data[27][21]
meas2_y_max = csv_data[28][13]
meas2_y_rms = csv_data[28][14]
meas2_y_area = csv_data[28][15]
meas3_y_max = csv_data[28][22]
meas3_y_rms = csv_data[28][23]
meas3_y_area = csv_data[28][24]
meas2_z_max = csv_data[29][16]
meas2_z_rms = csv_data[29][17]
meas2_z_area = csv_data[29][18]
meas3_z_max = csv_data[29][25]
meas3_z_rms = csv_data[29][26]
meas3_z_area = csv_data[29][27]
"#{meas2_x_max},#{meas2_x_rms}," \
"#{meas2_x_area},#{meas3_x_max},#{meas3_x_rms},#{meas3_x_area},#{meas2_y_max}," \
"#{meas2_y_rms},#{meas2_y_area},#{meas3_y_max},#{meas3_y_rms},#{meas3_y_area}," \
"#{meas2_z_max},#{meas2_z_rms},#{meas2_z_area},#{meas3_z_max},#{meas3_z_rms},#{meas3_z_area},\n"
end
@rpond-pa
Copy link

Some unsolicited suggestions:

Instead of csv_data[29][27] use constants to reference field names.

ADDRESS_FIELD=30
csv_data[ADDRESS_FIELD]

Later, it will be easier to read.

@rpond-pa
Copy link

Turn this into two methods. The numbers 24 and 27 are specific. But counting taps is generic. Break them out.

def tap_count(csv_file)
  # could put additional csv processing in here - currently just confirming tap count. 
  csv_data = CSV.read(csv_file)

  # count taps by counting how many of the first column are digits
  tap_count = 0
  csv_data.each_index do |row|
    tap_count += 1 if csv_data[row][0] =~ /\d+/
  end
  tap_count
end 

count = tap_count(input_csv_file)
if [27,24].include?(count)
  # good!
else
  # bad

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment