binary key in dictionary

C

cerr

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
 
G

Gary Herron

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:
Traceback (most recent call last):

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
 
J

John Gordon

In said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top