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:
parent
3037364ef9
commit
5927d361ef
10
dr.botzo.py
10
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:
|
||||
|
|
Loading…
Reference in New Issue