Functional Weave
Code in TypeScript

todo.tag-counts@1.0.0

impl/python.py

775 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 Any, Dict, List, Sequence

from .collections_sort_by import sort_by  ← from collections.sort-by ^1.0.0 · built alongside by fune
from .todo_item import Todo  ← from todo.item ^1.0.0 · built alongside by fune
from .todo_tag_counts_types import TagCount


def tag_counts(todos: Sequence[Todo], include_done: bool) -> List[TagCount]:
    """How many todos carry each tag: most used first, then tag in code-point order."""
    counts: Dict[str, int] = {}
    for todo in todos:
        if todo.done and not include_done:
            continue
        for tag in dict.fromkeys(todo.tags):
            counts[tag] = counts.get(tag, 0) + 1
    rows: List[Dict[str, Any]] = [{"tag": tag, "count": count} for tag, count in counts.items()]
    ordered = sort_by(sort_by(rows, "tag", "asc"), "count", "desc")
    return [TagCount(tag=r["tag"], count=r["count"]) for r in ordered]