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