Dictionary assignment

Mark Daley mark at diversiform.com
Fri Aug 15 18:58:46 EDT 2003


I'm becoming aware that my coding techniques leave much to be desired.
That's ok, because I'm really just a hack with no education whatsoever.  At
any rate, I realize I didn't supply enough information.  Here is the core of
my dilemma:

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

Now, all disgust from my example aside, shouldn't this line cause a new key
(whatever self.formats get() produces) whose contents are an exact copy of
current?

BTW, I do this to avoid those location references.  I need to get an actual
copy and this is the only way I can (currently) guarantee it.  Believe me,
I'm only treading water here!

- Mark

-----Original Message-----
From: python-list-admin at python.org
[mailto:python-list-admin at python.org]On Behalf Of John J. Lee
Sent: Friday, August 15, 2003 3:25 PM
To: python-list at python.org
Subject: Re: Dictionary assignment


"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]


John
--
http://mail.python.org/mailman/listinfo/python-list







More information about the Python-list mailing list