From 681fa6514c259f554043e94aafe6f90b26d70efd Mon Sep 17 00:00:00 2001 From: Noelle Leigh Date: Sun, 1 Jun 2025 22:21:10 -0400 Subject: [PATCH] validate_car: Don't reassign input variable This makes it easier to track what's happening in the function --- tilde-train.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/tilde-train.py b/tilde-train.py index ea12c90..d248aa8 100755 --- a/tilde-train.py +++ b/tilde-train.py @@ -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():