Compare commits

...

2 Commits

Author SHA1 Message Date
73cdf060b7
Add type hints to function signatures
For better code intelligence
2025-06-01 22:01:45 -04:00
8de77c5c21
Use pathlib in test_user_car 2025-06-01 22:01:45 -04:00

View File

@ -43,6 +43,7 @@ from signal import signal, SIGINT
import time ## allowing the loop steps of train animation to be slowed import time ## allowing the loop steps of train animation to be slowed
import string ## for input validation import string ## for input validation
from inspect import cleandoc from inspect import cleandoc
from pathlib import Path
traincarFN = ".choochoo" traincarFN = ".choochoo"
max_x = 35 ## max length of train car. max_x = 35 ## max length of train car.
@ -122,16 +123,15 @@ def print_help():
def test_user_car(): def test_user_car():
username = getpass.getuser() fname = Path.home() / traincarFN
fname = "/home/" + username + "/" + traincarFN
try: try:
myfile = open(fname, 'r') choochoo_string = fname.read_text("utf-8")
except: except OSError as err:
print("ERROR: Couldn't open " + fname) raise OSError(
print("Either it doesn't exist, or is not readble by the tilde.train script.") f"Couldn't open {fname}\n"
sys.exit(1) "Either it doesn't exist, or is not readble by the tilde.train script."
) from err
choochoo_string = myfile.read()
choochoo_list = choochoo_string.split("\n") choochoo_list = choochoo_string.split("\n")
car = "\n".join(choochoo_list) car = "\n".join(choochoo_list)
@ -177,27 +177,24 @@ def test_user_car():
if train_height > max_y+1: if train_height > max_y+1:
print("FAIL. Your train car is too tall.") print("FAIL. Your train car is too tall.")
print("It should be no taller than " + str(max_y) + " lines in height.") print("It should be no taller than " + str(max_y) + " lines in height.")
myfile.close()
sys.exit(1) sys.exit(1)
if train_length > max_x: if train_length > max_x:
print("FAIL. Your train car is too long.") print("FAIL. Your train car is too long.")
print("It should be no longer than " + str(max_x) + " characters in length.") print("It should be no longer than " + str(max_x) + " characters in length.")
myfile.close()
sys.exit(1) sys.exit(1)
print("PASS. Your train car will work on the tilde.town tracks! :)") print("PASS. Your train car will work on the tilde.town tracks! :)")
myfile.close()
sys.exit() sys.exit()
def link_car(car): def link_car(car: list[str]):
for idx,row in enumerate(car): for idx,row in enumerate(car):
car[idx] = " " + row car[idx] = " " + row
car[len(car)-3] = "+" + car[len(car)-3][1:] car[len(car)-3] = "+" + car[len(car)-3][1:]
car[len(car)-2] = "+" + car[len(car)-2][1:] car[len(car)-2] = "+" + car[len(car)-2][1:]
return car return car
def validate_car(car): def validate_car(car: list[str]):
## this function (1) checks that a train car isn't too tall or too long ## this function (1) checks that a train car isn't too tall or too long
## (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.
@ -260,7 +257,7 @@ def print_all_cars():
# print "Cannot open " + fname # for debuggering purposes # print "Cannot open " + fname # for debuggering purposes
def chuggachugga(stdscr): def chuggachugga(stdscr: curses.window):
curses.curs_set(0) curses.curs_set(0)
h, w = stdscr.getmaxyx() h, w = stdscr.getmaxyx()
x_pos = w-1 x_pos = w-1