Dictionary assignment

John J. Lee jjl at pobox.com
Sat Aug 16 00:25:23 CEST 2003

"Mark Daley" <mark at diversiform.com> writes:

> I've been using this to save one dictionary as an entry in another
> dictionary.  I was working, but now it seems I've done something to break
> it.  Here's the code in question:
>  def formatsave(self, args = None):
>       if self.formats.get() == '':
>            tkMessageBox.showwarning("No Format", "You must specify a format
> name.")
>       else:
>            for key in current.keys():
>                 format[self.formats.get()][key] = current[key]
>            temp = format.keys()
>            temp.sort()
>            list = tuple(temp)

Yuck.  You've assigned something to a builtin (list).  Strangely, the
object you chose to bind to list is a tuple!

>            gui.formats._list.setlist(list)
> Here's the error I'm getting:
> Traceback (most recent call last):
>       File "C:\PYTHON23\lib\lib-tk\Tkinter.py", line 1345, in __call__
>         return self.func(*args)
>       File "C:\Python23\Layout.py", line 191, in formatsave
>         format[self.formats.get()][key] = current[key]
> KeyError: 'Format 1'
> Any ideas?

Nobody here is likely to solve it for you.  Get aquainted with the
print statement!  There are three indexes there -- which one is the
exception coming from?  Split them up, one index per line, and print
out the results.  You'll soon see the problem.

print format[self.formats.get()]
print format[self.formats.get()][key]
print current[key]

If you're like me, you'll want to make sure all debug print statements
have a label, though, or you'll inevitably end up wondering (not now,
but later) where the hell the output you're seeing on the screen is
coming from.

print "format[self.formats.get()]", format[self.formats.get()]
print "format[self.formats.get()][key]", format[self.formats.get()][key]
print "current[key]", current[key]


More information about the Python-list mailing list