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

This commit is contained in:
Brian S. Stephan 2010-07-26 21:06:59 -05:00
parent 3037364ef9
commit 5927d361ef
1 changed files with 2 additions and 8 deletions

View File

@ -545,14 +545,8 @@ class FactFile(Module):
if os.path.isfile(filename): if os.path.isfile(filename):
# http://www.regexprn.com/2008/11/read-random-line-in-large-file-in.html # http://www.regexprn.com/2008/11/read-random-line-in-large-file-in.html
file = open(filename, 'r') file = open(filename, 'r')
count = 0 facts = file.readlines()
to_print = cur = file.readline() to_print = facts[random.randint(0, len(facts))]
while cur:
cur = file.readline()
count += 1
if random.randint(0, count) == 0:
# 1/count chance to replace
to_print = cur
# return text # return text
if replypath is None: if replypath is None: