Getting random lines from txt files
Working on my whispering wall the other day, I wanted a much better way to 'randomly' select lines it would give back to the user. random.shuffle seemed like a better way.
Thanks again to the interwebs for the tools to search up the things I need.
I wasn't sure how to properly handle the global aspect of 'LINE_GET' for this piece. LINE_GET is going to be the integer I use to select a number from the list that is made, that is then in turn used to get a 'random' line from my txt file.
In my whispering wall, this variable would be a part of the WhisperingWall class. And it's alteration would be internal, but for the sake of making this blog post I needed it to be easy to access and alter. And globals, well, everyone says they are bad. I may be writing completely horrible code for that part. I am not sure, but hey, I didn't use a global! kinda... sorta....
Anyway!
two methods, a class(**cough global**), and a system argument (sys.argv), that can more randomly select a line from a txt file and return it.
Here's the code:
####### random_script_picker.py ######
#!usr/bin/python3
import random
import sys
text_file = sys.argv[1]
#LINE_GET = 0
class NotGlobal(object):
line_get = 0
def create_script_line_order(somescript):
""" make a list with randomized order of line numbers from script
using random's shuffle."""
count = None
#print(somescript) I do love print statements.
try: #if txt file is empty, i will not be created
with open(somescript) as f:
for i, l in enumerate(f):
pass
# if i is not created, this is an UnboundLocalError
count = i
except:
# Empty will be returned and get_script_line will call ValueError
return "EMPTY"
if count != None:
first_list = []
# create a list with all line numbers in it
for x in range(1, i):
first_list.append(x)
# shuffle those items:
random.shuffle(first_list)
return first_list
def get_script_line(arg):
""" Chooses a random script line to give back to user """
#print(LINE_GET.line_get)
if arg.endswith('txt'):
order = create_script_line_order(arg)
else:
# when order is None, a Value error will be raised in else clause
order = None
if order == "EMPTY":
print("***txt file is empty*** \n\t", arg)
raise ValueError
elif order != None:
if LINE_GET.line_get >= len(order):
LINE_GET.line_get = 0
get_line = order[LINE_GET.line_get]
with open(arg) as f:
lines = f.readlines()
x = int(get_line)
#print(lines[x])
LINE_GET.line_get += 1
return lines[x]
else:
print("file must be a .txt for order to be created")
print("arg passed in = ", arg)
raise ValueError
LINE_GET = NotGlobal()
# to show it working:
for i in range(0, 5):
x = get_script_line(text_file)
print("\n", x)
##### results: #####
### the text file is a chunk of Monty Pythons Holy Grail I was modifying to
### try and train Wiwa's NLTK pos_tagger with.
Thanks again to the interwebs for the tools to search up the things I need.
I wasn't sure how to properly handle the global aspect of 'LINE_GET' for this piece. LINE_GET is going to be the integer I use to select a number from the list that is made, that is then in turn used to get a 'random' line from my txt file.
In my whispering wall, this variable would be a part of the WhisperingWall class. And it's alteration would be internal, but for the sake of making this blog post I needed it to be easy to access and alter. And globals, well, everyone says they are bad. I may be writing completely horrible code for that part. I am not sure, but hey, I didn't use a global! kinda... sorta....
Anyway!
two methods, a class(**cough global**), and a system argument (sys.argv), that can more randomly select a line from a txt file and return it.
Here's the code:
####### random_script_picker.py ######
#!usr/bin/python3
import random
import sys
text_file = sys.argv[1]
#LINE_GET = 0
class NotGlobal(object):
line_get = 0
def create_script_line_order(somescript):
""" make a list with randomized order of line numbers from script
using random's shuffle."""
count = None
#print(somescript) I do love print statements.
try: #if txt file is empty, i will not be created
with open(somescript) as f:
for i, l in enumerate(f):
pass
# if i is not created, this is an UnboundLocalError
count = i
except:
# Empty will be returned and get_script_line will call ValueError
return "EMPTY"
if count != None:
first_list = []
# create a list with all line numbers in it
for x in range(1, i):
first_list.append(x)
# shuffle those items:
random.shuffle(first_list)
return first_list
def get_script_line(arg):
""" Chooses a random script line to give back to user """
#print(LINE_GET.line_get)
if arg.endswith('txt'):
order = create_script_line_order(arg)
else:
# when order is None, a Value error will be raised in else clause
order = None
if order == "EMPTY":
print("***txt file is empty*** \n\t", arg)
raise ValueError
elif order != None:
if LINE_GET.line_get >= len(order):
LINE_GET.line_get = 0
get_line = order[LINE_GET.line_get]
with open(arg) as f:
lines = f.readlines()
x = int(get_line)
#print(lines[x])
LINE_GET.line_get += 1
return lines[x]
else:
print("file must be a .txt for order to be created")
print("arg passed in = ", arg)
raise ValueError
LINE_GET = NotGlobal()
# to show it working:
for i in range(0, 5):
x = get_script_line(text_file)
print("\n", x)
##### results: #####
### the text file is a chunk of Monty Pythons Holy Grail I was modifying to
### try and train Wiwa's NLTK pos_tagger with.
Comments
Post a Comment