classic-criteria

Provides criteria object


License
MIT
Install
pip install classic-criteria==0.0.1

Documentation

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)