Path: csiph.com!goblin2!goblin.stu.neva.ru!newsfeed1.swip.net!uio.no!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'value,': 0.03; 'subject:Python': 0.05; '"""': 0.05; 'installed.': 0.05; '-*-': 0.07; 'append': 0.07; 'utf-8': 0.07; '-1,': 0.09; 'coding:': 0.09; 'debugger': 0.09; 'ide': 0.09; 'inserted': 0.09; 'key)': 0.09; 'typeerror:': 0.09; 'python': 0.10; '2.7': 0.13; 'index': 0.13; 'def': 0.13; '"""gets': 0.16; '"""given': 0.16; "(it's": 0.16; '(key,': 0.16; 'bucket': 0.16; 'cause.': 0.16; 'creat': 0.16; 'default."""': 0.16; 'iterable': 0.16; 'key):': 0.16; 'range(0,': 0.16; 'received:206.46': 0.16; 'received:206.46.173': 0.16; 'running:': 0.16; 'set)': 0.16; 'subject:Learning': 0.16; 'surprising': 0.16; 'to:name:python list': 0.16; 'value))': 0.16; 'skip:" 30': 0.20; 'not,': 0.22; 'sep': 0.22; 'programming': 0.22; 'replacing': 0.23; 'import': 0.24; '(most': 0.24; 'skip:- 40': 0.25; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'appreciated.': 0.27; 'error': 0.27; 'found.': 0.27; 'function': 0.28; '(although': 0.29; '50,': 0.29; 'away.': 0.29; 'exists,': 0.29; 'index,': 0.29; 'key,': 0.29; 'lot.': 0.29; 'value)': 0.29; 'convert': 0.29; 'there.': 0.30; 'print': 0.30; 'code': 0.30; 'error.': 0.31; 'probably': 0.31; "can't": 0.32; 'statement': 0.32; 'though.': 0.33; 'traceback': 0.33; 'skip:- 10': 0.34; 'file': 0.34; 'execution': 0.35; 'follows:': 0.35; 'mapping': 0.35; 'replace': 0.35; 'returning': 0.35; 'set.': 0.35; 'but': 0.36; 'too': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'really': 0.37; 'seem': 0.37; 'goes': 0.39; 'does': 0.39; 'subject:the': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'some': 0.40; 'header :MIME-version:1': 0.60; 'default': 0.61; 'skip:n 10': 0.62; 'received:206': 0.63; 'note:': 0.66; 'sincerely': 0.67; 'frustrating': 0.84; 'hanging': 0.84; 'skip:/ 30': 0.84; 'received:192.168.1.7': 0.91 X-CMAE-Score: 0 X-CMAE-Analysis: v=2.1 cv=MtGvkDue c=1 sm=1 tr=0 a=AstGAA2XJoPAehtj9MTdvA==:117 a=o1OHuDzbAAAA:8 a=oR5dmqMzAAAA:8 a=IkcTkHD0fZMA:10 a=ff-B7xzCdYMA:10 a=8Fpe7JOIn5WgEEyJHQIA:9 a=1tbd7E57IgntbskO:21 a=WxGOB8dmfnR0M0Pb:21 a=QEXdDO2ut3YA:10 Date: Wed, 09 Sep 2015 10:27:03 -0700 From: Gary Roach User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.8.0 MIME-version: 1.0 To: Python List Subject: Lesson 39 of Learning Python the Hard Way hangs Content-Type: text/plain; charset=utf-8; format=flowed Content-transfer-encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 121 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1441823265 news.xs4all.nl 23810 [2001:888:2000:d::a6]:49479 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:96205 Hi all I am new to python but not programming (Although rusty) and am using Learning Python The Hard Way. I really like it. System: --------------- Debian 8 (jessie) KDE Desktop Python 2.7 (It's going to be a while before 2,7 goes away. There is just too much code out there. Ninja-IDE IDE - I like this a lot. I can't seem to find out how to put breaks into the code though. The debugger plugin is installed. -------------- For some reason lesson 39 is hanging and I can't find the cause. A print statement inserted into the get_slot function just after bucket shows that get_bucket is returning an []. It' not surprising that enumerate() doesn't like this very much. I just can't find the cause. The error message is as follows: ------------------------------------------- Running: /root/mystuff/mystuff/ex39_test.py (Wed Sep 9 09:57:51 2015) Traceback (most recent call last): File "/root/mystuff/mystuff/ex39_test.py", line 6, in hashmap.set(states, 'Oregon', 'OR') File "/root/mystuff/mystuff/hashmap.py", line 50, in set i, k, v = get_slot(aMap, key) TypeError: 'NoneType' object is not iterable Execution Successful! NOTE: Line 50 is the - i, k, v = get_slot(aMap, key)- of def set. -------------------------------------------- The calling code is: -------------------------------------------- # -*- coding: utf-8 -*- import hashmap # creat a mapping of stat to abbreviation states = hashmap.new() hashmap.set(states, 'Oregon', 'OR') -------------------------------------------- The hashmap code is as follows: -------------------------------------------- def new(num_buckets=256): """Initializes a Map with the given number of buckets.""" aMap = [] for i in range(0, num_buckets): aMap.append([]) return aMap def hash_key(aMap, key): """Given a key this will create a number and then convert it to an index for the aMap's buckets.""" return hash(key) % len(aMap) def get_bucket(aMap, key): """Given a key, find the bucket where it would go.""" bucket_id = hash_key(aMap, key) return aMap[bucket_id] def get_slot(aMap, key, default=None): """ Returns the index, key, and value of a slot found in a bucket. Returns -1, key, and default (None if not set) when not found. """ bucket = get_bucket(aMap, key) for i, kv in enumerate(bucket): k, v = kv if key == k: return i, k, v return -1, key, default def get(aMap, key, default=None): """Gets the value in a bucket for the given key, or the default.""" i, k, v = get_slot(aMap, key, default=default) return v def set(aMap, key, value): """Sets the key to the value, replacing any existing value.""" bucket = get_bucket(aMap, key) i, k, v = get_slot(aMap, key) if i >= 0: # the key exists, replace it bucket[i] = (key, value) else: # the key does not, append to create it bucket.append((key, value)) def delete(aMap, key): """Deletes the given key from the Map.""" bucket = get_bucket(aMap, key) for i in xrange(len(bucket)): k, v = bucket[i] if key == k: del bucket[i] break def list(aMap): """Prints out what's in the Map.""" for bucket in aMap: if bucket: for k, v in bucket: print k, v Very frustrating and probably a stupid error. Any help will be sincerely appreciated. Gary R.