Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Clueless with cPickle

Reply
Thread Tools

Clueless with cPickle

 
 
les
Guest
Posts: n/a
 
      05-08-2005
I am working on a homework assignment and trying to use cPickle to store
the answers from questor.py I believe I have the syntax correct but am not
sure if I am placing everything where it needs to be. Any help would be
greatly appreciated. When I attempt to run what I have I end up with the
following:

Traceback (most recent call last):
File "/home/les/workspace/Module 2/questor.py", line 18, in ?
f = file(questorlistfile)
NameError: name 'questorlistfile' is not defined

I thought that I had defined questorlistfile on the 4th line below

# define some constants for future use

import cPickle as p
#import pickle as p

questorfile = 'questor.data' # the name of the file where we will store the object

questorlist = []

# Write to the file
f = file(questorfile, 'w')
p.dump(questorlist, f) # dump the object to a file
f.close()

del questorlist # remove the shoplist

# Read back from the storage
f = file(questorlistfile)
storedlist = p.load(f)
print storedlist

kQuestion = 'question'
kGuess = 'guess'

# define a function for asking yes/no questions
def yesno(prompt):
ans = raw_input(prompt)
return (ans[0]=='y' or ans[0]=='Y')

# define a node in the question tree (either question or guess)
class Qnode:

# initialization method
def __init__(self,guess):
self.nodetype = kGuess
self.desc = guess

# get the question to ask
def query(self):
if (self.nodetype == kQuestion):
return self.desc + " "
elif (self.nodetype == kGuess):
return "Is it a " + self.desc + "? "
else:
return "Error: invalid node type!"

# return new node, given a boolean response
def nextnode(self,answer):
return self.nodes[answer]

# turn a guess node into a question node and add new item
# give a question, the new item, and the answer for that item
def makeQuest( self, question, newitem, newanswer ):

# create new nodes for the new answer and old answer
newAnsNode = Qnode(newitem)
oldAnsNode = Qnode(self.desc)

# turn this node into a question node
self.nodetype = kQuestion
self.desc = question

# assign the yes and no nodes appropriately
self.nodes = {newanswer:newAnsNode, not newanswerldAnsNode}



def traverse(fromNode):
# ask the question
yes = yesno( fromNode.query() )

# if this is a guess node, then did we get it right?
if (fromNode.nodetype == kGuess):
if (yes):
print "I'm a genius!!!"
return
# if we didn't get it right, return the node
return fromNode

# if it's a question node, then ask another question
return traverse( fromNode.nextnode(yes) )

def run():
# start with a single guess node
topNode = Qnode('python')

done = 0
while not done:
# ask questions till we get to the end
result = traverse( topNode )

# if result is a node, we need to add a question
if (result):
item = raw_input("OK, what were you thinking of? ")
print "Enter a question that distinguishes a",
print item, "from a", result.desc + ":"
q = raw_input()
ans = yesno("What is the answer for " + item + "? ")
result.makeQuest( q, item, ans )
print "Got it."

# repeat until done
print
done = not yesno("Do another? ")
print


# immediate-mode commands, for drag-and-drop or execfile() execution
if __name__ == '__main__':
run()
print
raw_input("press Return>")
else:
print "Module questor imported."
print "To run, type: questor.run()"
print "To reload after changes to the source, type: reload(questor)"

# end of questor.py
 
Reply With Quote
 
 
 
 
Roel Schroeven
Guest
Posts: n/a
 
      05-08-2005
les wrote:

> Traceback (most recent call last):
> File "/home/les/workspace/Module 2/questor.py", line 18, in ?
> f = file(questorlistfile)
> NameError: name 'questorlistfile' is not defined
>
> I thought that I had defined questorlistfile on the 4th line below
>
> # define some constants for future use
>
> import cPickle as p
> #import pickle as p
>
> questorfile = 'questor.data' # the name of the file where we will store the object


You defined questorfile, not questorlistfile.

--
If I have been able to see further, it was only because I stood
on the shoulders of giants. -- Isaac Newton

Roel Schroeven
 
Reply With Quote
 
 
 
 
les
Guest
Posts: n/a
 
      05-08-2005
OK, looks like it is time for a break!

Thanks for the replies!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Clueless on home networking through ethernet jacks =?Utf-8?B?Sm9obiBF?= Wireless Networking 2 12-22-2005 11:32 AM
Re: Clueless with cPickle Jp Calderone Python 0 05-08-2005 09:33 PM
Clueless, have patience... Amanda Wireless Networking 1 08-07-2004 03:14 PM
Still slightly clueless about this GUI thing...(JProgressBar never draws on a JDialog) Inertia_sublimation Java 5 01-03-2004 12:05 AM
clueless student trying to parse XML sal achhala Java 7 10-20-2003 05:07 PM



Advertisments