2246_verso_tests/util/picklist_to_locations.py

74 lines
1.8 KiB
Python
Raw Permalink Normal View History

2024-10-22 21:21:00 +00:00
import json
2024-10-24 18:40:13 +00:00
from sys import argv, exit
from os import path
2024-10-22 21:21:00 +00:00
from dataclasses import dataclass
@dataclass(frozen=True)
class Location:
row: int
col: int
def main():
input_file = argv[1]
output_file = argv[2]
2024-10-24 18:40:13 +00:00
alternate_mode = len(argv) == 4 and argv[3] == "alt"
2024-10-22 21:21:00 +00:00
if not test_file(input_file):
print("Input file does not exist")
2024-10-24 18:40:13 +00:00
exit(1)
2024-10-22 21:21:00 +00:00
if test_file(output_file):
print("Output file already exists")
2024-10-24 18:40:13 +00:00
exit(1)
2024-10-22 21:21:00 +00:00
locations = read_input_csv_to_wells(input_file)
json_obj = tubes_to_tubes_json(locations)
2024-10-24 18:40:13 +00:00
if alternate_mode:
with open(output_file, 'w') as file:
for location in locations:
index = (location.row - 1) * 12 + location.col
file.write(f"{index}\n")
exit(0)
2024-10-22 21:21:00 +00:00
with open(output_file, 'w') as file:
json.dump(json_obj, file)
def read_input_csv_to_wells(p: str) -> [Location]:
with open(p, 'r') as file:
2024-10-24 18:40:13 +00:00
lines=file.readlines()
2024-10-22 21:21:00 +00:00
# Assume 96w format only
def get_first_col(line): return line.split(',')[0]
def get_row(x): return ord(x.upper()) - 64
def get_row_col(line: str) -> Location:
2024-10-24 18:40:13 +00:00
col=get_first_col(line)[1:]
row=get_row(get_first_col(line)[0])
2024-10-22 21:21:00 +00:00
return Location(row, int(col))
return list(set([get_row_col(line) for line in lines[1:]]))
def tubes_to_tubes_json(i: [Location]) -> dict:
2024-10-24 18:40:13 +00:00
target_positions_list=[]
2024-10-22 21:21:00 +00:00
for tube in i:
target_positions_list.append((tube.row - 1) * 12
+ tube.col)
2024-10-24 18:40:13 +00:00
output_obj={}
output_obj["TargetPositions"]=[{"PositionIndex": p}
2024-10-22 21:21:00 +00:00
for p in target_positions_list]
return output_obj
def test_file(p: str) -> bool:
return path.exists(p) and path.isfile(p)
if __name__ == "__main__":
main()