0

I am developing a Python program to sync tasks between lists in different to-do formats--initially Emacs org-mode and todo.txt. I am unsure what data structure I should use to track the tasks in a centralized form (or whether this is even the best approach).

My first attempt was to create dictionaries of each task property in which the key is the original line from the task list, and the value is a string of the relevant property. For example, I had dictionaries for the following:

#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 then check which of the two text files was updated most recently, pull changed tasks from the most recent file, and overwrite these tasks on the old file. New tasks from either list are added to the other list. The tasks are also all stored in a centralized file: a CSV / tab-separated value file in which the headers are properties of the task (task_name, effort, priority, deadline, scheduled_date, todo_state, tags, etc.), and each row is a task.

It then occurred to me that maybe I should instead create an object class called "Task" in which each property is an attribute, and each task is an instance of the Task object instead of a series of dictionaries.

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

Finally, it occurred to me that I might want to use nested dictionaries or JSON formats--something like this:

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

Or I could put the tasks in an SQLite database.

Which approach is best, or is there another approach that is better than all of these? I am an intermediate Python developer with little experience with advanced data structures and classes, so I appreciate any help you can offer.

1 Answer 1

1

The priority queue as a data structure should fit well for this case. There are at least two ways to implement it in Python.

The first one is based on the Heap data structure and could be described as



    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')

that is taken from here.

The second way is to use a Queue module in Python 2, which is queue in Python 3. This module contains a class PriorityQueue that can satisfy your requirements.

The first one may be considered as a bit more straightforward and flexible for modifications, but the second one may be especially useful in threaded programming due to thread support in Python.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.