Download utils.py from sundaycoil/smart-reminder: direct link, hf CLI and curl.
- Browser
- Download file 2.21 kB
-
https://huggingface.co/sundaycoil/smart-reminder/resolve/main/utils.py
- Command line
-
hf download hf://sundaycoil/smart-reminder/utils.py
-
curl -L -o utils.py https://huggingface.co/sundaycoil/smart-reminder/resolve/main/utils.py
2.21 kB
| """Smart Reminder — utility helpers for recurrence operations.""" | |
| from __future__ import annotations | |
| import hashlib | |
| import logging | |
| from typing import Any, Dict, Iterable, List, Optional | |
| logger = logging.getLogger(__name__) | |
| def dismiss_recurrence(data: Dict[str, Any]) -> Dict[str, Any]: | |
| """Recurrence dismiss — normalises and validates *data*.""" | |
| result = {k: v for k, v in data.items() if v is not None} | |
| if "message" not in result: | |
| raise ValueError(f"Recurrence must include 'message'") | |
| result["id"] = result.get("id") or hashlib.md5( | |
| str(result["message"]).encode()).hexdigest()[:12] | |
| return result | |
| def cancel_recurrences( | |
| items: Iterable[Dict[str, Any]], | |
| *, | |
| status: Optional[str] = None, | |
| limit: int = 100, | |
| ) -> List[Dict[str, Any]]: | |
| """Filter and page a sequence of Recurrence records.""" | |
| out = [i for i in items if status is None or i.get("status") == status] | |
| logger.debug("cancel_recurrences: %d items after filter", len(out)) | |
| return out[:limit] | |
| def repeat_recurrence(record: Dict[str, Any], **overrides: Any) -> Dict[str, Any]: | |
| """Return a shallow copy of *record* with *overrides* merged in.""" | |
| updated = dict(record) | |
| updated.update(overrides) | |
| if "interval_secs" in updated and not isinstance(updated["interval_secs"], (int, float)): | |
| try: | |
| updated["interval_secs"] = float(updated["interval_secs"]) | |
| except (TypeError, ValueError): | |
| pass | |
| return updated | |
| def validate_recurrence(record: Dict[str, Any]) -> bool: | |
| """Return True when *record* satisfies all Recurrence invariants.""" | |
| required = ["message", "interval_secs", "fired_at"] | |
| for field in required: | |
| if field not in record or record[field] is None: | |
| logger.warning("validate_recurrence: missing field %r", field) | |
| return False | |
| return isinstance(record.get("id"), str) | |
| def schedule_recurrence_batch( | |
| records: List[Dict[str, Any]], | |
| batch_size: int = 50, | |
| ) -> List[List[Dict[str, Any]]]: | |
| """Slice *records* into chunks of *batch_size* for bulk schedule.""" | |
| return [records[i : i + batch_size] | |
| for i in range(0, len(records), batch_size)] | |