Skip to content

Biostumblematic

A biophysicist teaches himself how to code

First, some news: I’ve been offered a post-doc position! Hopefully this means that I’ll be doing some coding work besides just tinkering around on various random ideas that pop into my head, and instead be focused on solving a Real Life ™ problem.

Anyhow, I used the task manager from yesterday a bit and it’s sloppiness and lack of features bugged me. I re-wrote it a bit to allow for completion of tasks and to split the list preparation away from the task addition/completion functions.

First I needed to add columns to the database. Since it wasn’t important to keep the old one I just deleted it and re-created it

sqlite3 tasks.db
sqlite>create table task_list(task_id integer primary key,
sqlite>task varchar(150),
sqlite>added_on date,
sqlite>completion varchar(5));
sqlite>.quit

This gives each task an auto-incrementing ID and a completion field. It was unclear to me if sqlite supports booleans, and I was too lazy to worry about it so I just used a varchar.

Now the modified python:

#!/usr/bin/env python
# https://biostumblematic.wordpress.com

import sys, string
from pysqlite2 import dbapi2 as sqlite

def taskadd():
    connection = sqlite.connect('/home/jason/scripts/tasks.db')
    cursor = connection.cursor()
    # Creating a new task
    newtask = raw_input('What do you need to do? >> ')
    t = (newtask,)
    cursor.execute('insert into task_list values (NULL, ?, date("now"), "False")', t)
    connection.commit()
    cursor.close()

def taskcomplete():
    connection = sqlite.connect('/home/jason/scripts/tasks.db')
    cursor = connection.cursor()
    completedtask = input('ID of completed task >> ')
    cursor.execute('update task_list set completion = "True" where task_id ='+str(completedtask))
    connection.commit()
    cursor.close
            
def todayToDo():
    connection = sqlite.connect('/home/jason/scripts/tasks.db')
    cursor = connection.cursor()
    # Write today's tasks
    cursor.execute('select * from task_list where completion="False"')
    todaystasks=[]
    for row in cursor:
        todaystasks.append('> '+row[1]+'('+str(row[0])+')\n')
    output = open('todaystasks.txt', 'w')    
    output.writelines(todaystasks)
    output.close()
    cursor.close()

action = 0
while action != 4:        
    print 'What do you want to do?'
    print '1. Add a new task'
    print '2. Complete a task'
    print '3. Prepare the task list'
    print '4. Quit'
    action = input('>> ')
    if action == 1:
        taskadd()
    elif action == 2:
        taskcomplete()
    elif action == 3:
        todayToDo()
else:
    sys.exit()

It’s still not perfect. I have continued to fail to grasp how to pass variables around between functions. I’m sure that I could figure this out by reading some existing code or shoving my nose into python book, but I’m trying to get a grant application out and don’t have a lot of time to fool around (besides writing blog posts, of course).

Anyhow, running the program now presents you with a menu in which you can add tasks, complete them by entering their ID, and prepare the text file listing of all uncompleted tasks. I’ll use this for a bit and maybe tinker with it some more after we send the grant application off (tomorrow)!

Advertisements

Tags: , ,

%d bloggers like this: