From dcf097b55f1fbfcca26605725f63c3a8c093bef0 Mon Sep 17 00:00:00 2001 From: Mike Blume Date: Sun, 5 May 2013 10:53:23 -0700 Subject: [PATCH] use futuristic division Python used to use C-style division, where division of two ints was truncated, and division involving a float resulted in a float. This is confusing, because you often can't tell from looking at a line of code in isolation what sort of division it's supposed to do. With 'from __future__ import division' Python ensures that division is always explicit. // means (floored) integer division / means float division regardless of argument types. This should make the source a bit clearer now, as well as removing one obstacle if Anki is ever ported to Python 3. --- anki/sched.py | 27 ++++++++++++++------------- anki/utils.py | 11 ++++++----- aqt/reviewer.py | 2 +- aqt/sync.py | 4 ++-- 4 files changed, 23 insertions(+), 21 deletions(-) diff --git a/anki/sched.py b/anki/sched.py index cce43dfa3..564305a0d 100644 --- a/anki/sched.py +++ b/anki/sched.py @@ -2,6 +2,7 @@ # Copyright: Damien Elmes # License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html +from __future__ import division import time, random, itertools from operator import itemgetter from heapq import * @@ -88,7 +89,7 @@ class Scheduler(object): if card: idx = self.countIdx(card) if idx == 1: - counts[1] += card.left/1000 + counts[1] += card.left // 1000 else: counts[idx] += 1 return tuple(counts) @@ -368,7 +369,7 @@ select id, due from cards where did = ? and queue = 0 limit ?""", did, lim) if self.col.conf['newSpread'] == NEW_CARDS_DISTRIBUTE: if self.newCount: self.newCardModulus = ( - (self.newCount + self.revCount) / self.newCount) + (self.newCount + self.revCount) // self.newCount) # if there are cards to review, ensure modulo >= 2 if self.revCount: self.newCardModulus = max(2, self.newCardModulus) @@ -467,7 +468,7 @@ limit %d""" % (self._deckLimit(), self.reportLimit), lim=self.dayCutoff) if self._lrnQueue[0][0] < cutoff: id = heappop(self._lrnQueue)[1] card = self.col.getCard(id) - self.lrnCount -= card.left/1000 + self.lrnCount -= card.left // 1000 return card # daily learning @@ -545,7 +546,7 @@ did = ? and queue = 3 and due <= ? limit ?""", card.due = int(time.time() + delay) # due today? if card.due < self.dayCutoff: - self.lrnCount += card.left/1000 + self.lrnCount += card.left // 1000 # if the queue is not empty and there's nothing else to do, make # sure we don't put it at the head of the queue and end up showing # it twice in a row @@ -557,7 +558,7 @@ did = ? and queue = 3 and due <= ? limit ?""", else: # the card is due in one or more days, so we need to use the # day learn queue - ahead = ((card.due - self.dayCutoff) / 86400) + 1 + ahead = ((card.due - self.dayCutoff) // 86400) + 1 card.due = self.today + ahead card.queue = 3 self._logLrn(card, ease, conf, leaving, type, lastLeft) @@ -811,12 +812,12 @@ select id from cards where did in %s and queue = 2 and due <= ? limit ?)""" card.left = self._startingLeft(card) # queue 1 if card.due < self.dayCutoff: - self.lrnCount += card.left/1000 + self.lrnCount += card.left // 1000 card.queue = 1 heappush(self._lrnQueue, (card.due, card.id)) else: # day learn queue - ahead = ((card.due - self.dayCutoff) / 86400) + 1 + ahead = ((card.due - self.dayCutoff) // 86400) + 1 card.due = self.today + ahead card.queue = 3 return delay @@ -860,9 +861,9 @@ select id from cards where did in %s and queue = 2 and due <= ? limit ?)""" "Ideal next interval for CARD, given EASE." delay = self._daysLate(card) conf = self._revConf(card) - fct = card.factor / 1000.0 - ivl2 = self._constrainedIvl((card.ivl + delay/4) * 1.2, conf, card.ivl) - ivl3 = self._constrainedIvl((card.ivl + delay/2) * fct, conf, ivl2) + fct = card.factor / 1000 + ivl2 = self._constrainedIvl((card.ivl + delay // 4) * 1.2, conf, card.ivl) + ivl3 = self._constrainedIvl((card.ivl + delay // 2) * fct, conf, ivl2) ivl4 = self._constrainedIvl( (card.ivl + delay) * fct * conf['ease4'], conf, ivl3) if ease == 2: @@ -1024,7 +1025,7 @@ did = ?, queue = %s, due = ?, mod = ?, usn = ? where id = ?""" % queue, data) assert card.odid and card.type == 2 assert card.factor elapsed = card.ivl - (card.odue - self.today) - factor = ((card.factor/1000.0)+1.2)/2.0 + factor = ((card.factor/1000)+1.2)/2 ivl = int(max(card.ivl, elapsed * factor, 1)) conf = self._revConf(card) return min(conf['maxIvl'], ivl) @@ -1039,7 +1040,7 @@ did = ?, queue = %s, due = ?, mod = ?, usn = ? where id = ?""" % queue, data) return # if over threshold or every half threshold reps after that if (card.lapses >= lf and - (card.lapses-lf) % (max(lf/2, 1)) == 0): + (card.lapses-lf) % (max(lf // 2, 1)) == 0): # add a leech tag f = card.note() f.addTag("leech") @@ -1124,7 +1125,7 @@ did = ?, queue = %s, due = ?, mod = ?, usn = ? where id = ?""" % queue, data) def _updateCutoff(self): # days since col created - self.today = int((time.time() - self.col.crt) / 86400) + self.today = (time.time() - self.col.crt) // 86400 # end of day cutoff self.dayCutoff = self.col.crt + (self.today+1)*86400 # update all daily counts, but don't save decks to prevent needless diff --git a/anki/utils.py b/anki/utils.py index fb0a55973..238a86fb6 100644 --- a/anki/utils.py +++ b/anki/utils.py @@ -2,6 +2,7 @@ # Copyright: Damien Elmes # License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html +from __future__ import division import re, os, random, time, math, htmlentitydefs, subprocess, \ tempfile, shutil, string, httplib2, sys, locale from hashlib import sha1 @@ -90,15 +91,15 @@ def convertSecondsTo(seconds, type): if type == "seconds": return seconds elif type == "minutes": - return seconds / 60.0 + return seconds / 60 elif type == "hours": - return seconds / 3600.0 + return seconds / 3600 elif type == "days": - return seconds / 86400.0 + return seconds / 86400 elif type == "months": - return seconds / 2592000.0 + return seconds / 2592000 elif type == "years": - return seconds / 31536000.0 + return seconds / 31536000 assert False def _pluralCount(time, point): diff --git a/aqt/reviewer.py b/aqt/reviewer.py index b629bd29a..d0c22ba0e 100644 --- a/aqt/reviewer.py +++ b/aqt/reviewer.py @@ -557,7 +557,7 @@ function showAnswer(txt) { """ % dict(rem=self._remaining(), edit=_("Edit"), editkey=_("Shortcut key: %s") % "E", - more=_("More"), time=self.card.timeTaken()/1000) + more=_("More"), time=self.card.timeTaken() // 1000) def _showAnswerButton(self): self._bottomReady = True diff --git a/aqt/sync.py b/aqt/sync.py index 10990a389..4f8159c04 100644 --- a/aqt/sync.py +++ b/aqt/sync.py @@ -64,8 +64,8 @@ automatically.""")) self.mw.progress.update(label="%s\n%s" % ( self.label, _("%(a)dkB up, %(b)dkB down") % dict( - a=self.sentBytes/1024, - b=self.recvBytes/1024))) + a=self.sentBytes // 1024, + b=self.recvBytes // 1024))) def onEvent(self, evt, *args): pu = self.mw.progress.update