Refactor out the task-getting into getitem.
author |
Steve Losh <steve@stevelosh.com> |
date |
Fri, 28 Aug 2009 20:40:10 -0400 |
parents |
39710f3be8cf
|
children |
be695300145b
|
branches/tags |
(none) |
files |
t.py |
Changes
--- a/t.py Fri Aug 28 20:32:18 2009 -0400
+++ b/t.py Fri Aug 28 20:40:10 2009 -0400
@@ -88,6 +88,19 @@
for task in tasks:
getattr(self, kind)[task['id']] = task
+ def __getitem__(self, prefix):
+ """Return the unfinished task with the given prefix.
+
+ If more than one task matches the prefix an AmbiguousPrefix exception
+ will be raised.
+
+ """
+ matched = filter(lambda tid: tid.startswith(prefix), self.tasks.keys())
+ if len(matched) == 1:
+ return self.tasks[matched[0]]
+ else:
+ raise AmbiguousPrefix
+
def add_task(self, text):
"""Add a new, unfinished task with the given summary text."""
task_id = _hash(text)
@@ -111,12 +124,7 @@
will be raised.
"""
- matched = filter(lambda tid: tid.startswith(prefix), self.tasks.keys())
- if len(matched) == 1:
- task = self.tasks.pop(matched[0])
- self.done[task['id']] = task
- else:
- raise AmbiguousPrefix
+ self.tasks.pop(self[prefix]['id'])
def delete_finished(self):
"""Remove all finished tasks."""