Ejemplo n.º 1
0
def formatQA(cid, mid, fact, tags, cm, deck):
    "Return a dict of {id, question, answer}"
    d = {'id': cid}
    fields = {}
    for (k, v) in fact.items():
        fields["text:"+k] = stripHTML(v[1])
        if v[1]:
            fields[k] = '<span class="fm%s">%s</span>' % (
                hexifyID(v[0]), v[1])
        else:
            fields[k] = u""
    fields['tags'] = tags[0]
    fields['Tags'] = tags[0]
    fields['modelTags'] = tags[1]
    fields['cardModel'] = tags[2]
    # render q & a
    ret = []
    for (type, format) in (("question", cm.qformat),
                           ("answer", cm.aformat)):
        # convert old style
        format = re.sub("%\((.+?)\)s", "{{\\1}}", format)
        # allow custom rendering functions & info
        fields = runFilter("prepareFields", fields, cid, mid, fact, tags, cm, deck)
        html = render(format, fields)
        d[type] = runFilter("formatQA", html, type, cid, mid, fact, tags, cm, deck)
    return d
    def report(self):
        "Return an HTML string with a report."
        fmtPerc = oldanki.utils.fmtPercentage
        fmtFloat = oldanki.utils.fmtFloat
        if self.deck.isEmpty():
            return _("Please add some cards first.") + "<p/>"
        d = self.deck
        html = "<h1>" + _("Deck Statistics") + "</h1>"
        html += _("Deck created: <b>%s</b> ago<br>") % self.createdTimeStr()
        total = d.cardCount
        new = d.newCountAll()
        young = d.youngCardCount()
        old = d.matureCardCount()
        newP = new / float(total) * 100
        youngP = young / float(total) * 100
        oldP = old / float(total) * 100
        stats = d.getStats()
        (stats["new"], stats["newP"]) = (new, newP)
        (stats["old"], stats["oldP"]) = (old, oldP)
        (stats["young"], stats["youngP"]) = (young, youngP)
        html += _("Total number of cards:") + " <b>%d</b><br>" % total
        html += _(
            "Total number of facts:") + " <b>%d</b><br><br>" % d.factCount

        html += "<b>" + _("Card Maturity") + "</b><br>"
        html += _("Mature cards: <!--card count-->"
                  ) + " <b>%(old)d</b> (%(oldP)s)<br>" % {
                      'old': stats['old'],
                      'oldP': fmtPerc(stats['oldP'])
                  }
        html += _("Young cards: <!--card count-->"
                  ) + " <b>%(young)d</b> (%(youngP)s)<br>" % {
                      'young': stats['young'],
                      'youngP': fmtPerc(stats['youngP'])
                  }
        html += _("Unseen cards:") + " <b>%(new)d</b> (%(newP)s)<br>" % {
            'new': stats['new'],
            'newP': fmtPerc(stats['newP'])
        }
        avgInt = self.getAverageInterval()
        if avgInt:
            html += _("Average interval: ") + (
                "<b>%s</b> ") % fmtFloat(avgInt) + _("days")
            html += "<br>"
        html += "<br>"
        html += "<b>" + _("Correct Answers") + "</b><br>"
        html += _("Mature cards: <!--correct answers-->") + " <b>" + fmtPerc(
            stats['gMatureYes%']) + ("</b> " +
                                     _("(%(partOf)d of %(totalSum)d)") % {
                                         'partOf': stats['gMatureYes'],
                                         'totalSum': stats['gMatureTotal']
                                     } + "<br>")
        html += _("Young cards: <!--correct answers-->") + " <b>" + fmtPerc(
            stats['gYoungYes%']) + ("</b> " +
                                    _("(%(partOf)d of %(totalSum)d)") % {
                                        'partOf': stats['gYoungYes'],
                                        'totalSum': stats['gYoungTotal']
                                    } + "<br>")
        html += _("First-seen cards:") + " <b>" + fmtPerc(
            stats['gNewYes%']) + ("</b> " +
                                  _("(%(partOf)d of %(totalSum)d)") % {
                                      'partOf': stats['gNewYes'],
                                      'totalSum': stats['gNewTotal']
                                  } + "<br><br>")

        # average pending time
        existing = d.cardCount - d.newCountToday

        def tr(a, b):
            return "<tr><td>%s</td><td align=right>%s</td></tr>" % (a, b)

        def repsPerDay(reps, days):
            retval = ("<b>%d</b> " % reps) + ngettext("rep", "reps", reps)
            retval += ("/<b>%d</b> " % days) + ngettext("day", "days", days)
            return retval

        if existing and avgInt:
            html += "<b>" + _("Recent Work") + "</b>"
            if sys.platform.startswith("darwin"):
                html += "<table width=250>"
            else:
                html += "<table width=200>"
            html += tr(
                _("In last week"),
                repsPerDay(self.getRepsDone(-7, 0),
                           self.getDaysReviewed(-7, 0)))
            html += tr(
                _("In last month"),
                repsPerDay(self.getRepsDone(-30, 0),
                           self.getDaysReviewed(-30, 0)))
            html += tr(
                _("In last 3 months"),
                repsPerDay(self.getRepsDone(-92, 0),
                           self.getDaysReviewed(-92, 0)))
            html += tr(
                _("In last 6 months"),
                repsPerDay(self.getRepsDone(-182, 0),
                           self.getDaysReviewed(-182, 0)))
            html += tr(
                _("In last year"),
                repsPerDay(self.getRepsDone(-365, 0),
                           self.getDaysReviewed(-365, 0)))
            html += tr(
                _("Deck life"),
                repsPerDay(self.getRepsDone(-13000, 0),
                           self.getDaysReviewed(-13000, 0)))
            html += "</table>"

            html += "<br><br><b>" + _("Average Daily Reviews") + "</b>"
            if sys.platform.startswith("darwin"):
                html += "<table width=250>"
            else:
                html += "<table width=200>"
            html += tr(_("Deck life"), ("<b>%s</b> ") %
                       (fmtFloat(self.getSumInverseRoundInterval())) +
                       _("cards/day"))
            html += tr(_("In next week"), ("<b>%s</b> ") %
                       (fmtFloat(self.getWorkloadPeriod(7))) + _("cards/day"))
            html += tr(_("In next month"), ("<b>%s</b> ") %
                       (fmtFloat(self.getWorkloadPeriod(30))) + _("cards/day"))
            html += tr(_("In last week"), ("<b>%s</b> ") %
                       (fmtFloat(self.getPastWorkloadPeriod(7))) +
                       _("cards/day"))
            html += tr(_("In last month"), ("<b>%s</b> ") %
                       (fmtFloat(self.getPastWorkloadPeriod(30))) +
                       _("cards/day"))
            html += tr(_("In last 3 months"), ("<b>%s</b> ") %
                       (fmtFloat(self.getPastWorkloadPeriod(92))) +
                       _("cards/day"))
            html += tr(_("In last 6 months"), ("<b>%s</b> ") %
                       (fmtFloat(self.getPastWorkloadPeriod(182))) +
                       _("cards/day"))
            html += tr(_("In last year"), ("<b>%s</b> ") %
                       (fmtFloat(self.getPastWorkloadPeriod(365))) +
                       _("cards/day"))
            html += "</table>"

            html += "<br><br><b>" + _("Average Added") + "</b>"
            if sys.platform.startswith("darwin"):
                html += "<table width=250>"
            else:
                html += "<table width=200>"
            html += tr(
                _("Deck life"),
                _("<b>%(a)s</b>/day, <b>%(b)s</b>/mon") % {
                    'a': fmtFloat(self.newAverage()),
                    'b': fmtFloat(self.newAverage() * 30)
                })
            np = self.getNewPeriod(7)
            html += tr(
                _("In last week"),
                _("<b>%(a)d</b> (<b>%(b)s</b>/day)") %
                ({
                    'a': np,
                    'b': fmtFloat(np / float(7))
                }))
            np = self.getNewPeriod(30)
            html += tr(
                _("In last month"),
                _("<b>%(a)d</b> (<b>%(b)s</b>/day)") %
                ({
                    'a': np,
                    'b': fmtFloat(np / float(30))
                }))
            np = self.getNewPeriod(92)
            html += tr(
                _("In last 3 months"),
                _("<b>%(a)d</b> (<b>%(b)s</b>/day)") %
                ({
                    'a': np,
                    'b': fmtFloat(np / float(92))
                }))
            np = self.getNewPeriod(182)
            html += tr(
                _("In last 6 months"),
                _("<b>%(a)d</b> (<b>%(b)s</b>/day)") %
                ({
                    'a': np,
                    'b': fmtFloat(np / float(182))
                }))
            np = self.getNewPeriod(365)
            html += tr(
                _("In last year"),
                _("<b>%(a)d</b> (<b>%(b)s</b>/day)") %
                ({
                    'a': np,
                    'b': fmtFloat(np / float(365))
                }))
            html += "</table>"

            html += "<br><br><b>" + _("Average New Seen") + "</b>"
            if sys.platform.startswith("darwin"):
                html += "<table width=250>"
            else:
                html += "<table width=200>"
            np = self.getFirstPeriod(7)
            html += tr(
                _("In last week"),
                _("<b>%(a)d</b> (<b>%(b)s</b>/day)") %
                ({
                    'a': np,
                    'b': fmtFloat(np / float(7))
                }))
            np = self.getFirstPeriod(30)
            html += tr(
                _("In last month"),
                _("<b>%(a)d</b> (<b>%(b)s</b>/day)") %
                ({
                    'a': np,
                    'b': fmtFloat(np / float(30))
                }))
            np = self.getFirstPeriod(92)
            html += tr(
                _("In last 3 months"),
                _("<b>%(a)d</b> (<b>%(b)s</b>/day)") %
                ({
                    'a': np,
                    'b': fmtFloat(np / float(92))
                }))
            np = self.getFirstPeriod(182)
            html += tr(
                _("In last 6 months"),
                _("<b>%(a)d</b> (<b>%(b)s</b>/day)") %
                ({
                    'a': np,
                    'b': fmtFloat(np / float(182))
                }))
            np = self.getFirstPeriod(365)
            html += tr(
                _("In last year"),
                _("<b>%(a)d</b> (<b>%(b)s</b>/day)") %
                ({
                    'a': np,
                    'b': fmtFloat(np / float(365))
                }))
            html += "</table>"

            html += "<br><br><b>" + _("Card Ease") + "</b><br>"
            html += _("Lowest factor: %.2f") % d.s.scalar(
                "select min(factor) from cards") + "<br>"
            html += _("Average factor: %.2f") % d.s.scalar(
                "select avg(factor) from cards") + "<br>"
            html += _("Highest factor: %.2f") % d.s.scalar(
                "select max(factor) from cards") + "<br>"

            html = runFilter("deckStats", html)
        return html
Ejemplo n.º 3
0
    def report(self):
        "Return an HTML string with a report."
        fmtPerc = oldanki.utils.fmtPercentage
        fmtFloat = oldanki.utils.fmtFloat
        if self.deck.isEmpty():
            return _("Please add some cards first.") + "<p/>"
        d = self.deck
        html="<h1>" + _("Deck Statistics") + "</h1>"
        html += _("Deck created: <b>%s</b> ago<br>") % self.createdTimeStr()
        total = d.cardCount
        new = d.newCountAll()
        young = d.youngCardCount()
        old = d.matureCardCount()
        newP = new / float(total) * 100
        youngP = young / float(total) * 100
        oldP = old / float(total) * 100
        stats = d.getStats()
        (stats["new"], stats["newP"]) = (new, newP)
        (stats["old"], stats["oldP"]) = (old, oldP)
        (stats["young"], stats["youngP"]) = (young, youngP)
        html += _("Total number of cards:") + " <b>%d</b><br>" % total
        html += _("Total number of facts:") + " <b>%d</b><br><br>" % d.factCount

        html += "<b>" + _("Card Maturity") + "</b><br>"
        html += _("Mature cards: <!--card count-->") + " <b>%(old)d</b> (%(oldP)s)<br>" % {
                'old': stats['old'], 'oldP' : fmtPerc(stats['oldP'])}
        html += _("Young cards: <!--card count-->") + " <b>%(young)d</b> (%(youngP)s)<br>" % {
                'young': stats['young'], 'youngP' : fmtPerc(stats['youngP'])}
        html += _("Unseen cards:") + " <b>%(new)d</b> (%(newP)s)<br>" % {
                'new': stats['new'], 'newP' : fmtPerc(stats['newP'])}
        avgInt = self.getAverageInterval()
        if avgInt:
            html += _("Average interval: ") + ("<b>%s</b> ") % fmtFloat(avgInt) + _("days")
            html += "<br>"
        html += "<br>"
        html += "<b>" + _("Correct Answers") + "</b><br>"
        html += _("Mature cards: <!--correct answers-->") + " <b>" + fmtPerc(stats['gMatureYes%']) + (
                "</b> " + _("(%(partOf)d of %(totalSum)d)") % {
                'partOf' : stats['gMatureYes'],
                'totalSum' : stats['gMatureTotal'] } + "<br>")
        html += _("Young cards: <!--correct answers-->")  + " <b>" + fmtPerc(stats['gYoungYes%']) + (
                "</b> " + _("(%(partOf)d of %(totalSum)d)") % {
                'partOf' : stats['gYoungYes'],
                'totalSum' : stats['gYoungTotal'] } + "<br>")
        html += _("First-seen cards:") + " <b>" + fmtPerc(stats['gNewYes%']) + (
                "</b> " + _("(%(partOf)d of %(totalSum)d)") % {
                'partOf' : stats['gNewYes'],
                'totalSum' : stats['gNewTotal'] } + "<br><br>")

        # average pending time
        existing = d.cardCount - d.newCountToday
        def tr(a, b):
            return "<tr><td>%s</td><td align=right>%s</td></tr>" % (a, b)
        def repsPerDay(reps,days):
            retval =  ("<b>%d</b> " % reps)  + ngettext("rep", "reps", reps)
            retval += ("/<b>%d</b> " % days) + ngettext("day", "days", days)
            return retval
        if existing and avgInt:
            html += "<b>" + _("Recent Work") + "</b>"
            if sys.platform.startswith("darwin"):
                html += "<table width=250>"
            else:
                html += "<table width=200>"
            html += tr(_("In last week"), repsPerDay(
                self.getRepsDone(-7, 0),
                self.getDaysReviewed(-7, 0)))
            html += tr(_("In last month"), repsPerDay(
                self.getRepsDone(-30, 0),
                self.getDaysReviewed(-30, 0)))
            html += tr(_("In last 3 months"), repsPerDay(
                self.getRepsDone(-92, 0),
                self.getDaysReviewed(-92, 0)))
            html += tr(_("In last 6 months"), repsPerDay(
                self.getRepsDone(-182, 0),
                self.getDaysReviewed(-182, 0)))
            html += tr(_("In last year"), repsPerDay(
                self.getRepsDone(-365, 0),
                self.getDaysReviewed(-365, 0)))
            html += tr(_("Deck life"), repsPerDay(
                self.getRepsDone(-13000, 0),
                self.getDaysReviewed(-13000, 0)))
            html += "</table>"

            html += "<br><br><b>" + _("Average Daily Reviews") + "</b>"
            if sys.platform.startswith("darwin"):
                html += "<table width=250>"
            else:
                html += "<table width=200>"
            html += tr(_("Deck life"), ("<b>%s</b> ") % (
                fmtFloat(self.getSumInverseRoundInterval())) + _("cards/day"))
            html += tr(_("In next week"), ("<b>%s</b> ") % (
                fmtFloat(self.getWorkloadPeriod(7))) + _("cards/day"))
            html += tr(_("In next month"), ("<b>%s</b> ") % (
                fmtFloat(self.getWorkloadPeriod(30))) + _("cards/day"))
            html += tr(_("In last week"), ("<b>%s</b> ") % (
                fmtFloat(self.getPastWorkloadPeriod(7))) + _("cards/day"))
            html += tr(_("In last month"), ("<b>%s</b> ") % (
                fmtFloat(self.getPastWorkloadPeriod(30))) + _("cards/day"))
            html += tr(_("In last 3 months"), ("<b>%s</b> ") % (
                fmtFloat(self.getPastWorkloadPeriod(92))) + _("cards/day"))
            html += tr(_("In last 6 months"), ("<b>%s</b> ") % (
                fmtFloat(self.getPastWorkloadPeriod(182))) + _("cards/day"))
            html += tr(_("In last year"), ("<b>%s</b> ") % (
                fmtFloat(self.getPastWorkloadPeriod(365))) + _("cards/day"))
            html += "</table>"

            html += "<br><br><b>" + _("Average Added") + "</b>"
            if sys.platform.startswith("darwin"):
                html += "<table width=250>"
            else:
                html += "<table width=200>"
            html += tr(_("Deck life"), _("<b>%(a)s</b>/day, <b>%(b)s</b>/mon") % {
                'a': fmtFloat(self.newAverage()), 'b': fmtFloat(self.newAverage()*30)})
            np = self.getNewPeriod(7)
            html += tr(_("In last week"), _("<b>%(a)d</b> (<b>%(b)s</b>/day)") % (
                {'a': np, 'b': fmtFloat(np / float(7))}))
            np = self.getNewPeriod(30)
            html += tr(_("In last month"), _("<b>%(a)d</b> (<b>%(b)s</b>/day)") % (
                {'a': np, 'b': fmtFloat(np / float(30))}))
            np = self.getNewPeriod(92)
            html += tr(_("In last 3 months"), _("<b>%(a)d</b> (<b>%(b)s</b>/day)") % (
                {'a': np, 'b': fmtFloat(np / float(92))}))
            np = self.getNewPeriod(182)
            html += tr(_("In last 6 months"), _("<b>%(a)d</b> (<b>%(b)s</b>/day)") % (
                {'a': np, 'b': fmtFloat(np / float(182))}))
            np = self.getNewPeriod(365)
            html += tr(_("In last year"), _("<b>%(a)d</b> (<b>%(b)s</b>/day)") % (
                {'a': np, 'b': fmtFloat(np / float(365))}))
            html += "</table>"

            html += "<br><br><b>" + _("Average New Seen") + "</b>"
            if sys.platform.startswith("darwin"):
                html += "<table width=250>"
            else:
                html += "<table width=200>"
            np = self.getFirstPeriod(7)
            html += tr(_("In last week"), _("<b>%(a)d</b> (<b>%(b)s</b>/day)") % (
                {'a': np, 'b': fmtFloat(np / float(7))}))
            np = self.getFirstPeriod(30)
            html += tr(_("In last month"), _("<b>%(a)d</b> (<b>%(b)s</b>/day)") % (
                {'a': np, 'b': fmtFloat(np / float(30))}))
            np = self.getFirstPeriod(92)
            html += tr(_("In last 3 months"), _("<b>%(a)d</b> (<b>%(b)s</b>/day)") % (
                {'a': np, 'b': fmtFloat(np / float(92))}))
            np = self.getFirstPeriod(182)
            html += tr(_("In last 6 months"), _("<b>%(a)d</b> (<b>%(b)s</b>/day)") % (
                {'a': np, 'b': fmtFloat(np / float(182))}))
            np = self.getFirstPeriod(365)
            html += tr(_("In last year"), _("<b>%(a)d</b> (<b>%(b)s</b>/day)") % (
                {'a': np, 'b': fmtFloat(np / float(365))}))
            html += "</table>"

            html += "<br><br><b>" + _("Card Ease") + "</b><br>"
            html += _("Lowest factor: %.2f") % d.s.scalar(
                "select min(factor) from cards") + "<br>"
            html += _("Average factor: %.2f") % d.s.scalar(
                "select avg(factor) from cards") + "<br>"
            html += _("Highest factor: %.2f") % d.s.scalar(
                "select max(factor) from cards") + "<br>"

            html = runFilter("deckStats", html)
        return html