็คพไผไฟ้บๅ ๅ
ฅใขใซใใคใใๅฟ
ใ8ๆ้ๅคๅใซใชใใใใใผใๅถ็ดใ่ฟฝๅ (#7)
0484f98 Download utils/shift_patterns.py from sdu-shift/app: direct link, hf CLI and curl.
- Browser
- Download file 5.88 kB
-
https://huggingface.co/spaces/sdu-shift/app/resolve/main/utils/shift_patterns.py
- Command line
-
hf download hf://spaces/sdu-shift/app/utils/shift_patterns.py
-
curl -L -o shift_patterns.py https://huggingface.co/spaces/sdu-shift/app/resolve/main/utils/shift_patterns.py
5.88 kB
| """ใทใใใใฟใผใณๅฎ็พฉ | |
| ๅๅพๆฅญๅกใ้ธๆใงใใใทใใใใฟใผใณ๏ผ้ๅงใ็ตไบๆๅป๏ผใๅฎ็พฉใใใ | |
| CP-SATใฝใซใใผใฏๅใใฟใผใณใๆ้ฃ(6:00-11:00)/ใใฃใใผ(17:00-23:00)ใฎ | |
| ใฉใกใใใซใใผใใใใๅคๅฎใใฆๅถ็ดใซไฝฟ็จใใใ | |
| """ | |
| from __future__ import annotations | |
| from dataclasses import dataclass | |
| from typing import Optional | |
| # ๅถๆฅญๆ้ๅธฏใฎๅข็ | |
| BREAKFAST_START = 6.0 # 6:00 | |
| BREAKFAST_END = 11.0 # 11:00 | |
| DINNER_START = 17.0 # 17:00 | |
| DINNER_END = 23.0 # 23:00 | |
| MIN_OVERLAP_HRS = 2.0 # ใใฎๆ้ไปฅไธ้ใชใใฐใๆ ๅฝใใจใฟใชใ | |
| # ๆ้ฃใใญใณใฐๅคๅใใจใฟใชใ็ตไบๆๅปใฎไธ้๏ผใใฎๆๅปไปฅ้ใพใงๅคๅใใใใฟใผใณใๅฏพ่ฑก๏ผ | |
| LONG_BREAKFAST_END_HOUR = 14.0 # 14:00 | |
| class ShiftPattern: | |
| id: str | |
| label: str | |
| start: Optional[str] # "HH:MM"ใใใใซ/ใซในใฟใ ใฏNone | |
| end: Optional[str] # "HH:MM" | |
| force_both: bool = False # Trueใชใๆ้ฃใปใใฃใใผไธกๆนใซใใผ๏ผใใใซใทใใ๏ผ | |
| # โโ ๅ ้จ่จ็ฎ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| def _to_hour(self, t: str) -> float: | |
| h, m = map(int, t.split(":")) | |
| return h + m / 60.0 | |
| def start_hour(self) -> Optional[float]: | |
| return self._to_hour(self.start) if self.start else None | |
| def end_hour(self) -> Optional[float]: | |
| return self._to_hour(self.end) if self.end else None | |
| # โโ ในใญใใใซใใผๅคๅฎ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| def covers_breakfast(self) -> bool: | |
| """ๆ้ฃๆ้ๅธฏ(6:00-11:00)ใจ2ๆ้ไปฅไธ้ใชใใ""" | |
| if self.force_both: | |
| return True | |
| if not self.start or not self.end: | |
| return False | |
| overlap = max(0.0, | |
| min(self.end_hour(), BREAKFAST_END) - max(self.start_hour(), BREAKFAST_START)) | |
| return overlap >= MIN_OVERLAP_HRS | |
| def covers_dinner(self) -> bool: | |
| """ใใฃใใผๆ้ๅธฏ(17:00-23:00)ใจ2ๆ้ไปฅไธ้ใชใใ""" | |
| if self.force_both: | |
| return True | |
| if not self.start or not self.end: | |
| return False | |
| overlap = max(0.0, | |
| min(self.end_hour(), DINNER_END) - max(self.start_hour(), DINNER_START)) | |
| return overlap >= MIN_OVERLAP_HRS | |
| def duration_hours(self) -> float: | |
| """ๅฎๅคๅๆ้๏ผไผๆฉ่ๆ ฎใชใ๏ผ""" | |
| if self.force_both: | |
| return 11.0 # 5h + 6h | |
| if not self.start or not self.end: | |
| return 0.0 | |
| return max(0.0, self.end_hour() - self.start_hour()) | |
| def time_range_label(self) -> str: | |
| """่กจ็คบ็จใฎๆๅปๆๅญๅ""" | |
| if self.force_both: | |
| return "6:00ใ11:00 / 17:00ใ23:00" | |
| if self.start and self.end: | |
| return f"{self.start}ใ{self.end}" | |
| return "" | |
| # โโ ใใฟใผใณไธ่ฆง โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| ALL_PATTERNS: list[ShiftPattern] = [ | |
| # ๆ้ฃ็ณป | |
| ShiftPattern("b_short", "ๆ้ฃ็ญ็ช (6:00-10:00)", "06:00", "10:00"), | |
| ShiftPattern("b_std", "ๆ้ฃ (6:30-11:30)", "06:30", "11:30"), | |
| ShiftPattern("b_long", "ๆ้ฃใญใณใฐ (5:45-14:45)", "05:45", "14:45"), | |
| ShiftPattern("b_half", "ๅๅๅๆฅ (6:30-15:30)", "06:30", "15:30"), | |
| ShiftPattern("b_8h", "็คพไฟ8ๆ้ (6:00-14:00)", "06:00", "14:00"), | |
| # ้ใ๏ผใใฃใใผใซใใผ๏ผ | |
| ShiftPattern("d_full1", "้ใๆฉ็ช (13:00-22:30)", "13:00", "22:30"), | |
| ShiftPattern("d_full2", "้ใ้ ็ช (14:00-23:00)", "14:00", "23:00"), | |
| # ใใฃใใผ็ณป | |
| ShiftPattern("d_std1", "ใใฃใใผ (17:00-22:30)", "17:00", "22:30"), | |
| ShiftPattern("d_std2", "ใใฃใใผ (17:30-23:00)", "17:30", "23:00"), | |
| ShiftPattern("d_std3", "ใใฃใใผ (18:00-23:00)", "18:00", "23:00"), | |
| ShiftPattern("d_s1", "ใใฃใใผๅๅ (17:00-21:00)", "17:00", "21:00"), | |
| ShiftPattern("d_s2", "ใใฃใใผๅๅ (17:30-21:30)", "17:30", "21:30"), | |
| ShiftPattern("d_s3", "ใใฃใใผ็ญ็ช (18:00-22:00)", "18:00", "22:00"), | |
| # ใใใซ๏ผๆ้ฃ๏ผใใฃใใผ๏ผ | |
| ShiftPattern("double", "ใใใซ (ๆ้ฃ+ใใฃใใผ)", None, None, force_both=True), | |
| # ใซในใฟใ ๏ผๆๅปใๆๅ ฅๅ๏ผ | |
| ShiftPattern("custom", "ใซในใฟใ ๅ ฅๅ...", None, None), | |
| ] | |
| PATTERN_MAP: dict[str, ShiftPattern] = {p.id: p for p in ALL_PATTERNS} | |
| def is_long_breakfast_pattern(pattern_id: Optional[str], | |
| custom_start: Optional[str] = None, | |
| custom_end: Optional[str] = None) -> bool: | |
| """ๆ้ฃใใญใณใฐๅคๅใ๏ผ็ตไบๆๅปใ LONG_BREAKFAST_END_HOUR ไปฅ้๏ผใซ่ฉฒๅฝใใใๅคๅฎ""" | |
| if pattern_id == "custom": | |
| if not custom_start or not custom_end: | |
| return False | |
| sp = ShiftPattern("_c", "ใซในใฟใ ", custom_start, custom_end) | |
| else: | |
| sp = PATTERN_MAP.get(pattern_id) if pattern_id else None | |
| if not sp or not sp.covers_breakfast(): | |
| return False | |
| end_hour = sp.end_hour() | |
| return end_hour is not None and end_hour >= LONG_BREAKFAST_END_HOUR | |
| def default_pattern_from_fixed(breakfast: bool, dinner: bool) -> Optional[str]: | |
| """ๅบๅฎใใฟใผใณ(breakfast/dinner boolean)ใใๆขๅฎใฎใใฟใผใณIDใ่ฟใ""" | |
| if breakfast and dinner: | |
| return "double" | |
| if breakfast: | |
| return "b_std" | |
| if dinner: | |
| return "d_std1" | |
| return None | |