validate_car: Don't reassign input variable

This makes it easier to track what's happening in the function
This commit is contained in:
noelle 2025-06-01 22:21:10 -04:00
parent 56f7c8d278
commit 681fa6514c
Signed by: noelle
SSH Key Fingerprint: SHA256:30qkkOn+Czx9ud36Ekl0NB/y+W4c25oREt+qmasrU9w

View File

@ -198,40 +198,40 @@ def validate_car(car: list[str]):
## (2) pads it vertically or on the right side if it is too short or if
## not all lines are the same length and (3) removes bad characters.
car = "\n".join(car)
car = ''.join([i if ord(i) < 128 else ' ' for i in car])
car = car.split("\n")
car_str = "\n".join(car)
car_str = ''.join([i if ord(i) < 128 else ' ' for i in car_str])
car_list = car_str.split("\n")
## remove blank lines from top and bottom of car,
## so we can estimate its true size.
while car[0].strip() == "":
car.pop(0) ## clear top
while car[(len(car)-1)].strip() == "":
car.pop() ## clear bottom
while car_list[0].strip() == "":
car_list.pop(0) ## clear top
while car_list[(len(car_list)-1)].strip() == "":
car_list.pop() ## clear bottom
## len(choochoo_list) is the height of the train car, in number of rows.
if len(car) > max_y+1 or len(car) == 0:
if len(car_list) > max_y+1 or len(car_list) == 0:
return 0 ## train car too tall or non-existant; skip it.
## vertically pad short cars with 1 space (lines will be lengthened later).
while len(car) < max_y:
car = [" "] + car
while len(car_list) < max_y:
car_list = [" "] + car_list
for idx,row in enumerate(car):
car[idx] = row.rstrip()
for idx,row in enumerate(car_list):
car_list[idx] = row.rstrip()
longest_line = len(max(car, key=len)) ## longest line in .choochoo file.
longest_line = len(max(car_list, key=len)) ## longest line in .choochoo file.
for idx,row in enumerate(car):
for idx,row in enumerate(car_list):
if len(row) > max_x+1: ## check length of each row in .choochoo file.
return 0 ## train car too long; skip it.
elif "\t" in row or "\v" in row or "\f" in row or "\r" in row:
return 0 # skip if contains tabs, vert tabs, line feeds or carriage ret
elif len(row) < longest_line:
padding = " "*(longest_line - len(row))
car[idx] += padding ## add padding spaces.
car_list[idx] += padding ## add padding spaces.
return car
return car_list
def print_all_cars():