Skip to content

Instantly share code, notes, and snippets.

@rhysforyou
Created August 28, 2010 07:25
Show Gist options
  • Save rhysforyou/554848 to your computer and use it in GitHub Desktop.
Save rhysforyou/554848 to your computer and use it in GitHub Desktop.
import datetime
import operator
import csv
class TaskHandler(object):
def __init__( self, *tasks ):
self.tasks = list(tasks)
def __repr__(self):
return "\n".join(map(str, (self.sorted())))
# Returns an array of tasks sorted by due date, then priority, then title
def sorted(self):
return sorted(self.tasks, key=operator.attrgetter('priority', 'due', 'title'))
# Does pretty much the same thing as __repr__ except it numbers each of the
# items (for use in menus and whatnot)
def numbered(self):
output = []
for i, task in enumerate(self.sorted()):
output.append("%2i. %s\n" % (i, task))
return "".join(output)
def extend(self, *tasks):
self.tasks.extend(tasks)
def saveToCSV(self):
writer = csv.writer(open('tasks.csv', 'wb'))
for task in self.tasks:
writer.writerow([task.title, task.description, task.date, task.due, task.priority])
def loadFromCSV(self):
self.tasks = []
reader = csv.reader(open('tasks.csv', 'r'))
for row in reader:
task = Task(row[0])
task.description = row[1]
task.date = datetime.datetime.strptime(row[2], "%Y-%m-%d")
task.due = datetime.datetime.strptime(row[3], "%Y-%m-%d")
task.priority = -int(row[4])
self.extend(task)
# This class provides the basic scaffolding for every task in the program
class Task:
# This provides a simple method of expressing priorites as a string
# We use negative values for priorites for sorting reasons
priorities = { 0 : 'undefined', -1 : 'low', -2 : 'medium', -3 : 'high' }
# After much internal debate, I decided to keep initialisation simple, hence
# it only requiring one parameter. This goes along with the ideal of minimum
# time wastage.
def __init__( self, title ):
self.title = title
self.description = ''
self.date = datetime.date.today()
self.due = datetime.date.today()
self.priority = 0
# This controls what the class returns to a print statement
def __repr__( self ):
return "%s - Due %s, %s priority" % ( self.title, self.due.strftime( "%A, %B %d %Y" ),
self.priorities[self.priority].capitalize() )
tasks = TaskHandler(Task("Error, unable to load tasks"))
tasks.loadFromCSV()
print tasks.numbered()
Feed Dog 2010-08-29 2010-08-30 1
Feed Cat 2010-08-29 2010-08-31 2
Do Homework 2010-08-29 2010-08-30 3
Make Dinner 2010-08-29 2010-08-29 2
Clean Room 2010-08-29 2010-08-28 1
Practice Maths 2010-08-29 2010-08-30 3
Test1 2010-08-29 2010-08-29 0
Test2 2010-08-29 2010-08-29 0
Test3 2010-08-29 2010-08-29 0
Test4 2010-08-29 2010-08-29 0
Test5 2010-08-29 2010-08-29 0
Test6 2010-08-29 2010-08-29 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment