Skip to content

Instantly share code, notes, and snippets.

@imakewebthings
Forked from cshirky/Simple object error
Last active December 31, 2015 07:09
Show Gist options
  • Save imakewebthings/7952598 to your computer and use it in GitHub Desktop.
Save imakewebthings/7952598 to your computer and use it in GitHub Desktop.
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