Skip to content

Commit

Permalink
Add query pages object representation
Browse files Browse the repository at this point in the history
  • Loading branch information
Egsago-n committed Mar 25, 2024
1 parent a7f0072 commit f6ab93d
Showing 1 changed file with 35 additions and 12 deletions.
47 changes: 35 additions & 12 deletions src/phub/objects/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,39 @@

QueryItem = Video | FeedItem | User

class Pages:
'''
An iterator for query pages.
'''

def __init__(self, query: Query) -> None:
'''
Initialise a new Pages object.
'''

self.query = query

def __repr__(self) -> str:

return f'phub.Pages(query={self.query})'

def __getitem__(self, index: int | slice) -> Iterator[QueryItem] | Iterator[Iterator[QueryItem]]:
'''
Get a single, or slice of pages.
'''

if isinstance(index, int):
items = self.query._get_page(index)
return self.query._iter_page(items)

def wrap():
for i in range(index.start or 0,
index.stop or 0,
index.step or 1):

yield self[i]

return wrap()

class Query:
'''
Expand Down Expand Up @@ -74,22 +107,12 @@ def __len__(self) -> int:
return int(counter)

@cached_property
def pages(self) -> Iterator[Iterator[QueryItem]]:
def pages(self) -> Pages:
'''
Iterate through the query pages.
'''

i = 0
while 1:

try:
page = self._get_page(i)
i += 1

yield self._iter_page(page)

except errors.NoResult:
return
return Pages(self)

def __iter__(self) -> Iterator[QueryItem]:
'''
Expand Down

0 comments on commit f6ab93d

Please sign in to comment.