def check_attempt(self, test): now = int(time.time()) last_attempt = get(test.name, 'last_attempt', now) attempts = get(test.name, 'attempts', 0) elapsed = now - last_attempt hint = self.hints[attempts % len(self.hints)] if attempts and hint.cooldown > elapsed: files = ' '.join(self.assignment.src) if elapsed <= RETRY_THRESHOLD * hint.cooldown: raise EarlyExit(COOLDOWN_MSG + hint.text.format(files=files)) else: raise EarlyExit(hint.text.format(files=files) + RETRY_MSG) return now, attempts + 1
def check_attempt(self, test): now = int(time.time()) last_attempt = get(test.name, 'last_attempt', now) attempts = get(test.name, 'attempts', 0) secs_elapsed = now - last_attempt cooldown_time = self.cooldown[attempts] if attempts < len( self.cooldown) else self.cooldown[-1] cooldown = cooldown_time - secs_elapsed if attempts and cooldown > 0: files = ' '.join(self.assignment.src) raise EarlyExit( COOLDOWN_MSG.format(wait=cooldown, question=test.name.lower(), tries=attempts, files=files)) return now, attempts + 1
def make_attempt(self, test, attempts, succeeds=True): self.assignment.specified_tests = [test] if not succeeds: with self.assertRaises(EarlyExit): self.callRun() else: self.callRun() self.assertTrue( storage.get(test.name, 'attempts', 0) == attempts, 'attempts not correct')
def run(self, messages): if self.args.score or self.args.unlock or self.args.testing: return analytics = {} tests = self.assignment.specified_tests for test in tests: if get(test.name, 'correct', default=False): continue # suppress rate limiting if question is correct last_attempt, attempts = self.check_attempt(test) analytics[test.name] = { 'attempts': store(test.name, 'attempts', attempts), 'last_attempt': store(test.name, 'last_attempt', last_attempt), } messages['rate_limit'] = analytics