March 18, 2012
If you've been following along with my to do list series then you know that this is, in my opinion, the best to do list around. At this point I've been using this for about a year and I'm still very pleased with it. This list is text based so you can edit it in any text editor, it's available anywhere you can install and sync with dropbox (or something similar), and it gets a live update on your desktop. Add the Python script we're going to write today and you get a list that is also very easy to manage.
In case you haven't been following along, here are all the pages leading up to this point:
The script that we are going to write takes input from the command line and passes it to a series of functions that read, modify, and rewrite the todo.txt file. The first three lines of the script are to make the script run using the Python update, interpret the text encoding as UTF-8, and to import the needed modules
Next we write the main program. The purpose of this part of the program is to sort out the input passed through the command line and decide what to do with it. sys.argv returns the first argument passed through the command line (arguments are parsed by white space). The choice is made by the if statement and the appropriate function is called. If more information is necessary for the function, sys.argv is used to get additional parameters passed from the command line and to send that information to the function.
Now we'll take a look at each of the functions beginning with add, which is used to add items to the list:
The add function passes the parameter "item" (the to do item) to the function. The function opens the text file, reads it into the variable list, and then closes the text file. I keep my todo.txt file in my dropbox folder to keep it up-to-date across all my devices. You need to change the path to find your own todo.txt file. Note that you cannot use a tilde (~) as a shortcut in python. Next, we concatenate a new line character "\n", a dash, a space, and the "item" that was passed to the function. Now we write the updated list to the disk. Open the todo.txt file, write the list and then close the file. As a last step, we renumber the list by using the renum() function. Since we use this function frequently, I'll include it here.
You will notice some similarities to the add function. As before, we read the file into a variable. This time we use the variable temp because we need to split the string at each new line. List is then an array variable storing a to do item at each index. Then we parse through the list to find the first dash in each item, remove anything prior to the dash and then prepend the number. If the item already has a number it gets a new number given in order from the beginning. In the same way, if the item has no number it gets a number given in order from the beginning. This effectively renumbers the list regardless if an item was numbered or not, and if it was in order or not. This makes our work with other functions easy: we simply do what we need to do (delete, insert, etc.) and then call the renum function to set things straight.