Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2025
15
0
1 day
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.54 KB | None | 0 0
  1. from collections.abc import Iterable
  2. import dataclasses
  3. from typing import Self
  4.  
  5.  
  6. class Shift: ...
  7.  
  8. @dataclasses.dataclass
  9. class Skill:
  10.     id: int
  11.  
  12.  
  13. @dataclasses.dataclass
  14. class Operator1:
  15.     id: int
  16.     shift: Shift
  17.     skills: list[Skill]
  18.  
  19.     def __post_init__(self) -> None:
  20.         # All operators carry their own ID as a skill so we can lock jobs to
  21.         # specific people
  22.         self.skills.append(Skill(self.id))
  23.  
  24.     @classmethod
  25.     def new(
  26.         cls,
  27.         id: int,
  28.         shift: Shift,
  29.         skills: Skill | Iterable[Skill] | None = None,
  30.     ) -> Self:
  31.         if __debug__:
  32.             if isinstance(skills, Iterable):
  33.                 if not all(
  34.                     isinstance(item, Skill)  # type: ignore
  35.                     for item in skills
  36.                 ):
  37.                     raise ValueError("Invalid skill type included in list of skills")
  38.         return cls(
  39.             id,
  40.             shift,
  41.             (
  42.                 [skills]
  43.                 if isinstance(skills, Skill) else
  44.                 list(skills or [])
  45.             ),
  46.         )
  47.  
  48.  
  49. @dataclasses.dataclass
  50. class Operator2:
  51.     id: int
  52.     shift: Shift
  53.     skills: list[Skill]
  54.  
  55.     def __post_init__(self) -> None:
  56.         self.skills.append(Skill(self.id))
  57.  
  58.     @classmethod
  59.     def from_single(
  60.         cls,
  61.         id: int,
  62.         shift: Shift,
  63.         skills: Skill | None = None,
  64.     ) -> Self:
  65.         return cls(
  66.             id,
  67.             shift,
  68.             [] if skills is None else [skills],
  69.         )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement