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 <module>
    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.
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to