Python 3.13: Fix and polish #1

Merged
vilmibm merged 15 commits from noelle/tilde-train:python-3.13 into trunk 2025-06-02 22:23:23 +00:00
Showing only changes of commit 681fa6514c - Show all commits

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