Python 3.13: Fix and polish #1
@ -90,6 +90,9 @@ default_car = [
|
|||||||
r" \__/ \__/ \__/ \__/ ",
|
r" \__/ \__/ \__/ \__/ ",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
class CarError(Exception):
|
||||||
|
"""Error related validating a car."""
|
||||||
|
|
||||||
def print_help():
|
def print_help():
|
||||||
print(
|
print(
|
||||||
cleandoc(
|
cleandoc(
|
||||||
@ -206,9 +209,11 @@ def validate_car(car: list[str]):
|
|||||||
while car_list[(len(car_list)-1)].strip() == "":
|
while car_list[(len(car_list)-1)].strip() == "":
|
||||||
car_list.pop() # clear bottom
|
car_list.pop() # clear bottom
|
||||||
|
|
||||||
# len(choochoo_list) is the height of the train car, in number of rows.
|
# len(car_list) is the height of the train car, in number of rows.
|
||||||
if len(car_list) > max_y+1 or len(car_list) == 0:
|
if len(car_list) > max_y+1:
|
||||||
return 0 # train car too tall or non-existant; skip it.
|
raise CarError(f"Car is too tall ({len(car_list)} > {max_y + 1}).")
|
||||||
|
if len(car_list) == 0:
|
||||||
|
raise CarError(f"Car is empty.")
|
||||||
|
|
||||||
# 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_list) < max_y:
|
while len(car_list) < max_y:
|
||||||
@ -221,9 +226,9 @@ def validate_car(car: list[str]):
|
|||||||
|
|
||||||
for idx,row in enumerate(car_list):
|
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.
|
raise CarError(f"Car is too wide ({len(row)} > {max_x + 1}).")
|
||||||
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
|
raise CarError(f"Car contains illegal control characters.")
|
||||||
elif len(row) < longest_line:
|
elif len(row) < longest_line:
|
||||||
padding = " "*(longest_line - len(row))
|
padding = " "*(longest_line - len(row))
|
||||||
car_list[idx] += padding # add padding spaces.
|
car_list[idx] += padding # add padding spaces.
|
||||||
@ -241,13 +246,12 @@ def print_all_cars():
|
|||||||
continue # the train car was too tall; skip it.
|
continue # the train car was too tall; skip it.
|
||||||
|
|
||||||
car = validate_car(choochoo_list) # printing is only a DEBUG feature.
|
car = validate_car(choochoo_list) # printing is only a DEBUG feature.
|
||||||
if car != 0:
|
print("")
|
||||||
print("")
|
print(fname + ":")
|
||||||
print(fname + ":")
|
print("\n".join(car)) # print the car to stdout
|
||||||
print("\n".join(car)) # print the car to stdout
|
|
||||||
|
|
||||||
except:
|
except:
|
||||||
pass;
|
pass
|
||||||
|
|
||||||
|
|
||||||
def chuggachugga(stdscr: curses.window):
|
def chuggachugga(stdscr: curses.window):
|
||||||
@ -306,11 +310,10 @@ for fname in glob.glob('/home/*/' + traincarFN):
|
|||||||
continue # the train car was too tall; skip it.
|
continue # the train car was too tall; skip it.
|
||||||
|
|
||||||
car = validate_car(choochoo_list) # printing is only a DEBUG feature.
|
car = validate_car(choochoo_list) # printing is only a DEBUG feature.
|
||||||
if car != 0:
|
cars.append(car) # start a list of lists (list of cars) here.
|
||||||
cars.append(car) # start a list of lists (list of cars) here.
|
|
||||||
|
|
||||||
except:
|
except:
|
||||||
pass;
|
pass
|
||||||
|
|
||||||
while len(cars) < max_cars:
|
while len(cars) < max_cars:
|
||||||
cars.append([*default_car]) # add copies of default cars if train too short
|
cars.append([*default_car]) # add copies of default cars if train too short
|
||||||
|
Loading…
x
Reference in New Issue
Block a user