I tried this class a couple of different ways and I had it print just memory location I think one way now I have an error that says: Traceback (most recent call last):
File "F:\Python_Stuff\CSC143-901\Lexicon.py", line 83, in <module>
source = Lexicon.OpenFile('word')
TypeError: unbound method OpenFile() must be called with Lexicon instance as first argument (got str instance instead)
I think if I can get the open and read part working the rest should be easy to figure out and I am using just a simple txt file called word to do the operations on and I included that as an attachment. This is a course in Object Oriented programming and the python language is just a tool so we dont talk about it in class and the instructor does not seem to want to teach it he says he is not teaching the language so that makes it difficult. The book Object Oriented Programming with Python just has examples of opening writing and closing file which I do get to work.
I did get this to work normally but I am having trouble creating my own class and getting it to work.
# Lexicon Class read and write a file
"""This is the Lexicon Class"""
class Lexicon:
def __init__(self):
self._Contains = ' '
self._Add= ' '
self._Remove = ' '
self._ReadLexicon = ' '
self._WriteLexicon(' ')
self.openfile = ' '
def Contains(self,Contains):
filename = raw_input('Enter file name')
source = file(filename)
lexicon = lexlist()
txtword = raw_input('Input what you are searching for in the file')
for line in open("filename"):
if txtword in line:
return True
else:
return False
def Add(self,Add):
#Add word to the file
wordfile = file(source)
lexicon = lexlist()
for item in wordfile:
lexicon.append(item.rstrip('\n'))
def Remove(self,Remove):
#remove word from the file
wordfile = file(source)
lexicon = lexlist()
for item in wordfile:
lexicon.remove(item)
def ReadLexicon(self,ReadLexicon):
source = None
while not source: # still no successfully opened file
filename = raw_input('What is the filename? ')
try:
source = file(filename)
except IOError:
print 'Unable to open file', filename
return source
def WriteLexicon(defaultName):
"""Repeatedly prompt user for filename until successfully opening with write access.
"""
writable = None
while not writable: # still no successfully opened file
prompt = 'What should the output be named [%s]? '% defaultName
filename = raw_input(prompt)
if not filename: # user gave blank response
filename = defaultName # try the suggested default
try:
writable = file(filename, 'w')
except IOError:
print 'Sorry. Unable to write to file', filename
return writable
def OpenFile(self,name):
source = None
while not source: # still no successfully opened file
filename = raw_input('What is the filename? ')
try:
source = file(filename)
except IOError:
print 'Sorry. Unable to open file', filename
return source
##Test the Lexicon program
print'This is a test program for the Lexicon file'
#open the requested file
source = Lexicon.OpenFile('word')
##Lexicon.OpenFile()
##Lexicon.ReadLexicon(source)
print'This is what was in the file',source
#add something to the file
##Lexicon.Add()
#add something and show the file again for testing
##Lexicon.ReadLexicon()
print'Here is the file again with what you added',source