"""小書店的工具函式（M6）。

放在 python-course 資料夾，notebook 裏用 `import bookshop` 取用。
修改這個檔之後，要在 notebook 選 Kernel → Restart Kernel and Run All Cells，改動才會生效。
"""
from pathlib import Path

import yaml


def load_yaml(path):
    """讀入一個 YAML 檔，回傳讀到的 Python 物件。"""
    return yaml.safe_load(Path(path).read_text(encoding="utf-8"))


def by_title(catalog):
    """把目錄（list of dict）變成「書名 → 書」的 dict，方便查找。"""
    return {book["title"]: book for book in catalog}


def format_price(x):
    """把金額顯示成「1,220 元」的樣子（捨入到整數，不顯示小數）。"""
    return f"{x:,.0f} 元"
