Use rich for table output; add uv project setup

- Replace manual column-width formatting with rich.Table
- Add pyproject.toml, uv.lock for dependency management
- Add .python-version (3.11) and .envrc for direnv/uv auto-setup
- Add main.py scaffold

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-24 20:34:19 +08:00
parent 929b7a4cec
commit e10563b405
6 changed files with 267 additions and 10 deletions

View File

@@ -13,8 +13,10 @@ from datetime import datetime
try:
import requests
from bs4 import BeautifulSoup
from rich.console import Console
from rich.table import Table
except ImportError:
print("請先安裝依賴套件: pip install requests beautifulsoup4", file=sys.stderr)
print("請先安裝依賴套件: pip install requests beautifulsoup4 rich", file=sys.stderr)
sys.exit(1)
URL = "https://www.cpc.com.tw/historyprice.aspx?n=2890"
@@ -74,20 +76,17 @@ def print_table(records: list[dict], use_ad: bool) -> None:
return
headers = list(records[0].keys())
col_widths = [max(len(h), max(len(r[h]) for r in records)) + 2 for h in headers]
table = Table(show_header=True, header_style="bold")
for h in headers:
table.add_column(h)
def fmt_row(values):
return " ".join(v.ljust(w) for v, w in zip(values, col_widths))
separator = " ".join("-" * w for w in col_widths)
print(fmt_row(headers))
print(separator)
for r in records:
row = list(r.values())
if use_ad:
row[0] = roc_to_ad(row[0])
print(fmt_row(row))
table.add_row(*row)
Console().print(table)
def print_csv(records: list[dict], use_ad: bool) -> None: