Skip to content

Instantly share code, notes, and snippets.

@cshirky
Last active December 31, 2015 07:09
Show Gist options
  • Save cshirky/7952423 to your computer and use it in GitHub Desktop.
Save cshirky/7952423 to your computer and use it in GitHub Desktop.
Simple object error
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, query_type):
data = [ line.rstrip() for line in open(self.filename) if not line.isspace() ]
query_type = self.query_type
query_type = query_type.lower()
if (query_type == "all"):
return data # Return file as an array
elif (query_type == "string"):
return "".join(data) # Return file as a string
elif (query_type == "first"):
return data[0] # Return first line
elif (query_type == "random" or 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