diff --git a/anki/cram.py b/anki/cram.py index 41736f48e..bbd6fb383 100644 --- a/anki/cram.py +++ b/anki/cram.py @@ -5,16 +5,17 @@ from anki.utils import ids2str, intTime from anki.sched import Scheduler -# The order arg should be the opposite of what you want. So if you want -# modified ascending, pass in 'mod desc'. - class CramScheduler(Scheduler): name = "cram" - def __init__(self, deck, gids, order): + def __init__(self, deck, gids, order, min=0, max=None): Scheduler.__init__(self, deck) self.gids = gids + # should be the opposite order of what you want self.order = order + # days to limit cram to, where tomorrow=0. Max is inclusive. + self.min = min + self.max = max self.reset() def counts(self): @@ -55,9 +56,15 @@ class CramScheduler(Scheduler): def _resetNew(self): "All review cards that are not due yet." + if self.max is not None: + maxlim = "and due <= %d" % (self.today+1+self.max) + else: + maxlim = "" self.newQueue = self.db.list(""" -select id from cards where queue = 2 and due > %d -and gid in %s order by %s limit %d""" % (self.today, +select id from cards where queue = 2 and due >= %d +%s +and gid in %s order by %s limit %d""" % (self.today+1+self.min, + maxlim, ids2str(self.gids), self.order, self.reportLimit)) @@ -102,4 +109,3 @@ and gid in %s order by %s limit %d""" % (self.today, def nextIvl(self, card, ease): "Return the next interval for CARD, in seconds." return self._nextLrnIvl(card, ease) - diff --git a/anki/deck.py b/anki/deck.py index 444f88115..997861a05 100644 --- a/anki/deck.py +++ b/anki/deck.py @@ -654,9 +654,9 @@ select conf from gconf where id = (select gcid from groups where id = ?)""", self.sched.onClose() self.sched = self._stdSched - def cramGroups(self, gids, order="mod desc"): + def cramGroups(self, gids, order="mod desc", min=0, max=None): self.stdSched() - self.sched = anki.cram.CramScheduler(self, gids, order) + self.sched = anki.cram.CramScheduler(self, gids, order, min, max) # Undo/redo ########################################################################## diff --git a/tests/test_sched.py b/tests/test_sched.py index 5084c66ee..f0d745def 100644 --- a/tests/test_sched.py +++ b/tests/test_sched.py @@ -372,3 +372,40 @@ def test_cram(): d.sched.answerCard(c, 3) assert c.ivl == 1 assert c.due == d.sched.today + 1 + +def test_cramLimits(): + d = getEmptyDeck() + # create three cards, due tomorrow, the next, etc + for i in range(3): + f = d.newFact() + f['Front'] = str(i) + d.addFact(f) + c = f.cards()[0] + c.type = c.queue = 2 + c.due = d.sched.today + 1 + i + c.flush() + # the default cram should return all three + d.cramGroups([1]) + assert d.sched.counts()[0] == 3 + # if we start from the day after tomorrow, it should be 2 + d.cramGroups([1], min=1) + assert d.sched.counts()[0] == 2 + # or after 2 days + d.cramGroups([1], min=2) + assert d.sched.counts()[0] == 1 + # we may get nothing + d.cramGroups([1], min=3) + assert d.sched.counts()[0] == 0 + # tomorrow(0) + dayAfter(1) = 2 + d.cramGroups([1], max=1) + assert d.sched.counts()[0] == 2 + # if max is tomorrow, we get only one + d.cramGroups([1], max=0) + assert d.sched.counts()[0] == 1 + # both should work + d.cramGroups([1], min=0, max=0) + assert d.sched.counts()[0] == 1 + d.cramGroups([1], min=1, max=1) + assert d.sched.counts()[0] == 1 + d.cramGroups([1], min=0, max=1) + assert d.sched.counts()[0] == 2