From 5927d361ef4cd920a5348fb4432386a36af2b93e Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Mon, 26 Jul 2010 21:06:59 -0500 Subject: [PATCH] bite the bullet and load the entire file into memory rather than doing the fun only-load-one-line trick, so that it's easier to search with a regex later --- dr.botzo.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/dr.botzo.py b/dr.botzo.py index 8dd0a98..099e943 100755 --- a/dr.botzo.py +++ b/dr.botzo.py @@ -545,14 +545,8 @@ class FactFile(Module): if os.path.isfile(filename): # http://www.regexprn.com/2008/11/read-random-line-in-large-file-in.html file = open(filename, 'r') - count = 0 - to_print = cur = file.readline() - while cur: - cur = file.readline() - count += 1 - if random.randint(0, count) == 0: - # 1/count chance to replace - to_print = cur + facts = file.readlines() + to_print = facts[random.randint(0, len(facts))] # return text if replypath is None: