Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections.abc import Iterable
- import dataclasses
- from typing import Self
- class Shift: ...
- @dataclasses.dataclass
- class Skill:
- id: int
- @dataclasses.dataclass
- class Operator1:
- id: int
- shift: Shift
- skills: list[Skill]
- def __post_init__(self) -> None:
- # All operators carry their own ID as a skill so we can lock jobs to
- # specific people
- self.skills.append(Skill(self.id))
- @classmethod
- def new(
- cls,
- id: int,
- shift: Shift,
- skills: Skill | Iterable[Skill] | None = None,
- ) -> Self:
- if __debug__:
- if isinstance(skills, Iterable):
- if not all(
- isinstance(item, Skill) # type: ignore
- for item in skills
- ):
- raise ValueError("Invalid skill type included in list of skills")
- return cls(
- id,
- shift,
- (
- [skills]
- if isinstance(skills, Skill) else
- list(skills or [])
- ),
- )
- @dataclasses.dataclass
- class Operator2:
- id: int
- shift: Shift
- skills: list[Skill]
- def __post_init__(self) -> None:
- self.skills.append(Skill(self.id))
- @classmethod
- def from_single(
- cls,
- id: int,
- shift: Shift,
- skills: Skill | None = None,
- ) -> Self:
- return cls(
- id,
- shift,
- [] if skills is None else [skills],
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement