patternpythonMinor
Random Topic Generator
Viewed 0 times
randomgeneratortopic
Problem
I've written a python module that randomly generates a list of ideas that
can be used as the premises for furthur research. For instance, it would be useful in situations where a new thesis topic is required, either for a masters or a doctorate program. Or if someone is just simply bored with their lives and wants some brainstorming ideas about the kind of project that they would like to be involved in, this python module can be used.
The output is a list of random ideas or technologies. It is up to the user as to how they interpret the output. Each idea in the list can be assessed individually, or the ideas can be combined in ways never thought off before to create something new.
I would like some input on how else I can improve the project or what other features I may add. The project is hosted on github. The name of the project is ranto for lack of a better name.
For the impatient, here is the code from the sole python file in the project...
If you want to test it out, you need to download one of the data files from the github repo linked earlier.
Any feedback would be appreciated.
can be used as the premises for furthur research. For instance, it would be useful in situations where a new thesis topic is required, either for a masters or a doctorate program. Or if someone is just simply bored with their lives and wants some brainstorming ideas about the kind of project that they would like to be involved in, this python module can be used.
The output is a list of random ideas or technologies. It is up to the user as to how they interpret the output. Each idea in the list can be assessed individually, or the ideas can be combined in ways never thought off before to create something new.
I would like some input on how else I can improve the project or what other features I may add. The project is hosted on github. The name of the project is ranto for lack of a better name.
For the impatient, here is the code from the sole python file in the project...
import random
import sys
def topics(f):
f = open(f, 'r')
wordlist = []
for i in f:
wordlist.append(i.strip())
return wordlist
def mainp():
wordlist = topics('../data/' + sys.argv[1])
while True:
print random.sample(wordlist, int(sys.argv[2]))
if raw_input() == '':
continue
else:
break
mainp()If you want to test it out, you need to download one of the data files from the github repo linked earlier.
Any feedback would be appreciated.
Solution
I can't tell from your question whether you are wanting ideas to improve this python code or the project itself. Your code above works and is readable, so take the following re-spelling of it as minor nitpicks.
You may want to peruse the HOWTO for generator functions.
def topics(f):
for i in open(f): # inline the call to open(), and drop the implicit 'r'.
yield i.strip() # "yield" turns this into a generator function that will only
# read as many lines of the file as are requested.
def mainp(): # consider renaming to something more descriptive (generate_topics?)
wordlist = topics('../data/' + sys.argv[1])
response = True # Or any "truthy" value that drops us into the while loop one time.
while response:
print random.sample(wordlist, int(sys.argv[2]))
response = raw_input()
if __name__ == "__main__": # Putting in this if guard allows you to import your
mainp() # functions above from anywhere else you might want them.You may want to peruse the HOWTO for generator functions.
Code Snippets
def topics(f):
for i in open(f): # inline the call to open(), and drop the implicit 'r'.
yield i.strip() # "yield" turns this into a generator function that will only
# read as many lines of the file as are requested.
def mainp(): # consider renaming to something more descriptive (generate_topics?)
wordlist = topics('../data/' + sys.argv[1])
response = True # Or any "truthy" value that drops us into the while loop one time.
while response:
print random.sample(wordlist, int(sys.argv[2]))
response = raw_input()
if __name__ == "__main__": # Putting in this if guard allows you to import your
mainp() # functions above from anywhere else you might want them.Context
StackExchange Code Review Q#3295, answer score: 2
Revisions (0)
No revisions yet.