以不同格式同步任务列表的数据结构



我正在开发一个python程序,以在不同的待办事项格式之间同步任务 - 从本质上讲是emacs org-mode and todo.txt。我不确定我应该用哪种数据结构以集中式形式跟踪任务(或者这是否是最佳方法)。

我的第一个尝试是创建每个任务属性的字典,其中密钥是任务列表中的原始行,该值是相关属性的字符串。例如,我有以下字典:

#org-mode format
task_name["TODO [#C] Take out the trash"] = "Take out the trash" 
priority["TODO [#C] Take out the trash"] = "C"
#todo.txt format
effort["(A) Refill Starbucks card @10min"] = 20 # meaning 20 minutes of estimated time

i然后检查两个文本文件中的哪个已更新,从最新文件中拉更改任务,然后在旧文件上覆盖这些任务。来自两个列表的新任务都添加到其他列表中。这些任务也都存储在集中式文件中:CSV/TAB分隔的值文件,其中标题是任务的属性(task_name,努力,优先级,截止日期,decenduled_date,todo_state,tags,tags等),以及每行是一项任务。

然后我想到,也许我应该创建一个名为"任务"的对象类,其中每个属性是一个属性,每个任务是任务对象的实例,而不是一系列词典。

class Task(object):
    def __init__(self, name, effort, priority):
        name = self.name
        effort = self.effort
        priority = self.priority

最后,我可能想使用嵌套词典或json格式 - 像这样的东西:

{line: "TODO [#C] Take out the trash" {
        "task_name": "Take out the trash."
        "priority": "C"
        "todo_state": "TODO"
        }}

,或者我可以将任务放在SQLite数据库中。

哪种方法是最好的,还是还有另一种比所有方法都更好的方法?我是一个中级Python开发人员,在高级数据结构和课程方面很少经验,因此我感谢您提供的任何帮助。

优先队列作为数据结构应非常适合这种情况。在Python中至少有两种实施方法。

第一个基于堆数据结构,可以描述为


    pq = []                         # list of entries arranged in a heap
    entry_finder = {}               # mapping of tasks to entries
    REMOVED = ''      # placeholder for a removed task
    counter = itertools.count()     # unique sequence count
    def add_task(task, priority=0):
        'Add a new task or update the priority of an existing task'
        if task in entry_finder:
            remove_task(task)
        count = next(counter)
        entry = [priority, count, task]
        entry_finder[task] = entry
        heappush(pq, entry)
    def remove_task(task):
        'Mark an existing task as REMOVED.  Raise KeyError if not found.'
        entry = entry_finder.pop(task)
        entry[-1] = REMOVED
    def pop_task():
        'Remove and return the lowest priority task. Raise KeyError if empty.'
        while pq:
            priority, count, task = heappop(pq)
            if task is not REMOVED:
                del entry_finder[task]
                return task
        raise KeyError('pop from an empty priority queue')

从这里取。

第二种方法是在Python 3中使用Python 2中的队列模块。该模块包含一个可以满足您要求的类优先级。

第一个可以被认为是更简单的和灵活的修改,但是由于Python中的线程支持,第二个可能在螺纹编程中特别有用。

最新更新