Commit 295faf9e authored by Campbell Barton's avatar Campbell Barton

tweaks to file line reading

parent be4b420c
...@@ -28,7 +28,7 @@ bl_info = { ...@@ -28,7 +28,7 @@ bl_info = {
} }
def find_random_line(f): def read_random_line(f):
import os import os
chunk_size = 16 chunk_size = 16
...@@ -36,31 +36,28 @@ def find_random_line(f): ...@@ -36,31 +36,28 @@ def find_random_line(f):
import random import random
f_handle.seek(0, os.SEEK_END) f_handle.seek(0, os.SEEK_END)
size = f_handle.tell() size = f_handle.tell()
i = random.randint(1, size) i = random.randint(0, size)
while True:
# find the line
while i != 0:
i -= chunk_size i -= chunk_size
if i < 0: if i < 0:
chunk_size += i
i = 0 i = 0
f_handle.seek(i, os.SEEK_SET) f_handle.seek(i, os.SEEK_SET)
d = f_handle.read(chunk_size) d = f_handle.read(chunk_size)
i_newline = d.rfind(b'\n') i_newline = d.rfind(b'\n')
if i_newline == -1: if i_newline != -1:
pass
else:
i += i_newline + 1 i += i_newline + 1
break break
if i == 0:
break
f_handle.seek(i, os.SEEK_SET) f_handle.seek(i, os.SEEK_SET)
data = f_handle.readline() return f_handle.readline()
return data
def find_random_tip(): def find_random_tip():
import os import os
text = find_random_line( text = read_random_line(
os.path.join(os.path.dirname(__file__), "tips.txt"), os.path.join(os.path.dirname(__file__), "tips.txt"),
).rstrip() ).rstrip()
...@@ -69,8 +66,6 @@ def find_random_tip(): ...@@ -69,8 +66,6 @@ def find_random_tip():
text, url = text[:url_index], text[url_index + 2:] text, url = text[:url_index], text[url_index + 2:]
else: else:
url = b'' url = b''
print(text)
print(url)
return text.decode("utf-8"), url.decode("utf-8") return text.decode("utf-8"), url.decode("utf-8")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment