We don't have a very useful task manager yet, for a few reasons. We can only interact with it by running a file that lives in one place as a Python script. We can't add or delete anything. Etc.
These are related, more intimately than you might think, to a few general principles of software engineering:
- Try to disentangle the data itself from the code that operates on that data.
- It's very hard to design good interfaces to data.
Let's start addressing these! The first step is to take the tasks that are currently listed explicitly in our
tasks object in
main.py and put them in a separate text file. This latter file will be a plain old text file: we'll call it
task_list.txt, but if you call it something else that's fine too. For now, it will be our "database."
(Martin Kleppmann's wonderful Designing Data-Intensive Applications also begins by contemplating the use of a plain text file as a database. If you want to learn a lot more about to think about computers storing data, that's the book to check out. As this project aspires to be, the book is advanced but requires remarkably little prior knowledge.)
So! Your text file should look like this:
...and we'll correspondingly modify the
tasks list in
main.py to read from that "database" instead of holding the strings itself from the start:
There's a lot of new Python stuff in that code block!
- We now begin with
tasksbeing an empty list.
- You don't need to understand the details of what's going on in the second line (it's a context manager, and it's great to know what a context manager is, but roughly 1% of working professional Python programmers can describe in any detail the mechanics of a context manager). For now, you can think of it as an incantation that opens a file named
task_list.txtand gives you a "file handler" named
fthat you can manipulate in the indented block right below that line.
f.readlines()gives you the lines in the file one by one; as you "iterated over" the
taskslist in the previous version of this file, you can "iterate over" the lines in the file, assigning them to the
task_linevariable one by one and executing whatever code is in the indented block below that.
- So, in particular, you can have indented blocks inside of indented blocks, representing nested levels of execution of the code.
tasksis a list, and lists are "mutable": you can change what they refer to. And, in particular, you can append things to them.
.strip()is a method you can call on a string to take the newline off the end of it. (When
task_linethe lines in the file one-by-one, each of those lines includes a character that your on-screen text editor represents by breaking that line off and starting a new one. The task I want to describe is
Move shelves, not
Move shelves\n, so I ask Python to strip off the
Again: If this is all new to you, you are not supposed to understand this just by reading it once. I'm going through it quickly to give you a clear sense of what to read elsewhere, not because you should be able to know what's going on by just reading this post. Many many people have explained these things elsewhere, and you can Google for those other people's explanations.
And, again: if this is all very familiar to you, just keep on reading.
Our directory should now look like:
And you should still get the same output if you run
main.py (see the previous post).