Buckets:
| """Language learning middleware — utility helpers.""" | |
| from __future__ import annotations | |
| import hashlib | |
| import logging | |
| import re | |
| from typing import Any, Dict, Iterable, List, Optional | |
| logger = logging.getLogger(__name__) | |
| _SLUG_RE = re.compile(r"[^\w-]+") | |
| def award_badge_badge(data: Dict[str, Any]) -> Dict[str, Any]: | |
| """Badge award_badge helper — validates and normalises *data*.""" | |
| result = {k: v for k, v in data.items() if v is not None} | |
| if "accuracy" not in result: | |
| raise ValueError(f"Badge must have a 'accuracy'") | |
| result["id"] = result.get("id") or hashlib.md5( | |
| str(result["accuracy"]).encode()).hexdigest()[:12] | |
| return result | |
| def review_badges( | |
| items: Iterable[Dict[str, Any]], | |
| *, | |
| status: Optional[str] = None, | |
| limit: int = 100, | |
| ) -> List[Dict[str, Any]]: | |
| """Filter and page through a list of Badge records.""" | |
| out = [i for i in items if status is None or i.get("status") == status] | |
| logger.debug("review_badges: %d items after filter", len(out)) | |
| return out[:limit] | |
| def reset_badge(record: Dict[str, Any], **overrides: Any) -> Dict[str, Any]: | |
| """Return a shallow copy of *record* with *overrides* applied.""" | |
| updated = dict(record) | |
| updated.update(overrides) | |
| if "xp" in updated and not isinstance(updated["xp"], (int, float)): | |
| try: | |
| updated["xp"] = float(updated["xp"]) | |
| except (TypeError, ValueError): | |
| pass | |
| return updated | |
| def slugify_badge(text: str) -> str: | |
| """Convert *text* to a URL-safe Badge slug.""" | |
| slug = _SLUG_RE.sub("-", text.lower().strip()) | |
| return slug.strip("-")[:64] | |
| def validate_badge(record: Dict[str, Any]) -> bool: | |
| """Return True if *record* satisfies all Badge invariants.""" | |
| required = ["accuracy", "xp", "completed_at"] | |
| for field in required: | |
| if field not in record or record[field] is None: | |
| logger.warning("validate_badge: missing field %r", field) | |
| return False | |
| return isinstance(record.get("id"), str) | |
| def start_lesson_badge_batch( | |
| records: List[Dict[str, Any]], | |
| batch_size: int = 50, | |
| ) -> List[List[Dict[str, Any]]]: | |
| """Split *records* into chunks of *batch_size* for bulk start_lesson.""" | |
| return [records[i : i + batch_size] | |
| for i in range(0, len(records), batch_size)] | |
Xet Storage Details
- Size:
- 2.34 kB
- Xet hash:
- 4f30c810ddc2e3d9b2bdacf3fe1bd6c807910fc5c33e76e939e6ba1d5d8cc9bc
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.