add the ability to limit a cram to a given period

This commit is contained in:
Damien Elmes 2011-03-19 13:06:33 +09:00
parent 9b70af678c
commit 138d9881d3
3 changed files with 52 additions and 9 deletions

View file

@ -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)

View file

@ -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
##########################################################################

View file

@ -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