List->Sequence in a bunch of table/browser methods

Most code doesn't require a list specifically, and build a list
is an extra step.
This commit is contained in:
Damien Elmes 2021-03-29 16:48:33 +10:00
parent 13011f9708
commit 28aae21d51
4 changed files with 31 additions and 31 deletions

View File

@ -138,13 +138,13 @@ select id from cards where did in %s and queue = {QUEUE_TYPE_REV} and due <= ? l
# Resetting/rescheduling
##########################################################################
def schedule_cards_as_new(self, card_ids: List[CardId]) -> OpChanges:
def schedule_cards_as_new(self, card_ids: Sequence[CardId]) -> OpChanges:
"Put cards at the end of the new queue."
return self.col._backend.schedule_cards_as_new(card_ids=card_ids, log=True)
def set_due_date(
self,
card_ids: List[CardId],
card_ids: Sequence[CardId],
days: str,
config_key: Optional[Config.String.Key.V] = None,
) -> OpChanges:

View File

@ -5,7 +5,7 @@ from __future__ import annotations
import html
from dataclasses import dataclass
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
from typing import Any, Callable, Dict, List, Optional, Tuple, Union, Sequence
import aqt
import aqt.forms
@ -533,16 +533,16 @@ class Browser(QMainWindow):
# Menu helpers
######################################################################
def selected_cards(self) -> List[CardId]:
def selected_cards(self) -> Sequence[CardId]:
return self.table.get_selected_card_ids()
def selected_notes(self) -> List[NoteId]:
def selected_notes(self) -> Sequence[NoteId]:
return self.table.get_selected_note_ids()
def selectedNotesAsCards(self) -> List[CardId]:
def selectedNotesAsCards(self) -> Sequence[CardId]:
return self.table.get_card_ids_from_selected_note_ids()
def oneModelNotes(self) -> List[NoteId]:
def oneModelNotes(self) -> Sequence[NoteId]:
sf = self.selected_notes()
if not sf:
return []
@ -742,7 +742,7 @@ where id in %s"""
def _on_export_notes(self) -> None:
cids = self.selectedNotesAsCards()
if cids:
ExportDialog(self.mw, cids=cids)
ExportDialog(self.mw, cids=list(cids))
# Flags & Marking
######################################################################
@ -1027,7 +1027,7 @@ where id in %s"""
class ChangeModel(QDialog):
def __init__(self, browser: Browser, nids: List[NoteId]) -> None:
def __init__(self, browser: Browser, nids: Sequence[NoteId]) -> None:
QDialog.__init__(self, browser)
self.browser = browser
self.nids = nids
@ -1198,7 +1198,7 @@ class ChangeModel(QDialog):
b.mw.progress.start()
b.begin_reset()
mm = b.mw.col.models
mm.change(self.oldModel, self.nids, self.targetModel, fmap, cmap)
mm.change(self.oldModel, list(self.nids), self.targetModel, fmap, cmap)
b.search()
b.end_reset()
b.mw.progress.finish()

View File

@ -21,7 +21,7 @@ def set_due_date_dialog(
*,
mw: aqt.AnkiQt,
parent: QWidget,
card_ids: List[CardId],
card_ids: Sequence[CardId],
config_key: Optional[Config.String.Key.V],
) -> None:
if not card_ids:
@ -54,7 +54,7 @@ def set_due_date_dialog(
)
def forget_cards(*, mw: aqt.AnkiQt, parent: QWidget, card_ids: List[CardId]) -> None:
def forget_cards(*, mw: aqt.AnkiQt, parent: QWidget, card_ids: Sequence[CardId]) -> None:
if not card_ids:
return

View File

@ -40,7 +40,7 @@ from aqt.utils import (
)
Item = Union[CardId, NoteId]
ItemList = Union[List[CardId], List[NoteId]]
ItemList = Union[Sequence[CardId], Sequence[NoteId]]
@dataclass
@ -118,13 +118,13 @@ class Table:
# Get ids
def get_selected_card_ids(self) -> List[CardId]:
def get_selected_card_ids(self) -> Sequence[CardId]:
return self._model.get_card_ids(self._selected())
def get_selected_note_ids(self) -> List[NoteId]:
def get_selected_note_ids(self) -> Sequence[NoteId]:
return self._model.get_note_ids(self._selected())
def get_card_ids_from_selected_note_ids(self) -> List[CardId]:
def get_card_ids_from_selected_note_ids(self) -> Sequence[CardId]:
return self._state.card_ids_from_note_ids(self.get_selected_note_ids())
# Selecting
@ -531,12 +531,12 @@ class ItemState(ABC):
# Stateless Helpers
def note_ids_from_card_ids(self, items: Sequence[Item]) -> List[NoteId]:
def note_ids_from_card_ids(self, items: Sequence[Item]) -> Sequence[NoteId]:
return self.col.db.list(
f"select distinct nid from cards where id in {ids2str(items)}"
)
def card_ids_from_note_ids(self, items: Sequence[Item]) -> List[CardId]:
def card_ids_from_note_ids(self, items: Sequence[Item]) -> Sequence[CardId]:
return self.col.db.list(f"select id from cards where nid in {ids2str(items)}")
# Columns and sorting
@ -590,11 +590,11 @@ class ItemState(ABC):
"""Return the appropriate item id for a card id."""
@abstractmethod
def get_card_ids(self, items: List[Item]) -> List[CardId]:
def get_card_ids(self, items: Sequence[Item]) -> Sequence[CardId]:
"""Return the card ids for the given item ids."""
@abstractmethod
def get_note_ids(self, items: List[Item]) -> List[NoteId]:
def get_note_ids(self, items: Sequence[Item]) -> Sequence[NoteId]:
"""Return the note ids for the given item ids."""
# Toggle
@ -694,10 +694,10 @@ class CardState(ItemState):
def get_item_from_card_id(self, card: CardId) -> Item:
return card
def get_card_ids(self, items: List[Item]) -> List[CardId]:
return list(map(CardId, items))
def get_card_ids(self, items: Sequence[Item]) -> Sequence[CardId]:
return cast(Sequence[CardId], items)
def get_note_ids(self, items: List[Item]) -> List[NoteId]:
def get_note_ids(self, items: Sequence[Item]) -> Sequence[NoteId]:
return super().note_ids_from_card_ids(items)
def toggle_state(self) -> NoteState:
@ -706,7 +706,7 @@ class CardState(ItemState):
def get_new_item(self, old_item: Item) -> CardId:
return super().card_ids_from_note_ids([old_item])[0]
def get_new_items(self, old_items: Sequence[Item]) -> List[CardId]:
def get_new_items(self, old_items: Sequence[Item]) -> Sequence[CardId]:
return super().card_ids_from_note_ids(old_items)
@ -782,11 +782,11 @@ class NoteState(ItemState):
def get_item_from_card_id(self, card: CardId) -> Item:
return self.get_card(card).note().id
def get_card_ids(self, items: List[Item]) -> List[CardId]:
def get_card_ids(self, items: Sequence[Item]) -> Sequence[CardId]:
return super().card_ids_from_note_ids(items)
def get_note_ids(self, items: List[Item]) -> List[NoteId]:
return list(map(NoteId, items))
def get_note_ids(self, items: Sequence[Item]) -> Sequence[NoteId]:
return cast(Sequence[NoteId], items)
def toggle_state(self) -> CardState:
return CardState(self.col)
@ -794,7 +794,7 @@ class NoteState(ItemState):
def get_new_item(self, old_item: Item) -> NoteId:
return super().note_ids_from_card_ids([old_item])[0]
def get_new_items(self, old_items: Sequence[Item]) -> List[NoteId]:
def get_new_items(self, old_items: Sequence[Item]) -> Sequence[NoteId]:
return super().note_ids_from_card_ids(old_items)
@ -959,13 +959,13 @@ class DataModel(QAbstractTableModel):
def get_item(self, index: QModelIndex) -> Item:
return self._items[index.row()]
def get_items(self, indices: List[QModelIndex]) -> List[Item]:
def get_items(self, indices: List[QModelIndex]) -> Sequence[Item]:
return [self.get_item(index) for index in indices]
def get_card_ids(self, indices: List[QModelIndex]) -> List[CardId]:
def get_card_ids(self, indices: List[QModelIndex]) -> Sequence[CardId]:
return self._state.get_card_ids(self.get_items(indices))
def get_note_ids(self, indices: List[QModelIndex]) -> List[NoteId]:
def get_note_ids(self, indices: List[QModelIndex]) -> Sequence[NoteId]:
return self._state.get_note_ids(self.get_items(indices))
# Get row numbers from items