Functional Weave
Code in TypeScript

todo.summary@1.0.0

impl/python.py

1,074 bytes · the Python implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

from typing import Sequence

from .dates_day_of_week import day_of_week  ← from dates.day-of-week ^1.0.0 · built alongside by fune
from .math_round_div import round_div  ← from math.round-div ^1.0.0 · built alongside by fune
from .todo_due_status import due_status  ← from todo.due-status ^1.0.0 · built alongside by fune
from .todo_item import Todo  ← from todo.item ^1.0.0 · built alongside by fune
from .todo_summary_types import TodoSummary


def summarise_todos(todos: Sequence[Todo], today: str) -> TodoSummary:
    """Counts for a todo list; percent_done rounds down so 100 means all done."""
    day_of_week(today)  # a bad today is an error even for an empty list
    done = 0
    overdue = 0
    due_today = 0
    for todo in todos:
        status = due_status(todo, today)
        if todo.done:
            done += 1
        if status == "overdue":
            overdue += 1
        if status == "today":
            due_today += 1
    total = len(todos)
    active = total - done
    return TodoSummary(
        total=total,
        active=active,
        done=done,
        overdue=overdue,
        due_today=due_today,
        percent_done=0 if total == 0 else round_div(done * 100, total, "down"),
        items_left="1 item left" if active == 1 else "%d items left" % active,
    )