Usage Examples — pytodotxt documentation

# Usage Examples

## Adding a task to a todo.txt file

     1"""Example how to add a task to an existing todo.txt file"""
     2import pytodotxt
     3
     4
     5todotxt = pytodotxt.TodoTxt("todo.txt")
     6todotxt.parse()
     7
     8new_task = pytodotxt.Task("(A) Do some more stuff")
     9todotxt.add(new_task)
    10
    11todotxt.save()

Instead of calling `todotxt.add`, you could of course also just call `todotxt.tasks.append`, but `add` will conveniently update `new_task.todotxt` and `linenr`.

## Mark all tasks of a file as completed

     1"""How to mark tasks as completed"""
     2import datetime
     3
     4import pytodotxt
     5
     6
     7todotxt = pytodotxt.TodoTxt("todo.txt")
     8todotxt.parse()
     9
    10for task in todotxt.parse():
    11    if task.is_completed:
    12        continue
    13
    14    task.completion_date = datetime.date.today()
    15    task.is_completed = True
    16
    17todotxt.save()

## Have your own custom Task subclass

     1"""How to customize your task class"""
     2import pytodotxt
     3
     4
     5class MyTask(pytodotxt.Task): pass
     6
     7
     8todotxt = pytodotxt.TodoTxt("todo.txt",
     9                            parser=pytodotxt.TodoTxtParser(task_type=MyTask))
    10
    11for task in todotxt.parse():
    12    assert isinstance(task, MyTask)