아래 예시는 “오늘 내 기분은 꿀꿀해요 🐷😩”를 요즘 힙스터 개발자가 작성했을 법한 감성으로 표현한 Python 스크립트입니다.
(터미널에서 컬러풀하게 출력되므로 pip install rich 로 한 번만 설치해두면 좋아요!)
python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
📅 2024-06-XX
👾 author : @byte_bard
🎯 purpose: log & broadcast that I'm officially feeling *meh* today.
"""
from __future__ import annotations
import asyncio
import datetime as dt
from dataclasses import dataclass, asdict
from pathlib import Path
from random import choice
# ┌────────────┐
# │ dependencies│
# └────────────┘
try:
from rich.console import Console
from rich.panel import Panel
from rich.table import Table
from rich.theme import Theme
except ImportError as e: # noqa: E722
raise SystemExit(
"🚨 'rich' 라이브러리가 필요해요!\n"
" 👉 pip install rich\n"
) from e
# ┌──────────────────┐
# │ custom aesthetics│
# └──────────────────┘
palette = Theme(
{
"title": "bold magenta",
"sad": "bold #787878",
"accent": "#ffafcc",
"info": "italic #6c6f7f",
"timestamp": "#999999",
}
)
console = Console(theme=palette)
# ┌─────────┐
# │ models │
# └─────────┘
@dataclass
class MoodLog:
timestamp: dt.datetime
mood: str
vibe_emoji: str
hash_tags: tuple[str, ...] = ("#lowkey", "#moody")
def to_row(self) -> tuple[str, str, str]:
return (
self.timestamp.strftime("%Y-%m-%d %H:%M"),
f"[sad]{self.mood} {self.vibe_emoji}[/]",
" ".join(self.hash_tags),
)
async def persist(self, file: Path = Path("mood_log.json")) -> None:
"""Quick-n-dirty local log (append-only JSON-Lines)."""
import json
file.touch(exist_ok=True)
async with asyncio.to_thread(open, file, "a", encoding="utf-8") as f:
json_line = json.dumps(asdict(self), ensure_ascii=False)
await asyncio.to_thread(f.write, json_line + "\n")
# ┌──────────────────┐
# │ core async logic │
# └──────────────────┘
async def main() -> None:
mood_options = [
("꿀꿀", "🐷"),
("down", "😞"),
("meh", "😩"),
("우울", "🌧️"),
]
today_mood, emoticon = choice(mood_options)
log = MoodLog(
timestamp=dt.datetime.now(),
mood=today_mood,
vibe_emoji=emoticon,
)
# 1) Store the feeling 📓
await log.persist()
# 2) Display it with ~vibes~
table = Table(show_header=True, header_style="accent")
table.add_column("🕒 when", style="timestamp", no_wrap=True)
table.add_column("🪫 mood")
table.add_column("🏷️ tags")
table.add_row(*log.to_row())
banner = Panel(
table,
title="[title]today's vibe[/title]",
subtitle="CTRL+C to bounce",
border_style="accent",
)
console.print(banner)
if __name__ == "__main__":
asyncio.run(main())