mirror of
https://github.com/csunny/DB-GPT.git
synced 2025-10-23 01:49:58 +00:00
feat(core): Support higher-order operators (#1984)
Co-authored-by: 谨欣 <echo.cmy@antgroup.com>
This commit is contained in:
@@ -15,3 +15,29 @@ class PaginationResult(BaseModel, Generic[T]):
|
||||
total_pages: int = Field(..., description="total number of pages")
|
||||
page: int = Field(..., description="Current page number")
|
||||
page_size: int = Field(..., description="Number of items per page")
|
||||
|
||||
@classmethod
|
||||
def build_from_all(
|
||||
cls, all_items: List[T], page: int, page_size: int
|
||||
) -> "PaginationResult[T]":
|
||||
"""Build a pagination result from all items"""
|
||||
if page < 1:
|
||||
page = 1
|
||||
if page_size < 1:
|
||||
page_size = 1
|
||||
total_count = len(all_items)
|
||||
total_pages = (
|
||||
(total_count + page_size - 1) // page_size if total_count > 0 else 0
|
||||
)
|
||||
page = max(1, min(page, total_pages)) if total_pages > 0 else 0
|
||||
start_index = (page - 1) * page_size if page > 0 else 0
|
||||
end_index = min(start_index + page_size, total_count)
|
||||
items = all_items[start_index:end_index]
|
||||
|
||||
return cls(
|
||||
items=items,
|
||||
total_count=total_count,
|
||||
total_pages=total_pages,
|
||||
page=page,
|
||||
page_size=page_size,
|
||||
)
|
||||
|
Reference in New Issue
Block a user