Search code examples
python-2.7wxpython

Trouble resetting / backtracking with variables


I've written a Cryptoquote Generator in Python using wxPython. I don't really have any problems with wxPython itself, but rather with resetting my variables. My program works this way: I have a button that generates an encrypted quote. Then, the user has a decode button they press to change one letter at a time. There is a button to reset the whole quote and a button to reset changes one step back at a time.

I have a base_copy variable to store the original encrypted quote. It is an empty list that is populated with the individual characters of the encrypted quote one at a time when on_generate_quote is called. It remains unchanged throughout the loop -- so that I may call on it with on_clear_all or on_clear_last to reset my encrypted quote. The problem is, if I use my decode_button to decode a letter, then use my clear_all_button, then decode_button again, my clear_all_button calls on a base_copy that has now been somehow tainted with changed letters that should only be in my split_cryptoquote copy. Why is this, since I never make an implicit call to alter base_copy? (I do, however, make a call in on_clear_all to set split_cryptoquote to base_copy, but this shouldn't change base_copy.)

#/------------------ wxPython GUI -----------------\

class MainWindow(wx.Frame):

    quote = []
    quote.append(quote_fetch(quotes))
    split_cryptoquote = []
    base_copy = []
    split_buffer = []
    buffer_origin = None
    buffer_replace = None
    quote_altered = False #Becomes true after first decoding change.

    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title="Cryptogrammar", size=(1000, 200))
        self.CreateStatusBar()

        self.txt = wx.StaticText(self, -1, "".join(MainWindow.split_cryptoquote), (20,30), (40,40))
        self.txt.SetForegroundColour("WHITE")

        #Menu
        filemenu = wx.Menu()
        menu_about = filemenu.Append(wx.ID_ABOUT, "&About", " Information about this program")
        menu_how = filemenu.Append(HOW_TO, "&How to Play", " How to play Cryptogrammar")
        menu_exit = filemenu.Append(wx.ID_EXIT,"E&xit", " Close Cryptogrammar")
        #menuGenerate = filemenu.Append(wx.ID_NONE, "&Generate New", "Generate a new cryptogram")

        #menu_bar
        menu_bar = wx.MenuBar()
        menu_bar.Append(filemenu, "&File")
        self.SetMenuBar(menu_bar)


        #Buttons
        generate_button = wx.Button(self, -1, "&Generate Cryptogram")
        decode_button = wx.Button(self, -1, "&Decode Letter")
        clear_all_button = wx.Button(self, -1, "&Clear All Changes")
        clear_last_button = wx.Button(self, -1, "Clear &Last Change")
        answer_button = wx.Button(self, -1, "Show &Answer")
        but_list = [generate_button, decode_button, clear_all_button, clear_last_button, answer_button]

        #Sizers
        self.sizer2 = wx.BoxSizer(wx.HORIZONTAL)
        for i in range(0, 5):
            self.sizer2.Add(but_list[i], 1, wx.EXPAND)

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.txt, 1, wx.EXPAND)
        self.sizer.Add(self.sizer2, 0, wx.EXPAND)

        #Events
        self.Bind(wx.EVT_MENU, self.on_about, menu_about)
        self.Bind(wx.EVT_MENU, self.on_exit, menu_exit)
        self.Bind(wx.EVT_MENU, self.on_how, menu_how)
        self.Bind(wx.EVT_BUTTON, self.on_generate_quote, generate_button)
        self.Bind(wx.EVT_BUTTON, self.on_decode, decode_button)
        self.Bind(wx.EVT_BUTTON, self.on_answer, answer_button)
        self.Bind(wx.EVT_BUTTON, self.on_clear_all, clear_all_button)
        self.Bind(wx.EVT_BUTTON, self.on_clear_last, clear_last_button)


        self.SetSizer(self.sizer)
        self.SetAutoLayout(1)
        self.sizer.Fit(self)
        self.SetTitle("Cryptogrammar")
        self.Centre()

    def on_about(self, e):
        dialogue = wx.MessageDialog(self, "A program for generating random cryptograms.\n\n\n\nCopyright 2014 Joshua Simmons\nVersion 0.1.0", "About Cryptogrammar", wx.OK)
        dialogue.ShowModal()
        dialogue.Destroy()

    def on_exit(self, e):
        self.Close(True)

    def on_how(self, e):
        dialogue = wx.MessageDialog(self, "HOW TO PLAY:\n\n\n--\tPress the 'Generate Cryptogram' to spawn a cryptogram.\n\n--\tUse the 'Decode Letter' to replace an encrypted letter with a letter of your choice. 'Decoded' letters will be lowercase to distinguish them.\n\n--\tUse the 'Clear Changes' button to reset the puzzle.\n\n--\t'Show Answer' solves the puzzle!", "How to play Cryptogrammar", wx.OK)
        dialogue.ShowModal()
        dialogue.Destroy()

    def on_decode(self, e):
        dialogue = wx.TextEntryDialog(self, "Which letter do you wish to change? Use format: 'a=e'", "Decode Letter", "")
        dialogue.ShowModal()
        decode = dialogue.GetValue()
        #Text entry filter
        match = re.search(r'\w+=\w+|^\d*$', decode)
        if not match:
            err = wx.MessageDialog(self, "That is not a correct entry format.", "Entry Error", style=wx.ICON_HAND)
            err.ShowModal()
            #Letter replacement
        else:
            if not MainWindow.quote_altered:
                MainWindow.buffer_origin = decode[0].upper()
                MainWindow.buffer_replace = decode[2].upper()
            else:
                for n in range(0, len(MainWindow.split_buffer)):
                    if MainWindow.split_buffer[n] == MainWindow.buffer_origin:
                        MainWindow.split_buffer[n] = MainWindow.buffer_replace.lower()
                MainWindow.buffer_origin = decode[0].upper()
                MainWindow.buffer_replace = decode[2].upper()
            origin = decode[0].upper()
            replace = decode[2].upper() #For resetting changes one at a time.
            for n in range(0, len(MainWindow.split_cryptoquote)):
                if MainWindow.split_cryptoquote[n] == origin:
                    MainWindow.split_cryptoquote[n] = replace.lower()
            MainWindow.quote_altered = True
            origin = None
            replace = None
            self.txt.SetLabel("".join(MainWindow.split_cryptoquote))
            self.sizer.Layout()


        dialogue.Destroy()

    def on_generate_quote(self, e):

        MainWindow.quote.pop()
        MainWindow.quote.append(quote_fetch(quotes))
        cryptoquote = generate_cryptogram(MainWindow.quote[0], encrypt_key(shuffle_alphabet()))
        MainWindow.split_cryptoquote = []
        MainWindow.base_copy = []
        for i in cryptoquote:
            MainWindow.split_cryptoquote.append(i)
            MainWindow.base_copy.append(i)
            MainWindow.split_buffer.append(i)
        self.txt.SetLabel("".join(MainWindow.split_cryptoquote))
        self.txt.SetForegroundColour("BLACK")
        self.sizer.Layout()

    def on_answer(self, e):
        if len(MainWindow.base_copy) == 0:
            err = wx.MessageDialog(self, "You haven't generated a puzzle yet, doofus!", "Encryption Error", style=wx.ICON_HAND)
            err.ShowModal()
        else:
            self.txt.SetLabel(MainWindow.quote[0])
            self.txt.SetForegroundColour("BLUE")
            self.sizer.Layout()

    def on_clear_last(self, e):
        if MainWindow.quote_altered:
            self.txt.SetLabel("".join(MainWindow.split_buffer))
        else:
            self.txt.SetLabel("".join(MainWindow.base_copy))
        self.txt.SetForegroundColour("BLACK")
        self.sizer.Layout()


    def on_clear_all(self, e):
        print MainWindow.base_copy
        MainWindow.split_cryptoquote = MainWindow.base_copy
        MainWindow.split_buffer = MainWindow.base_copy
        MainWindow.quote_altered = False
        self.txt.SetLabel("".join(MainWindow.base_copy))
        self.txt.SetForegroundColour("BLACK")
        self.sizer.Layout()


app = wx.App(False)
frame = MainWindow(None, "Cryptogrammar")
frame.Show()
app.MainLoop()

Solution

  • (I do, however, make a call in on_clear_all to set split_cryptoquote to base_copy, but this shouldn't change base_copy.)

    You've spotted your own problem:

    MainWindow.split_cryptoquote = MainWindow.base_copy binds MainWindow.split_cryptoquote to the same object as MainWindow.base_copy, so when you modify one, you modify the other.

    If you change the line to

    MainWindow.split_cryptoquote = MainWindow.base_copy[:]

    You will force python to create a new object (a copy of MainWindow.base_copy) , and this problem should not occur.

    Edit: The line below: MainWindow.split_buffer = MainWindow.base_copy also needs the same treatment, I think.

    See this example:

    >>> lista = [1,2]
    >>> listb = lista
    >>> listb.append(3)
    >>> lista
    [1, 2, 3]
    >>> listb
    [1, 2, 3]
    
    >>> listc = lista[:]
    >>> listc.append(4)
    >>> listc
    [1, 2, 3, 4]
    >>> lista
    [1, 2, 3]