# HG changeset patch # User Steve Losh # Date 1251506410 14400 # Node ID 5bfd01d9c28fe50c91623237e4768b2d83b7c9a0 # Parent 39710f3be8cf096abb1f6d1664faf57a93aca230 Refactor out the task-getting into getitem. diff -r 39710f3be8cf -r 5bfd01d9c28f t.py --- 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."""