make the group tree part of the scheduler instead

This commit is contained in:
Damien Elmes 2011-03-28 21:41:00 +09:00
parent 728715ff84
commit 2a1355eb16
4 changed files with 65 additions and 38 deletions

View file

@ -2,9 +2,7 @@
# Copyright: Damien Elmes <anki@ichi2.net>
# License: GNU GPL, version 3 or later; http://www.gnu.org/copyleft/gpl.html
import time, os, random, re, stat, simplejson, datetime, copy, itertools
import operator
import time, os, random, re, stat, simplejson, datetime, copy
from anki.lang import _, ngettext
from anki.utils import parseTags, tidyHTML, ids2str, hexifyID, \
checksum, fieldChecksum, addTags, delTags, stripHTML, intTime, \
@ -557,24 +555,6 @@ update facts set tags = :t, mod = :n where id = :id""", [fix(row) for row in res
"A list of all group names."
return self.db.list("select name from groups order by name")
def groupsTree(self):
return self._groupChildren(self.groups())
def _groupChildren(self, grps):
tree = []
for (head, tail) in itertools.groupby([x.split("::", 1) for x in grps],
key=operator.itemgetter(0)):
tail = list(tail)
l = [c[1] for c in tail if len(c) > 1]
children = self._groupChildren(l)
tree.append((head, children))
return tuple(tree)
for g in grps:
names = g.split("::")
if g == last:
top[-1][1].append(g)
pass
def groupId(self, name):
"Return the id for NAME, creating if necessary."
id = self.db.scalar("select id from groups where name = ?", name)

View file

@ -2,7 +2,7 @@
# Copyright: Damien Elmes <anki@ichi2.net>
# License: GNU GPL, version 3 or later; http://www.gnu.org/copyleft/gpl.html
import time, datetime, simplejson, random
import time, datetime, simplejson, random, itertools
from operator import itemgetter
from heapq import *
#from anki.cards import Card
@ -133,6 +133,39 @@ group by gid""", self.today):
self.deck.db.execute(
"select id, name from groups order by name")]
def groupCountTree(self):
return self._groupChildren(self.groupCounts())
def _groupChildren(self, grps):
tree = []
# split strings
for g in grps:
g[0] = g[0].split("::", 1)
# group and recurse
def key(grp):
return grp[0][0]
for (head, tail) in itertools.groupby(grps, key=key):
tail = list(tail)
rev = 0
new = 0
children = []
for c in tail:
if len(c[0]) == 1:
# current node
rev += c[1]
new += c[2]
else:
# set new string to tail
c[0] = c[0][1]
children.append(c)
children = self._groupChildren(children)
# tally up children counts
for ch in children:
rev += ch[1]
new += ch[2]
tree.append((head, rev, new, children))
return tuple(tree)
# Getting the next card
##########################################################################

View file

@ -177,12 +177,3 @@ def test_selective():
assert deck.db.scalar("select count() from cards where gid = 3") == 3
deck.setGroupForTags(["one"], [], 2)
assert deck.db.scalar("select count() from cards where gid = 2") == 2
def test_groups():
d = getEmptyDeck()
tree = d._groupChildren(
["a", "b", "c::1", "c::2", "d", "d::1", "d::1::2::3"])
assert tree[0] == ("a", tuple())
assert tree[1] == ("b", tuple())
assert tree[2] == ("c", (("1", tuple()), ("2", tuple())))
assert tree[3] == ('d', (('1', (('2', (('3', ()),)),)),))

View file

@ -592,14 +592,16 @@ def test_collapse():
def test_groupCounts():
d = getEmptyDeck()
# add two facts
# add a fact with default group
f = d.newFact()
f['Front'] = u"one"
d.addFact(f)
# and one that's a child
f = d.newFact()
f['Front'] = u"two"
f.gid = d.groupId("Default Group::1")
d.addFact(f)
# make one a review card
# make it a review card
c = f.cards()[0]
c.queue = 2
c.due = 0
@ -607,11 +609,32 @@ def test_groupCounts():
# add one more with a new group
f = d.newFact()
f['Front'] = u"two"
f.gid = d.groupId("new")
f.gid = d.groupId("foo::bar")
d.addFact(f)
# and one that's a sibling
f = d.newFact()
f['Front'] = u"three"
f.gid = d.groupId("foo::baz")
d.addFact(f)
d.reset()
assert d.sched.counts() == (2, 0, 1)
assert len(d.groups()) == 2
assert d.sched.counts() == (3, 0, 1)
assert len(d.groups()) == 4
cnts = d.sched.groupCounts()
assert cnts[0] == ["Default Group", 1, 1]
assert cnts[1] == ["new", 0, 1]
assert cnts[0] == ["Default Group", 0, 1]
assert cnts[1] == ["Default Group::1", 1, 0]
assert cnts[2] == ["foo::bar", 0, 1]
assert cnts[3] == ["foo::baz", 0, 1]
tree = d.sched.groupCountTree()
assert tree[0][0] == "Default Group"
# sum of child and parent
assert tree[0][1] == 1
assert tree[0][2] == 1
# child count is just review
assert tree[0][3][0][0] == "1"
assert tree[0][3][0][1] == 1
assert tree[0][3][0][2] == 0
# event if parent group didn't exist, it should have been created with a
# counts summary.
assert tree[1][0] == "foo"
assert tree[1][1] == 0
assert tree[1][2] == 2