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


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

Lesson 39 of Learning Python the Hard Way hangs

Started byGary Roach <gary719_list1@verizon.net>
First post2015-09-09 10:27 -0700
Last post2015-09-09 14:53 -0700
Articles 7 — 5 participants

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


Contents

  Lesson 39 of Learning Python the Hard Way hangs Gary Roach <gary719_list1@verizon.net> - 2015-09-09 10:27 -0700
    Re: Lesson 39 of Learning Python the Hard Way hangs John Gordon <gordon@panix.com> - 2015-09-09 20:45 +0000
      Re: Lesson 39 of Learning Python the Hard Way hangs Denis McMahon <denismfmcmahon@gmail.com> - 2015-09-09 21:36 +0000
      Re: Lesson 39 of Learning Python the Hard Way hangs (Fixed?) Gary Roach <gary719_list1@verizon.net> - 2015-09-09 16:58 -0700
      Re: Lesson 39 of Learning Python the Hard Way hangs (Fixed?) Gary Roach <garyroach@verizon.net> - 2015-09-09 16:58 -0700
        Re: Lesson 39 of Learning Python the Hard Way hangs (Fixed?) YBM <ybmess@nooos.fr.invalid> - 2015-09-10 03:42 -0300
      Re: Lesson 39 of Learning Python the Hard Way hangs Gary Roach <garyroach@verizon.net> - 2015-09-09 14:53 -0700

#96205 — Lesson 39 of Learning Python the Hard Way hangs

FromGary Roach <gary719_list1@verizon.net>
Date2015-09-09 10:27 -0700
SubjectLesson 39 of Learning Python the Hard Way hangs
Message-ID<mailman.280.1441823265.8327.python-list@python.org>
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.

[toc] | [next] | [standalone]


#96225

FromJohn Gordon <gordon@panix.com>
Date2015-09-09 20:45 +0000
Message-ID<msq5q5$jg3$1@reader1.panix.com>
In reply to#96205
In <mailman.280.1441823265.8327.python-list@python.org> Gary Roach <gary719_list1@verizon.net> writes:

> 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!

Where does the message "Execution Successful!" come from?  It seems like
you have other code that you haven't shown us.

In any case, I saved your code and ran it, and did not get an error.

-- 
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] | [next] | [standalone]


#96229

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2015-09-09 21:36 +0000
Message-ID<msq8p7$l6g$3@dont-email.me>
In reply to#96225
On Wed, 09 Sep 2015 20:45:57 +0000, John Gordon wrote:

> In any case, I saved your code and ran it, and did not get an error.

+1

I think "Execution Succesful!" might be coming from his IDE?

-- 
Denis McMahon, denismfmcmahon@gmail.com

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


#96233 — Re: Lesson 39 of Learning Python the Hard Way hangs (Fixed?)

FromGary Roach <gary719_list1@verizon.net>
Date2015-09-09 16:58 -0700
SubjectRe: Lesson 39 of Learning Python the Hard Way hangs (Fixed?)
Message-ID<mailman.299.1441843121.8327.python-list@python.org>
In reply to#96225
On 09/09/2015 01:45 PM, John Gordon wrote:
> In <mailman.280.1441823265.8327.python-list@python.org> Gary Roach <gary719_list1@verizon.net> writes:
>
>> 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!
> Where does the message "Execution Successful!" come from?  It seems like
> you have other code that you haven't shown us.
>
> In any case, I saved your code and ran it, and did not get an error.
>
No other code. Ninja-IDE tacked the "Execution Successful" at the end if 
the error message. Dont know why.

Interesting that you ran the code successfully. I have tried to run the 
code in both Ninja and at the command line and got the  exact same error 
message.

A note: If you could, please post your replies to the list. If you 
don't, it either breaks the thread or ends up sending a copy to my 
personal email address. I am assuming that your email client gives you 
that choice. If not, I'm not sure how you would assure that the proper 
way happens. I use the icedove (thunderbird) mail client and the choice 
is a drop down list at the top of the mail editor page.

Thanks for your reply.
Still confused

Gary R.

PS I copied over my code with the one from the lesson and all of a 
sudden it works. I then used the code I copied to the email. It also 
worked. Now I can't duplicate the problem. The problems fixed but ....

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


#96237 — Re: Lesson 39 of Learning Python the Hard Way hangs (Fixed?)

FromGary Roach <garyroach@verizon.net>
Date2015-09-09 16:58 -0700
SubjectRe: Lesson 39 of Learning Python the Hard Way hangs (Fixed?)
Message-ID<mailman.305.1441861224.8327.python-list@python.org>
In reply to#96225
On 09/09/2015 01:45 PM, John Gordon wrote:
> In <mailman.280.1441823265.8327.python-list@python.org> Gary Roach <gary719_list1@verizon.net> writes:
>
>> 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!
> Where does the message "Execution Successful!" come from?  It seems like
> you have other code that you haven't shown us.
>
> In any case, I saved your code and ran it, and did not get an error.
>
No other code. Ninja-IDE tacked the "Execution Successful" at the end if 
the error message. Dont know why.

Interesting that you ran the code successfully. I have tried to run the 
code in both Ninja and at the command line and got the  exact same error 
message.

A note: If you could, please post your replies to the list. If you 
don't, it either breaks the thread or ends up sending a copy to my 
personal email address. I am assuming that your email client gives you 
that choice. If not, I'm not sure how you would assure that the proper 
way happens. I use the icedove (thunderbird) mail client and the choice 
is a drop down list at the top of the mail editor page.

Thanks for your reply.
Still confused

Gary R.

PS I copied over my code with the one from the lesson and all of a 
sudden it works. I then used the code I copied to the email. It also 
worked. Now I can't duplicate the problem. The problems fixed but ....

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


#96243 — Re: Lesson 39 of Learning Python the Hard Way hangs (Fixed?)

FromYBM <ybmess@nooos.fr.invalid>
Date2015-09-10 03:42 -0300
SubjectRe: Lesson 39 of Learning Python the Hard Way hangs (Fixed?)
Message-ID<55f1256a$0$4570$426a74cc@news.free.fr>
In reply to#96237
Le 09/09/2015 20:58, Gary Roach a écrit :
> On 09/09/2015 01:45 PM, John Gordon wrote:
>> In <mailman.280.1441823265.8327.python-list@python.org> Gary Roach
>> <gary719_list1@verizon.net> writes:
>>
>>> 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!
>> Where does the message "Execution Successful!" come from?  It seems like
>> you have other code that you haven't shown us.
>>
>> In any case, I saved your code and ran it, and did not get an error.
>>
> No other code. Ninja-IDE tacked the "Execution Successful" at the end if
> the error message. Dont know why.
>
> Interesting that you ran the code successfully. I have tried to run the
> code in both Ninja and at the command line and got the  exact same error
> message.
>
> A note: If you could, please post your replies to the list. If you
> don't, it either breaks the thread or ends up sending a copy to my
> personal email address. I am assuming that your email client gives you
> that choice. If not, I'm not sure how you would assure that the proper
> way happens. I use the icedove (thunderbird) mail client and the choice
> is a drop down list at the top of the mail editor page.
>
> Thanks for your reply.
> Still confused
>
> Gary R.
>
> PS I copied over my code with the one from the lesson and all of a
> sudden it works. I then used the code I copied to the email. It also
> worked. Now I can't duplicate the problem. The problems fixed but ....

So something was *necessarely* different between your test and what
you've posted here (which works for me as well, without error).

Your first tests were different from what you've posted, this is
certain.

Could be because you redefined "list" in your module, you shouldn't:

 >>> list('abcd')
['a', 'b', 'c', 'd']
 >>> def list(aMap):
...   """Prints out what's in the Map."""
...   for bucket in aMap:
...       if bucket:
...             for k, v in bucket:
...                 print k, v
 >>> list('abcd')
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "<stdin>", line 5, in list
ValueError: need more than 1 value to unpack

So if you've been using "list" as a constructor *after*
def list... *in your module* it wouldn't have behaved as expected.

As a rule of thumb : do not use types names (list, str, tuple, ...)
as identifiers in your code. Most of the times it is harmless first,
(especially in modules, because of namespaces isolation) but
sooner or later it leads to strange errors...

 >>> list(message)
['h', 'e', 'l', 'l', 'o']
 >>> list = [1,2,3]
 >>> list(message)
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable

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


#96238

FromGary Roach <garyroach@verizon.net>
Date2015-09-09 14:53 -0700
Message-ID<mailman.304.1441861224.8327.python-list@python.org>
In reply to#96225
On 09/09/2015 01:45 PM, John Gordon wrote:
> In <mailman.280.1441823265.8327.python-list@python.org> Gary Roach <gary719_list1@verizon.net> writes:
>
>> 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!
> Where does the message "Execution Successful!" come from?  It seems like
> you have other code that you haven't shown us.
>
> In any case, I saved your code and ran it, and did not get an error.
>
No other code. Ninja-IDE tacked the "Execution Successful" at the end if 
the error message. Dont know why.

Interesting that you ran the code successfully. I have tried to run the 
code in both Ninja and at the command line and got the  exact same error 
message.

A note: If you could, please post your replies to the list. If you 
don't, it either breaks the thread or ends up sending a copy to my 
personal email address. I am assuming that your email client gives you 
that choice. If not, I'm not sure how you would assure that the proper 
way happens. I use the icedove (thunderbird) mail client and the choice 
is a drop down list at the top of the mail editor page.

Thanks for your reply.
Still confused

Gary R.

[toc] | [prev] | [standalone]


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


csiph-web