Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #51604 > unrolled thread

binary key in dictionary

Started bycerr <ron.eggler@gmail.com>
First post2013-07-30 13:29 -0700
Last post2013-07-31 14:26 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  binary key in dictionary cerr <ron.eggler@gmail.com> - 2013-07-30 13:29 -0700
    Re: binary key in dictionary Gary Herron <gary.herron@islandtraining.com> - 2013-07-30 13:44 -0700
    Re: binary key in dictionary John Gordon <gordon@panix.com> - 2013-07-31 14:26 +0000

#51604 — binary key in dictionary

Fromcerr <ron.eggler@gmail.com>
Date2013-07-30 13:29 -0700
Subjectbinary key in dictionary
Message-ID<9004a556-958f-4d1d-81a7-4d1b731348c5@googlegroups.com>
Hi,

In my application I have followingf lines:
                print curr_mac
                print hexlify(buf)
                binmac = unhexlify(curr_mac)
                tmpgndict[binmac] += buf
curr_mac being a 3Byte MAVC address in ASCII and I want to populate a dictionary where the value(buf) is indexed by binary mac.

I get this in my code:

Traceback (most recent call last):
  File "gateway.py", line 2485, in <module>
    main()
  File "gateway.py", line 2459, in main
    cloud_check()
  File "gateway.py", line 770, in cloud_check
    gnstr_dict[src] = gn_from_cloud(curr_mac)
  File "gateway.py", line 2103, in gn_from_cloud
    tmpgndict[binmac] += "HELLO"
KeyError: '\x04\xeeu'

but then again, the following works fine in the python interpreter:
>>> mac = '04ee75'
>>> dat = '2a0001016d03c400040001000a'
>>> mydict = {}
>>> mydict[unhexlify(mac)]=dat
>>> print mydict
{'\x04\xeeu': '2a0001016d03c400040001000a'}

I really seem to do something wrong and can't see what it is. Can anyone help me further here?

Thank you very much!
Ron

[toc] | [next] | [standalone]


#51605

FromGary Herron <gary.herron@islandtraining.com>
Date2013-07-30 13:44 -0700
Message-ID<mailman.5340.1375217447.3114.python-list@python.org>
In reply to#51604
On 07/30/2013 01:29 PM, cerr wrote:
> Hi,
>
> In my application I have followingf lines:
>                  print curr_mac
>                  print hexlify(buf)
>                  binmac = unhexlify(curr_mac)
>                  tmpgndict[binmac] += buf
> curr_mac being a 3Byte MAVC address in ASCII and I want to populate a dictionary where the value(buf) is indexed by binary mac.
>
> I get this in my code:
>
> Traceback (most recent call last):
>    File "gateway.py", line 2485, in <module>
>      main()
>    File "gateway.py", line 2459, in main
>      cloud_check()
>    File "gateway.py", line 770, in cloud_check
>      gnstr_dict[src] = gn_from_cloud(curr_mac)
>    File "gateway.py", line 2103, in gn_from_cloud
>      tmpgndict[binmac] += "HELLO"
> KeyError: '\x04\xeeu'
>
> but then again, the following works fine in the python interpreter:
>>>> mac = '04ee75'
>>>> dat = '2a0001016d03c400040001000a'
>>>> mydict = {}
>>>> mydict[unhexlify(mac)]=dat
>>>> print mydict
> {'\x04\xeeu': '2a0001016d03c400040001000a'}
>
> I really seem to do something wrong and can't see what it is. Can anyone help me further here?
>
> Thank you very much!
> Ron


You are confusing the problem with excess code.  Examine the following 
simpler example which illustrates the problem:
 >>> d = {}
 >>> d[1] = 99
 >>> d[2] += 98
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
KeyError: 2
 >>>

The  line
   d[1] = 99
creates a key-value pair in the dictionary, but the line
   d[2] += 98
tries to add 98 to an already existing value at d[2],   But there is no 
value at d[2] until you set it:
   d[2] = 0 # for instance

You may want to look at defaultdict from the collections module.

Gary Herron




[toc] | [prev] | [next] | [standalone]


#51661

FromJohn Gordon <gordon@panix.com>
Date2013-07-31 14:26 +0000
Message-ID<ktb6qj$b4g$1@reader1.panix.com>
In reply to#51604
In <9004a556-958f-4d1d-81a7-4d1b731348c5@googlegroups.com> cerr <ron.eggler@gmail.com> writes:

> Traceback (most recent call last):
>   File "gateway.py", line 2485, in <module>
>     main()
>   File "gateway.py", line 2459, in main
>     cloud_check()
>   File "gateway.py", line 770, in cloud_check
>     gnstr_dict[src] = gn_from_cloud(curr_mac)
>   File "gateway.py", line 2103, in gn_from_cloud
>     tmpgndict[binmac] += "HELLO"
> KeyError: '\x04\xeeu'

You're not assigning to tmpgndict[binmac]; you're appending to it.  This
requires that tmpgndict[binmac] already exists, which it does not.

Make sure that tmpgndict[binmac] exists before you try appending to it.

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web