JoeYing
Add ReScopeBench cases and task preview
f726fd6
Raw History Blame Contribute Delete
3.51 kB
"""Shared helpers for course selection tools."""
from __future__ import annotations
import copy
import re
from datetime import datetime
from typing import Any, Dict, Iterable, Optional
ID_PREFIXES: Dict[str, str] = {
"department": "department",
"term": "term",
"course": "course",
"course_section": "section",
"student": "student",
"registration": "registration",
"waitlist_entry": "waitlist",
"advising_hold": "hold",
}
LEGACY_ID_PREFIXES: Dict[str, str] = {
"department": "DEPT",
"term": "TERM",
"course": "COURSE",
"course_section": "SECTION",
"student": "STUDENT",
"registration": "REG",
"waitlist_entry": "WAIT",
"advising_hold": "HOLD",
}
def success_response(data: Dict[str, Any], message: str = "") -> Dict[str, Any]:
return {"status": "success", "data": data, "message": message}
def error_response(error_code: str, message: str) -> Dict[str, Any]:
return {"status": "error", "data": {}, "message": f"{error_code}: {message}"}
def now_iso() -> str:
return datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
def safe_copy(value: Any) -> Any:
return copy.deepcopy(value)
def slugify(value: Any) -> str:
text = re.sub(r"[^a-z0-9]+", "-", str(value or "").lower()).strip("-")
return text or "item"
def generate_id(entity_type: str, sequence: int) -> str:
return f"{ID_PREFIXES.get(entity_type, entity_type)}-generated-{int(sequence)}"
def validate_id_format(value: str, entity_type: str) -> bool:
prefix = ID_PREFIXES.get(entity_type)
return isinstance(value, str) and prefix is not None and bool(
re.match(rf"^{re.escape(prefix)}-[a-z0-9][a-z0-9-]*$", value)
)
def extract_id_sequence(value: Any, entity_type: Optional[str] = None) -> Optional[int]:
if not isinstance(value, str):
return None
match = re.search(r"-generated-(\d+)$", value)
return int(match.group(1)) if match else None
def _unique(candidate: str, existing_ids: Optional[Iterable[str]]) -> str:
existing = set(existing_ids or [])
if candidate not in existing:
return candidate
index = 2
while f"{candidate}-{index}" in existing:
index += 1
return f"{candidate}-{index}"
def build_humanized_id(entity_type: str, record: Dict[str, Any], *, existing_ids: Optional[Iterable[str]] = None) -> str:
if entity_type == "registration":
candidate = (
f"registration-{record.get('term_id')}-{str(record.get('student_id')).removeprefix('student-')}-"
f"{str(record.get('course_id')).removeprefix('course-')}-{slugify(record.get('section_code') or str(record.get('section_id')).split('-')[-1])}"
)
return _unique(candidate, existing_ids)
if entity_type == "waitlist_entry":
candidate = (
f"waitlist-{record.get('term_id')}-{str(record.get('student_id')).removeprefix('student-')}-"
f"{str(record.get('course_id')).removeprefix('course-')}-{slugify(record.get('section_code') or str(record.get('section_id')).split('-')[-1])}"
)
return _unique(candidate, existing_ids)
if entity_type == "advising_hold":
candidate = f"hold-{record.get('term_id')}-{str(record.get('student_id')).removeprefix('student-')}-{slugify(record.get('title') or record.get('hold_type'))}"
return _unique(candidate, existing_ids)
return _unique(f"{ID_PREFIXES.get(entity_type, entity_type)}-{slugify(record.get('name') or record.get('title') or 'record')}", existing_ids)