/
BorisPlus
/
Telegram-Bot-Discussion-Examples
Обзор
Документация
Войти
/
BorisPlus
/
Telegram-Bot-Discussion-Examples
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
examples/pagination_bot/database/operations.py
51 строка
1 KB
b
2025_10_13_initial
13 окт 2025, 00:05
13 окт 2025, 00:05
0fc4405
Код
Авторство
О чём код?
from dataclasses import dataclass from typing import List, Union from .collections import _dataset, ID, NAME, JOB @dataclass class PersonRecord: id: int name: str job: str def fetch(id: int) -> Union[PersonRecord, None]: for row in _dataset: if row.get(ID, None) == id: return PersonRecord(**row) return None LIMIT = 5 def partition(query: str, page: int) -> List[PersonRecord]: if page < 1: page = 1 partition_start = LIMIT * (page - 1) current_partition = 0 collection: List[PersonRecord] = [] for row in _dataset: if query in row.get(NAME, []) or query in row.get(JOB, []) or query == "*": if partition_start <= current_partition and len(collection) < LIMIT: collection.append(PersonRecord(**row)) if len(collection) == LIMIT: break current_partition += 1 return collection def is_partition_exist(query: str, page: int) -> bool: if page < 1: page = 1 partition_start = LIMIT * (page - 1) current_partition = 0 for row in _dataset: if query in row.get(NAME, []) or query in row.get(JOB, []) or query == "*": if partition_start <= current_partition: return True current_partition += 1 return False