Note that there are some explanatory texts on larger screens.

plurals
  1. POPython for loop not working on custom container
    text
    copied!<p><strong>EDIT</strong>: I added <code>__iter__</code> to my Map class (i forgot it didn't inherit from Tree), but now the for loop returns "generator objects":</p> <pre><code>&lt;generator object _next at 0x82a4b94&gt; Traceback (most recent call last): File "Map.py", line 43, in &lt;module&gt; print "First: %s, Second: %s" % (pair.first(), pair.second()) AttributeError: 'generator' object has no attribute 'first' </code></pre> <p>So there's an issue with my next function, right?</p> <hr> <p>For fun I created a RedBlack Tree in Python, and it's working properly. Now to copy the STL from C++ I'm creating a Map class to wrap the tree, for an alternative to the Python dict.</p> <p>The issue is when I try and loop through the Map, it's not working properly.</p> <pre><code>phonebook = Map() phonebook["Joe"] = "555-555-3422" phonebook["Rob"] = "231-523-2357" for pair in phonebook: print "First: %s, Second: %s" % (pair.first(), pair.second()) </code></pre> <p>The error that I get is:</p> <pre><code>Traceback (most recent call last): File "Map.py", line 38, in &lt;module&gt; for pair in phonebook: File "Map.py", line 19, in __getitem__ return self._tree.find(key) File "Python/Tree/SearchTree.py", line 82, in find raise TreeException('No node with key %s' % key) Tree.TreeException: 'No node with key 0' </code></pre> <p>I don't know why it's looking for key 0, when my keys are Strings. Using pdb I noticed that after the for loop starts, the first line executed is a call to <code>__getitem__</code> with key 0....</p> <p>Map is defined as:</p> <pre><code>class Map: def __init__(self): self._tree = RedBlackTree() def __getitem__(self, key): return self._tree.find(key) def __setitem__(self, key, item): self._tree.insert(Pair(key, item)) </code></pre> <p>From my understanding, I had to create an iterator for my tree, for this to work properly. I wasn't quite sure how to do this, so I looked around and combined a few approaches: (In my RedBlack tree implementation, NULL is an actual node)</p> <pre><code>class TreeIterator(): def __init__(self, root, size): self._current = root self._size = size self.num_visited = 0 def __iter__(self): return self def next(self): return self._next(self._current) def _next(self, curr): self.num_visited = self.num_visited + 1 if self.num_visited == self._size: raise StopIteration if curr.left is not None and curr.left is not TreeNode.NULL: for node in _next(curr.left): yield node yield curr if curr.right is not None and curr.right is not TreeNode.NULL: for node in _next(curr.right): yield node </code></pre> <p>and in my SearchTree superclass:</p> <pre><code>def __iter__(self): return TreeIterator(self.root, self.size) </code></pre> <p>What am I doing wrong?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload