def create_rows_and_field_passes_by_row_codes(grouped_row_codes, field_direction, rows_per_pass): rows = [] for pass_start_code, pass_end_code in grouped_row_codes: field_start_code, field_end_code = orient_items(pass_start_code, pass_end_code, field_direction) if pass_start_code is field_start_code: row_direction = 'up' else: row_direction = 'back' new_row = Row(field_start_code, field_end_code, direction=row_direction) rows.append(new_row) # Sort row number so can group them into passes. sorted_rows = sorted(rows, key=lambda row: row.number) # Current pass number to assign rows to. Passes are indexed off 1. current_pass_num = 1 field_passes = defaultdict(list) for row_idx, row in enumerate(sorted_rows): if row_idx == 0: current_direction = row.direction num_rows_in_current_pass = len(field_passes[current_pass_num]) end_of_pass = (row.direction != current_direction) or (num_rows_in_current_pass >= rows_per_pass) if end_of_pass: if num_rows_in_current_pass < rows_per_pass: print 'WARNING - pass {} only has {} rows'.format(current_pass_num, num_rows_in_current_pass) current_pass_num += 1 field_passes[current_pass_num].append(row) current_direction = row.direction # Convert field passes to a list and make sure it's sorted. sorted_field_passes = [] for sorted_pass_num in sorted(field_passes.keys()): sorted_field_passes.append(field_passes[sorted_pass_num]) field_passes = sorted_field_passes return rows, field_passes
def create_rows(grouped_row_codes, field_direction): rows = [] for row_number, codes in grouped_row_codes.iteritems(): if len(codes) == 1: print "Only found 1 code for row {}".format(row_number) elif len(codes) > 2: print "Found {} codes for row {} with positions: ".format(len(codes), row_number) for code in codes: print "\t{}".format(code.position) else: # Create row objects with start/end codes. code1, code2 = codes start_code, end_code = orient_items(code1, code2, field_direction) if start_code and end_code: rows.append(Row(start_code, end_code)) return rows
def create_rows_and_field_passes_by_pass_codes(grouped_row_codes, field_direction): rows = [] field_passes = defaultdict(list) for pass_start_code, pass_end_code in grouped_row_codes: field_start_code, field_end_code = orient_items(pass_start_code, pass_end_code, field_direction) if pass_start_code is field_start_code: row_direction = 'up' else: row_direction = 'back' new_row = Row(field_start_code, field_end_code, direction=row_direction) rows.append(new_row) # At this point row numbers should be pass numbers so can use them to group rows into passes. field_passes[new_row.number].append(new_row) # Convert pass number on codes to row numbers pass_side = pass_start_code.name[-1].lower() pass_start_code.assigned_row = pass_start_code.row * 2 - 1 pass_end_code.assigned_row = pass_end_code.row * 2 - 1 if (pass_side == 'r' and row_direction == 'up') or (pass_side == 'l' and row_direction == 'back'): pass_start_code.assigned_row += 1 pass_end_code.assigned_row += 1 sorted_field_passes = [] for sorted_pass_num in sorted(field_passes.keys()): rows_in_pass = field_passes[sorted_pass_num] if len(rows_in_pass) == 1: print "Only 1 row found in pass {}".format(sorted_pass_num) elif len(rows_in_pass) > 2: print "Error: more than 2 rows in pass {}".format(sorted_pass_num) sys.exit(-1) sorted_field_passes.append(rows_in_pass) field_passes = sorted_field_passes return rows, field_passes