Repository navigation
To-do manager: what happens to a task name that contains |?
#13
|
In file.write(f"{item['task']}|{item['done']}
")and |
Replies: 1 comment
|
Good edge-case hunting — as written, a task name containing Two fixes, in increasing order of "right": 1. Split from the right, not the left. The status is always the last field, and it's only ever the literal task_name, done_status = line.rstrip("\n").rsplit("|", 1)
todo_list.append({"task": task_name, "done": done_status == "True"})This survives any number of 2. Graduate to JSON. The file is structured data with types — that's what import json
def save_tasks():
with open("tasks.txt", "w") as f:
json.dump(todo_list, f)
def load_tasks():
try:
with open("tasks.txt") as f:
return json.load(f)
except FileNotFoundError:
return []Names, quotes, pipes, unicode, everything round-trips. For the learning arc of this repo, |
Good edge-case hunting — as written, a task name containing
|silently loses the task.save_tasks()writes"pay bob|True"aspay bob|True(with the embedded bar), butload_tasks()splits on every|, gets 3 parts, fails thelen(parts) == 2check, and drops the line without a word. Data loss with no warning is the worst failure mode here.Two fixes, in increasing order of "right":
1. Split from the right, not the left. The status is always the last field, and it's only ever the literal
True/False, so the separator is unambiguous if you anchor it:This survives any n…