Skip to content

Instantly share code, notes, and snippets.

@cshirky
Created December 14, 2013 16:24
Show Gist options
  • Save cshirky/7961317 to your computer and use it in GitHub Desktop.
Save cshirky/7961317 to your computer and use it in GitHub Desktop.
### Here's FileHandler.py
#!/usr/bin/env python
import random
class FileHandler(object):
def __init__(self):
pass
def line_from_file(self, filename, query_type):
data = [ line.rstrip() for line in open(filename) if not line.isspace() ]
query_type = query_type.lower()
if (query_type == "random" or query_type == "rand"):
return random.choice(data) # Return random line
### And here's the test script
import FileHandler
f = FileHandler()
print f.line_from_file("meatball-answers.txt", "random")
#### But when I'm all like...
# cshirky$ python import_test.py
### ...Python's all like...
#Traceback (most recent call last):
# File "import_test.py", line 3, in <module>
# f = FileHandler()
# TypeError: 'module' object is not callable
### ...and same error with the 'from FileHandler import line_from_file' syntax
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment