-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from luizcdc/lista-resultados
Melhora a usabilidade da paginação de resultados
- Loading branch information
Showing
8 changed files
with
118 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
class CustomListTypeHint(type): | ||
def __getitem__(self, item): | ||
if isinstance(item, type): | ||
item = item.__name__ | ||
return f"{self.__name__}[{item}]" | ||
|
||
|
||
class ListaResultados(list, metaclass=CustomListTypeHint): | ||
def continuar_busca(self) -> "ListaResultados": | ||
"""Retorna a próxima página de resultados, caso exista.""" | ||
return self[-1].continuar_busca() if len(self) else ListaResultados() | ||
|
||
def mais_paginas(self, num_paginas: int = 1) -> int: | ||
"""Extende a lista de resultados com mais resultados, caso existam. | ||
:param num_paginas: informa quantas páginas adicionais devem ser solicitadas. | ||
Se omitido, busca-se apenas a próxima página. | ||
:return: Número de páginas adicionais recebidas com sucesso. | ||
""" | ||
return sum(int(self._mais_uma_pagina()) for _ in range(num_paginas)) | ||
|
||
def _mais_uma_pagina(self) -> bool: | ||
"""Extende a lista de resultados com a próxima página, caso exista. | ||
:return: True se recebeu uma nova página com sucesso, False caso contrário. | ||
""" | ||
if not len(self): | ||
return False | ||
|
||
novos_resultados = self[-1].continuar_busca() | ||
|
||
if not novos_resultados: | ||
return False | ||
|
||
if len(novos_resultados) > 1 and isinstance( | ||
novos_resultados[1], ListaResultados | ||
): | ||
novos_resultados = novos_resultados[1] | ||
|
||
self.extend(novos_resultados) | ||
|
||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters