Modifying card ordinals programatically

Hi,

Quick question, is there a check hidden somewhere in the Anki code that checks for/corrects multiple cards with the same ordinal? Or syncs cards with notes as soon as you write to either? I am currently doing this:

for nid in nids:
  note = col.get_note(nid)
    if note.note_type().get('type', MODEL_STD) != MODEL_CLOZE: continue

    note_cards = {str(card.ord + 1): card for card in note.cards()}
    i = 0

    def substitute(match):
        nonlocal i, cards
        i += 1
        card = note_cards[match.group(1)]
        card.ord = i - 1
        cards.append(card)
        return rf'{{{{c{i}::'

    for fld in note.keys():
        note[fld] = re.sub(r'{{c(\d+)::', substitute, note[fld])

    notes.append(note)

col.update_cards(cards)
col.update_notes(notes)

It seems to work but I just want to make sure that there aren’t edge cases where Anki “fixes” the cards while I am restructuring the cards and notes.

Any thoughts?

Aside from Check Database, it should leave them alone.

Perfect, thanks.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.