-
-
Save imakewebthings/7952598 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
class FileHandler(object): | |
def __init__(self, filename = "", query_type = ""): | |
self.filename = filename | |
self.query_type = query_type | |
def line_from_file(self, filename=None, query_type=None): | |
if filename != None | |
self.filename = filename | |
if query_type != None | |
self.query_type = query_type | |
data = [ line.rstrip() for line in open(self.filename) if not line.isspace() ] | |
if (self.query_type == "all"): | |
return data # Return file as an array | |
elif (query_type == "string"): | |
return "".join(data) # Return file as a string | |
elif (self.query_type == "first"): | |
return data[0] # Return first line | |
elif (self.query_type == "random" or self.query_type == "rand"): | |
return random.choice(data) # Return random line | |
if __name__ == '__main__': | |
# I want this to set f.filename to "froge-data.txt" and f.query_type to "first", but they remain empty strings | |
f = FileHandler() | |
print f.line_from_file("froge_data.txt", "first") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment