anki.foreign_data#

Helpers for serializing third-party collections to a common JSON form.

Submodules#

Classes#

ForeignCardType

ForeignNotetype

ForeignCard

Data for creating an Anki card.

ForeignNote

ForeignData

ForeignDataEncoder

Extensible JSON <https://json.org> encoder for Python data structures.

Package Contents#

class anki.foreign_data.ForeignCardType#
name: str#
qfmt: str#
afmt: str#
static front_back() ForeignCardType#
static back_front() ForeignCardType#
static cloze() ForeignCardType#
class anki.foreign_data.ForeignNotetype#
name: str#
fields: list[str]#
templates: list[ForeignCardType]#
is_cloze: bool = False#
static basic(name: str) ForeignNotetype#
static basic_reverse(name: str) ForeignNotetype#
static cloze(name: str) ForeignNotetype#
class anki.foreign_data.ForeignCard#

Data for creating an Anki card.

Usually a review card, as the default card generation routine will take care of missing new cards.

due – UNIX timestamp interval – days ease_factor – decimal fraction (2.5 corresponds to default ease)

due: int = 0#
interval: int = 1#
ease_factor: float = 2.5#
reps: int = 0#
lapses: int = 0#
class anki.foreign_data.ForeignNote#
fields: list[str] = []#
tags: list[str] = []#
notetype: str | anki.models.NotetypeId = ''#
deck: str | anki.decks.DeckId = ''#
cards: list[ForeignCard] = []#
class anki.foreign_data.ForeignData#
notes: list[ForeignNote] = []#
notetypes: list[ForeignNotetype] = []#
default_deck: str | anki.decks.DeckId = ''#
serialize() str#
class anki.foreign_data.ForeignDataEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)#

Bases: json.JSONEncoder

Extensible JSON <https://json.org> encoder for Python data structures.

Supports the following objects and types by default:

Python

JSON

dict

object

list, tuple

array

str

string

int, float

number

True

true

False

false

None

null

To extend this to recognize other objects, subclass and implement a .default() method with another method that returns a serializable object for o if possible, otherwise it should call the superclass implementation (to raise TypeError).

default(obj: object) dict#

Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).

For example, to support arbitrary iterators, you could implement default like this:

def default(self, o):
    try:
        iterable = iter(o)
    except TypeError:
        pass
    else:
        return list(iterable)
    # Let the base class default method raise the TypeError
    return super().default(o)