Classic Criteria
ΠΠ°Π΅Ρ ΠΎΠ±ΡΠ΅ΠΊΡ-ΠΊΡΠΈΡΠ΅ΡΠΈΠΉ.
ΠΡΠΆΠ΅Π½ Π΄Π»Ρ ΠΎΠΏΠΈΡΠ°Π½ΠΈΡ ΠΊΡΠΈΡΠ΅ΡΠΈΠ΅Π² ΠΎΠ±ΡΠ΅ΠΊΡΠΎΠ², ΡΡΠΎΠ±Ρ Π·Π°ΡΠ΅ΠΌ ΠΎΠΏΡΠ΅Π΄Π΅Π»ΡΡΡ, ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²ΡΡΡ Π»ΠΈ ΡΠ΅ ΠΈΠ»ΠΈ ΠΈΠ½ΡΠ΅ ΠΎΠ±ΡΠ΅ΠΊΡΡ ΠΊΡΠΈΡΠ΅ΡΠΈΡ, ΠΈΠ»ΠΈ Π΄Π»Ρ ΡΠΎΡΠΌΠΈΡΠΎΠ²Π°Π½ΠΈΡ Π·Π°ΠΏΡΠΎΡΠ°, ΠΊ ΠΏΡΠΈΠΌΠ΅ΡΡ, Π² SQL-Ρ ΡΠ°Π½ΠΈΠ»ΠΈΡΠ΅.
ΠΡΠΈΠΌΠ΅Ρ:
from datetime import datetime
from dataclasses import dataclass
from classic.criteria import Criteria
@dataclass
class Task:
created_at: datetime
finished_at: datetime
@dataclass
class TaskOlderThan(Criteria[Task]):
date: datetime
def is_satisfied_by(self, candidate: Task) -> bool:
return candidate.created_at < self.date
@dataclass
class TaskObsolete(Criteria[Task]):
days_to_work: int
def is_satisfied_by(self, candidate: Task) -> bool:
days_spent = candidate.finished_at - candidate.created_at
return days_spent.days > self.days_to_work
some_task = Task(
created_at=datetime(2024, 1, 1),
finished_at=datetime(2024, 1, 10),
)
criteria = TaskObsolete(3) & TaskOlderThan(datetime(2024, 1, 31))
assert criteria.is_satisfied_by(some_task)
some_task = Task(
created_at=datetime(2024, 1, 1),
finished_at=datetime(2024, 1, 1),
)
criteria = TaskObsolete(3) & TaskOlderThan(datetime(2024, 1, 31))
assert not criteria.is_satisfied_by(some_task)