Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'much!': 0.05; 'binary': 0.07; 'problem:': 0.07; 'tries': 0.07; 'ascii': 0.09; 'here?': 0.09; 'lines:': 0.09; 'main()': 0.09; 'received:67.192': 0.09; 'received:67.192.241': 0.09; 'received:dfw.emailsrvr.com': 0.09; 'ron': 0.09; 'python': 0.11; 'creates': 0.14; 'collections': 0.16; 'defaultdict': 0.16; 'dictionary,': 0.16; 'illustrates': 0.16; 'subject:key': 0.16; 'wrote:': 0.18; 'code.': 0.18; '>>>': 0.22; 'example': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'received:emailsrvr.com': 0.24; 'simpler': 0.24; 'fine': 0.24; 'received:(smtp server)': 0.26; 'code:': 0.26; 'header:In-Reply- To:1': 0.27; '"",': 0.31; '>>>>': 0.31; 'gary': 0.31; 'keyerror:': 0.31; 'anyone': 0.31; 'file': 0.32; '(most': 0.33; 'mac': 0.33; 'problem': 0.35; "can't": 0.35; 'something': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'really': 0.36; 'indexed': 0.36; 'module.': 0.36; 'hi,': 0.36; 'wrong': 0.37; 'application': 0.37; 'being': 0.38; 'thank': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'skip:u 10': 0.60; 'is.': 0.60; 'further': 0.61; 'address': 0.63; 'confusing': 0.84; 'd[1]': 0.84; 'd[2]': 0.84; 'examine': 0.93 X-Virus-Scanned: OK Date: Tue, 30 Jul 2013 13:44:47 -0700 From: Gary Herron User-Agent: Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20130623 Thunderbird/17.0.7 MIME-Version: 1.0 To: python-list@python.org Subject: Re: binary key in dictionary References: <9004a556-958f-4d1d-81a7-4d1b731348c5@googlegroups.com> In-Reply-To: <9004a556-958f-4d1d-81a7-4d1b731348c5@googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 63 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1375217447 news.xs4all.nl 15876 [2001:888:2000:d::a6]:42797 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:51605 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 > 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 "", line 1, in 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