answers.py v0.0.1 - source

Pyenos pyenos at pyenos.org
Thu Dec 28 06:04:49 EST 2006


#!/usr/bin/python

####################################################
# answers.py --- A simple answer bot
# Copyright (C) 2006 Logan Lee
#
# MESSAGE:
# 
# This program is a simple text-console program
#+that asks for a problem and its solution then 
#+persists the problem-solution dictionary to a 
#+pickled file.
#
# Feel free to do whatever you like with the code.
# If you have any comments please reply to the
#+usenet article which this source was published.
#
# I am particularly interested in your comments on
#+correct usage of programming design patterns,
#+better coding practise, or ideas about features.
# 
#####################################################

global ANSWER_PATH

###############################################
# IMPORTANT: Set ANSWER_PATH to your preference 
###############################################
ANSWER_PATH=""
###############################################

global VERSION
VERSION=0.01

class Problem:
    def __init__(self):self.problem=""
    def set(self,problem):self.problem=problem
    def get(self):return self.problem

class Solution:
    def __init__(self):self.solution=""
    def set(self,solution):self.solution=solution
    def get(self):return self.solution

class Storage:
    def add(self,problem,solution):
        self.answers[problem]=solution
    def save(self):
        import pickle
        try: pickle.dump(self.answers,file(ANSWER_PATH+".answers","w"));print "Answers saved"
        except: print "Couldn't save answers. Something went wrong"

    def load(self):
        import pickle
        try: self.answers=pickle.load(file(ANSWER_PATH+".answers"));print "Answers loaded"
        except: print "Couldn't load answers. Something went wrong or this may be your first time running"
        
    def getAnswer(self,problem):
        try: answer=self.answers[problem]
        except: print "I don't have the answer to that problem"
        return answer
    
    def __init__(self,answers={}):
        self.answers=answers

class Console:
    class Menu:
        global storage
        storage=Storage()
        
        class Level0:
            def do(self):
                import sys
                action=sys.exit()
            
        class Level1:

            def do(self):
                problem=Problem();solution=Solution()
                text=("Do you have a problem? $ ","Do you have the solution? $ ","no idea","Try again with your common sense")
                commands=("answer","load(disabled, already loaded)","quit","help")
                problem.set(raw_input(text[0]))

                if problem.get()==commands[0]:return 4
                #elif problem.get()==commands[1]:storage.load();return 1
                elif problem.get()==commands[2]:return 0
                elif problem.get()==commands[3]:print commands;return 1
                
                solution.set(raw_input(text[1]))

                if solution.get()==text[2]:print text[3];solution.set(raw_input(text[1]))

                if raw_input("Your solution to '%s' is '%s', correct? $ " % (problem.get(),solution.get()))=="yes":storage.add(problem.get(),solution.get());print "Answer confirmed";return 2
                else: return 1
                
        class Level2:
            def do(self):
                if raw_input("Should I save the answer? $ ")=="yes":
                    storage.save()
                    return 3
                else: return 1

        class Level3:
            def do(self):
                text=("Do you have another problem?"+" $ ","Do you want to quit?"+" $ ")
                if raw_input(text[0])=="yes":
                    return 1
                elif raw_input(text[1])=="yes":return 0
                else: return 3

        class Level4:
            def do(self):
                text=("What answer do you seek? $ ","Do you want to ask again? $ ")
                problem=raw_input(text[0]);answer=storage.getAnswer(problem)
                print "Answer to '%s' is '%s'" % (problem,answer)
                if raw_input(text[1])=="yes":return 4
                else: return 1
                
        def start(self):
	    storage.load()
            level=1
            while 1:
                if level==0:
                    Console.Menu.Level0().do()
                if level==1:
                    level=Console.Menu.Level1().do()
                if level==2:
                    level=Console.Menu.Level2().do()
                if level==3:
                    level=Console.Menu.Level3().do()
                if level==4:
                    level=Console.Menu.Level4().do()
        
    def start(self):Console.Menu().start()
    
if __name__=="__main__":
    Console().start()



More information about the Python-list mailing list