Python Scripting: The To Do List

And they asked him, “Are you the one who is to come, or shall we look for another?” And Jesus answered them, “Go and tell John what you hear and see: the blind receive their sight and the lame walk, lepers are cleansed and the deaf hear, and the dead are raised up, and the poor have good news preached to them. Blessed is the one who is not offended by me.”

Matt 11:3-6

I'm a teacher by vocation and gifting; I love to teach. By corollary, I love to learn. I'm curious about everything and I have a blast learning about almost anything. Over the years, I've picked up quite a bit of information through the Internet. This website is my simple attempt to pay back; to contribute to the community and try to make the interwebs a better place.

As such, I don't require registration. I don't want your personal information. I don't set cookies. I won't put up advertisements. If I mention a product or service, it's because I use it, benefit from it, and I think others would too. I don't need, or want, your money. I simply want to offer some information to other curious characters.

I'm writing this site as an experiment. It's all written in a text editor. I make my own graphics, write my own JavaScript, CSS, PHP, and HTML from scratch because I'm trying to learn more about how all these pieces fit together. I use TextWrangler because I think it's a great text editor and it does everything I want in a very simple and efficient way.

If you want to contact me, try FaceBook or Twitter.

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

#!/usr/local/bin/python3.2 # -*- coding: utf-8 import os, sys

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[1] 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.

choice = sys.argv[1] if choice.lower == 'd': # delete an item num = eval(sys.argv[2]) delete(num) elif choice.lower == 'i': # insert an item at a location num = eval(sys.argv[2]) item = sys.argv[3] insert(num, item) elif choice.lower == 'r': # renumber the list renum() elif choice.lower == 'm': # move an item to a different location num1 = eval(sys.argv[2]) num2 = eval(sys.argv[3]) move(num1, num2) elif choice.lower == 'a': item = sys.argv[2] add(item) else:

Now we'll take a look at each of the functions beginning with add, which is used to add items to the list:

def add(item): file = open('/pathToFile/todo.txt', 'r') list = file.read() file.close list = list + '\n- ' + item file = open('/pathToFile/todo.txt', 'w') file.write(list) file.close renum()

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.

def renum(): file = open('/pathToFile/todo.txt', 'r') temp = file.read() file.close list = temp.split('\n') ct = len(list) file = open('/pathToFile/todo.txt', 'w') for l in range(ct): file.write(str(l+1) + ' - ' + list[l][list[l].find('-')+2:]) if l != ct-1: file.write('\n') file.close

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.


All content and graphics copyright (c) 2012-2017 Brian Dentler - all rights reserved.