def main(args): parser = argparse.ArgumentParser(description='Test a redditbot') parser.add_argument('file', type=file, help='file to use') parser.add_argument('--debug', action='store_true', default=False) parser.add_argument('--username', type=str, help='username to run with') parser.add_argument('--password', type=str, help='password to run with') parser.add_argument('--sandbox', action='store_true', default=False, help='Run the bot inside the RestrictedPython sandbox') options = parser.parse_args(args[1:]) if options.debug: logging.basicConfig(level=logging.DEBUG) else: logging.basicConfig(level=logging.INFO) class TestBox(redditbots.TestingSandbox): def username(self): return options.username def password(self): return options.password class SecureTestBox(redditbots.BotSandbox): def username(self): return options.username def password(self): return options.password if options.sandbox: botManager = redditbots.BotManager(SecureTestBox) else: botManager = redditbots.BotManager(TestBox) botManager.loadBot(options.file) botManager.triggerInit() r = botManager.getReddit() try: while True: r_all = r.get_subreddit('all').get_new_by_date() print "Processing new submissions in /r/all" for post in r_all: botManager.triggerNewSubmission(post) print "Processing new comments" for comment in r.get_all_comments(): botManager.triggerNewComment(comment) nextProcess = 0 while nextProcess < sys.maxint and nextProcess >= 0: nextProcess = botManager.processReplyQueue() now = monoclock.nano_count()/1000000000 print "Processing replies in %i seconds"%(nextProcess-now) if nextProcess-now > 1: time.sleep(nextProcess-now) except urllib2.URLError, e: print "Network error:", e
def test_nano_count_getsBigger(self): values = [] for n in range(10000): values.append(monoclock.nano_count()) # Values either get bigger or stay the same self.assertEqual(values, sorted(values)) # None of the values should be the same self.assertEqual(len(values), len(set(values)))
def test_nano_count_getsBigger(self): values = [] for n in xrange(10000): values.append(monoclock.nano_count()) # Values either get bigger or stay the same self.assertEqual(values, sorted(values)) # None of the values should be the same self.assertEqual(len(values), len(set(values)))
def processReplyQueue(self): now = monoclock.nano_count()/1000000000 if now > self._nextReplyTime: for pair in self._replyQueue: post, reply = pair self.login() try: ret = post.reply(reply) self._replyQueue.remove(pair) except RateLimitExceeded, e: print "Rate limit exceeded, waiting another 10 minutes." self._nextReplyTime = now+60*10 return self._nextReplyTime
def test_wrongArguments(self): self.assertRaises(TypeError, lambda: monoclock.nano_count(1)) self.assertRaises(TypeError, lambda: monoclock.nano_count(None)) self.assertRaises(TypeError, lambda: monoclock.nano_count([])) self.assertRaises(TypeError, lambda: monoclock.nano_count({})) self.assertRaises(TypeError, lambda: monoclock.nano_count(keyword=1))
def test_nano_count_isCorrectType(self): val1 = monoclock.nano_count() # In reality it is really always a long, I think. self.assertTrue(isinstance(val1, int))
def test_nano_count_isCorrectType(self): val1 = monoclock.nano_count() # In reality it is really always a long, I think. self.assertTrue(isinstance(val1, (int, long)))
import monoclock t = monoclock.nano_count() t = t/1000000 print t