Download cases/cloud_data_warehouse/raw/mcp_servers/utils.py from JoeYing/ReScopeBench: direct link, hf CLI and curl.
- Browser
- Download file 3.14 kB
-
https://huggingface.co/datasets/JoeYing/ReScopeBench/resolve/main/cases/cloud_data_warehouse/raw/mcp_servers/utils.py
- Command line
-
hf download hf://datasets/JoeYing/ReScopeBench/cases/cloud_data_warehouse/raw/mcp_servers/utils.py
-
curl -L -o utils.py https://huggingface.co/datasets/JoeYing/ReScopeBench/resolve/main/cases/cloud_data_warehouse/raw/mcp_servers/utils.py
3.14 kB
| """Shared helpers for the cloud data warehouse environment.""" | |
| from __future__ import annotations | |
| import copy | |
| import re | |
| from datetime import datetime | |
| from typing import Any, Dict, Iterable, Optional | |
| RUNTIME_NOW = "2026-06-01T00:00:00Z" | |
| ID_PREFIXES: Dict[str, str] = { | |
| "organization": "org", | |
| "user": "user", | |
| "warehouse": "wh", | |
| "dataset": "ds", | |
| "table": "tbl", | |
| "column": "col", | |
| "access_grant": "grant", | |
| "query_job": "query", | |
| "import_job": "import", | |
| "export_job": "export", | |
| "cost_usage_record": "usage", | |
| "quota_policy": "quota", | |
| } | |
| LEGACY_ID_PREFIXES: Dict[str, str] = { | |
| "organization": "ORG", | |
| "dataset": "DST", | |
| "table": "TBL", | |
| "query_job": "QRY", | |
| } | |
| 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 safe_copy(value: Any) -> Any: | |
| return copy.deepcopy(value) | |
| def now_iso() -> str: | |
| return RUNTIME_NOW | |
| def slugify(value: Any) -> str: | |
| text = str(value or "").strip().lower() | |
| text = re.sub(r"[^a-z0-9]+", "-", text).strip("-") | |
| return text or "untitled" | |
| def org_slug(org_id: str) -> str: | |
| return org_id[4:] if isinstance(org_id, str) and org_id.startswith("org-") else slugify(org_id) | |
| def build_humanized_id(entity_type: str, record: Dict[str, Any], *, existing_ids: Optional[Iterable[str]] = None) -> str: | |
| existing = set(existing_ids or []) | |
| prefix = ID_PREFIXES.get(entity_type, entity_type) | |
| if entity_type == "dataset": | |
| base = f"ds-{org_slug(record.get('org_id', 'org'))}-{slugify(record.get('business_domain') or record.get('name'))}" | |
| elif entity_type == "table": | |
| dataset_id = str(record.get("dataset_id") or "") | |
| dataset_slug = dataset_id[3:] if dataset_id.startswith("ds-") else slugify(dataset_id) | |
| base = f"tbl-{dataset_slug}-{slugify(record.get('name'))}" | |
| elif entity_type == "quota_policy": | |
| scope = "warehouse-cap" if record.get("warehouse_id") else "monthly" | |
| base = f"quota-{org_slug(record.get('org_id', 'org'))}-{slugify(record.get('resource_group'))}-{scope}" | |
| else: | |
| base = f"{prefix}-{slugify(record.get('name') or record.get('display_name') or entity_type)}" | |
| candidate = base | |
| index = 2 | |
| while candidate in existing: | |
| candidate = f"{base}-{index}" | |
| index += 1 | |
| return candidate | |
| def is_iso_datetime(value: Any) -> bool: | |
| if not isinstance(value, str): | |
| return False | |
| try: | |
| datetime.strptime(value, "%Y-%m-%dT%H:%M:%SZ") | |
| return True | |
| except ValueError: | |
| return False | |
| def is_date(value: Any) -> bool: | |
| if not isinstance(value, str): | |
| return False | |
| try: | |
| datetime.strptime(value, "%Y-%m-%d") | |
| return True | |
| except ValueError: | |
| return False | |
| def datetime_gt(left: str, right: str) -> bool: | |
| return datetime.strptime(left, "%Y-%m-%dT%H:%M:%SZ") > datetime.strptime(right, "%Y-%m-%dT%H:%M:%SZ") | |