FAQ
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%6):
      """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Þfault)
      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.

Search Discussions

  • John Gordon at Sep 9, 2015 at 8:45 pm

    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 at panix.com B is for Basil, assaulted by bears
                                     -- Edward Gorey, "The Gashlycrumb Tinies"
  • Denis McMahon at Sep 9, 2015 at 9:36 pm

    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 at gmail.com
  • Gary Roach at Sep 9, 2015 at 9:53 pm

    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.
  • Gary Roach at Sep 9, 2015 at 11:58 pm

    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 ....
  • Gary Roach at Sep 9, 2015 at 11:58 pm

    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 ....
  • YBM at Sep 10, 2015 at 6:42 am

    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
  • Chris Angelico at Sep 10, 2015 at 4:40 am

    On Thu, Sep 10, 2015 at 3:27 AM, Gary Roach wrote:
    Python 2.7 (It's going to be a while before 2,7 goes away. There is just too
    much code out there.

    2.7 isn't going to go away, but that doesn't mean you should learn on
    it. Unless you have a particular reason for learning 2.7, I would
    recommend picking up 3.4 (or 3.5, which is about to be released).


    ChrisA
  • Ben Finney at Sep 10, 2015 at 4:56 am

    Gary Roach <gary719_list1@verizon.net> writes:


    Python 2.7 (It's going to be a while before 2,7 goes away. There is
    just too much code out there.

    The time since Python 2 got any improvements is long in the past, and it
    will never get any more.


    It is rapidly becoming the case that new libraries just don't support
    Python 2, for the same reason.


    Rather than learning the dead-end Python 2, you should be using the time
    better by learning Python 3 which is actively-developed and will serve
    you much longer.


    --
      \ ?Remember: every member of your ?target audience? also owns a |
       `\ broadcasting station. These ?targets? can shoot back.? ?Michael |
    _o__) Rathbun to advertisers, news.admin.net-abuse.email |
    Ben Finney

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedSep 9, '15 at 5:27p
activeSep 10, '15 at 6:42a
posts9
users7
websitepython.org

People

Translate

site design / logo © 2019 Grokbase