# high_rack_bay_calculation

Jun 30th, 2020
1,544
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. from collections import namedtuple
2.
3.
4. def calc_bay_pos(
5.     bay_no,
6.     rows,
7.     cols,
8.     bay_start_no=0,
9.     row_start_no=0,
10.     col_start_no=0,
11.     horizontal=True,
12.     to_up=True,
13.     to_left=True,
14.     ):
15.     Row = namedtuple("Bay", "row col")
16.     bay_no -= bay_start_no
17.     if bay_no < 0:
18.         raise ValueError
19.     if bay_no >= rows * cols:
20.         raise ValueError
21.     if horizontal:
22.         col = bay_no % cols
23.         row = bay_no // cols
24.     else:
25.         row = bay_no % rows
26.         col = bay_no // rows
27.     if not to_up:
28.         row = rows - row - 1
29.     if not to_left:
30.         col = cols - col - 1
31.     return Row(row + row_start_no, col + col_start_no)
32.
33.
34. def make_matrix(rows, cols):
35.     return [[0 for _ in range(cols)] for _ in range(rows)]
36.
37.
38. def print_matrix(result):
39.     for row in result:
40.         print(" | ".join(map("{:^3d}".format, row)))
41.
42.
43. rows = 3
44. cols = 5
45. mat = make_matrix(rows, cols)
46. for horizontal in (True, False):
47.     for up in (True, False):
48.         for left in (True, False):
49.             print("=" * 25)
50.             print("Horizontal" if horizontal else "Vertical", end=" ")
51.             print("- Up" if up else "- Down", end=" ")
52.             print("- Left" if left else "- Right")
53.             for bay in range(rows * cols):
54.                 pos = calc_bay_pos(bay, rows, cols, horizontal=horizontal, to_up=up, to_left=left)
55.                 mat[pos.row][pos.col] = bay
56.             print_matrix(mat)
57.             print()
58.
59.
60.
61. def calculate_xy_position(row, col, row_shift, col_shift, row_offset, col_offset):
62.     Position = namedtuple("Position", "x y")
63.     return Position(row * row_shift + row_offset, col * col_shift + col_offset)
64.
65.
66.
67. # calculate_xy_position(1, 0, 100, 100, 50, 50)
RAW Paste Data