473,320 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Cpickle/Data file need program to retain information

I have a program and I want the program to be able to save question/answers so the program actually gets smarter without getting amnesia everytime its started. Can anyone help???

Expand|Select|Wrap|Line Numbers
  1. # questor.py
  2.  
  3.  # define some constants for futureuse
  4.  
  5. kQuestion = 'question'
  6. kGuess = 'guess'
  7.  
  8. # define a function for asking yes/no questions
  9. def yesno(prompt): 
  10.           ans = raw_input(prompt) 
  11.           return (ans[0]=='y' or ans[0]=='Y')
  12.  
  13. # define a node in the question tree (either question or guess)
  14. class Qnode: 
  15.  
  16.           # initialization method 
  17.           def __init__(self,guess): 
  18.                      self.nodetype = kGuess 
  19.                      self.desc = guess 
  20.           # get the question to ask 
  21.           def query(self): 
  22.                      if (self.nodetype == kQuestion): 
  23.                                return self.desc + " " 
  24.                      elif (self.nodetype == kGuess): 
  25.                         return "Is it a " + self.desc + "? " 
  26.                      else: 
  27.                         return "Error: invalid node type!" 
  28.  
  29.           # return new node, given a boolean response 
  30.           def nextnode(self,answer): 
  31.                      return self.nodes[answer] 
  32.  
  33.           # turn a guess node into a question node and add new item 
  34.        # give a question, the new item, and the answer for that item   
  35.           def makeQuest( self, question, newitem, newanswer ): 
  36.  
  37.        # create new nodes for the new answer and old answer newAnsNode = Qnode(newitem) 
  38. oldAnsNode = Qnode(self.desc) 
  39.  
  40. # turn this node into a question node 
  41. self.nodetype = kQuestion 
  42. self.desc = question 
  43.  
  44. # assign the yes and no nodes appropriately 
  45. self.nodes = {newanswer:newAnsNode, not newanswerdAnsNode} 
  46.  
  47. def traverse(fromNode): 
  48.            # ask the question 
  49.            yes = yesno( fromNode.query() ) 
  50.  
  51.            # if this is a guess node, then did we get it right? 
  52.            if (fromNode.nodetype == kGuess):
  53.                       if (yes): 
  54.                          print "I'm a genius!!!" 
  55.                          return 
  56.            # if we didn't get it right, return the node 
  57.            return fromNode 
  58.  
  59.            # if it's a question node, then ask another question 
  60.            return traverse( fromNode.nextnode(yes) )
  61.  
  62. def run(): 
  63.            # start with a single guess node 
  64.            topNode = Qnode('python') 
  65.  
  66.            done = 0 
  67.            while not done: 
  68.                       # ask questions till we get to the end 
  69.                       result = traverse( topNode ) 
  70.  
  71.                       # if result is a node, we need to add a question 
  72.                       if (result):
  73.                  item = raw_input("OK, what were you thinking of? ") 
  74.                  print "Enter a question that distinguishes a", 
  75.                  print item, "from a", result.desc + ":" 
  76.                  q = raw_input() 
  77.                 ans = yesno("What is the answer for " + item + "?")          
  78.                 result.makeQuest( q, item, ans ) 
  79.                 print "Got it." 
  80.  
  81.                        # repeat until done
  82.                        print 
  83.                        done = not yesno("Do another? ") 
  84.                        print
  85.  
  86.  
  87.  
  88. #  immediate-mode commands, for drag-and-drop or execfile() execution
  89. if __name__ == '__main__': 
  90.           run() 
  91.           print 
  92.           raw_input("press Return>")
  93. else: 
  94.  
  95.           print "Module questor imported." 
  96.           print "To run, type: questor.run()" 
  97.           print "To reload after changes to the source, type: reload(questor)"
  98. # end of questor.py
Aug 10 '06 #1
0 1728

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Guenter Walser | last post by:
Hello, When using the codeline: pickle.dump(self, file, pickle.HIGHEST_PROTOCOL) my program runs perfectly fine: --------------------------- Testprotocol -----------------------------
0
by: Simon Dahlbacka | last post by:
I'm trying to cache some information using cPickle and I thought that using cPickle.HIGHEST_PROTOCOL would give me an efficient representation. However, when trying to load the information...
2
by: sh | last post by:
Hi guys, Well, I have a (maybe dumb) question. I want to write my own little blog using Python (as a fairly small but doable project for myself to learn more deaply Python in a web context). ...
1
by: A.B., Khalid | last post by:
I wonder if someone can explain what is wrong here. I am pickling a list of dictionaries (see code attached) and unpickling it back using the HIGHEST_PROTOCOL of pickle and cPickle. I am getting an...
5
by: Marcus Lowland | last post by:
Hello, I'm fairly new to python and have read about and wanted to begin experimenting with cpickle. As I understand, this should be a native module in the python library. I have python 2.3 and now...
4
by: Mingus Tsai | last post by:
Hello- please help with unpickling problem: I am using Python version 2.3.4 with IDLE version 1.0.3 on a Windows XPhome system. My problem is with using cPickle to deserialize my pickled...
8
by: Jeff Poole | last post by:
This is going to be a pretty vague message because it involves a large block of code I'd rather avoid posting. Basically, I've been pickling a dictionary of instances of a class I've created...
0
by: Roopesh | last post by:
I am trying to write xml files which are inside a zip file into the database. In the zipfile module the function read returns bytes. What I did was to make a blob out of the returned bytes and...
8
by: Victor Kryukov | last post by:
Hello list, I've found the following strange behavior of cPickle. Do you think it's a bug, or is it by design? Best regards, Victor. from pickle import dumps from cPickle import dumps as...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.