No, it wouldn't help with the iteration, but I thought you were having a
problem seeing the representation of your object. Now I'm trying to figure this
thing out….
It looks like the reason the 'char' only showing the first letter is because in
the add() method, the first letter of the string is what it assigns to head:
head, tail = s[0], s[1:]
So it uses just one letter at a time per node.
Joseph
On Aug 29, 2013, at 8:55 PM, Maria McKinley <[email protected]> wrote:
> Not yet, would that solve the problem that it is not iterating? Using some
> good old-fashioned print statements, I found out a little bit more.
>
> I added the following print statements:
>
> for char, node in self.root.iteritems():
> # node.value is none when not at end of word
>
> if node.value is None:
> print 'none'
> print 'char',char
> print 'node',node
> yield node.items()
> else:
> print 'stuff'
> yield node
>
> And this was the output. There are 2 words in the trie, but it only reports
> the first letter of each word, and stops:
>
> none
> char p
> node <trie.Trie instance at 0x10e8d0560>
> <generator object items at 0x10e8bc690>
> none
> char t
> node <trie.Trie instance at 0x10e8bab48>
> <generator object items at 0x10e8bc5a0>
>
> ~maria
>
>
> On Thu, Aug 29, 2013 at 7:59 PM, Joseph Wright <[email protected]> wrote:
> Have you tried defining __repr__?
>
> Joseph
>
> On Aug 29, 2013, at 7:56 PM, Maria McKinley <[email protected]> wrote:
>
>> Thanks Morris. That does answer one question, I can't assume code on
>> wikipedia is bug-free. Changing it doesn't solve the problem, unfortunately,
>> but you are right, time to hit the debugger. Thanks everyone.
>>
>> cheers,
>> Maria
>>
>>
>> On Thu, Aug 29, 2013 at 5:46 PM, Morris Bernstein
>> <[email protected]> wrote:
>> I hate to suggest this because I almost never use it, but have you
>> considered using the pdb debugger and setting a breakpoint?
>>
>> Meanwhile, your problem is here:
>> def items(self):
>> """Return an iterator over the items of the `Trie`."""
>> for char, node in self.root.iteritems():
>> if node.value is None:
>> yield node.items
>>
>> node.items is the the Trie.items() method bound to the node object.
>>
>> I think, taking a quick look at the code, you want to yield node.items(),
>> function call again. Looks like the same problem.
>>
>>
>>
>>
>>
>>
>>
>> On Thu, Aug 29, 2013 at 5:17 PM, Maria McKinley <[email protected]>
>> wrote:
>> Doh. Thanks. This does the trick, but it gives me the instance location. I
>> assumed this is because there is no __str__ method defined, but when I added
>> a __str__ method it didn't change anything. Probably didn't implement the
>> __str__ method correctly, but since I didn't even get an error, not sure
>> this was even the problem. (Pretty sure, for example, that I shouldn't
>> always be referencing the head node.)
>>
>> def __str__(self):
>> return "Node letter is %s" % (self.root[0])
>>
>> for c in mytrie.items():
>> print c
>> ...:
>> <bound method Trie.items of <trie.Trie instance at 0x1010dc710>>
>> <bound method Trie.items of <trie.Trie instance at 0x1010dca70>>
>>
>> thanks again,
>> Maria
>>
>>
>> On Thu, Aug 29, 2013 at 4:40 PM, Cris Ewing <[email protected]> wrote:
>> I expect that the problem here is that you are attempting to iterate over
>> the method itself, rather than its result. You'd need to call the method to
>> do that:
>>
>> for c in mytrie.items():
>> print c
>>
>> hth
>>
>> c
>>
>> On Aug 29, 2013, at 4:38 PM, Maria McKinley wrote:
>>
>>> Hello,
>>>
>>> I hope someone on this list doesn't mind answering what I think is a quick
>>> question. I have been playing around with the python code found here:
>>>
>>> http://en.wikipedia.org/wiki/Trie#A_Python_version
>>>
>>> I can't get the iterator to work, and I wonder if I'm not calling it
>>> correctly. I thought once I made my object, and added stuff to it, I could
>>> just do this:
>>>
>>> for c in mytrie.items:
>>> print c
>>>
>>> but I get this error:
>>>
>>> TypeError: 'instancemethod' object is not iterable
>>>
>>> What am I doing wrong?
>>>
>>> thanks,
>>> Maria
>>
>> Cris Ewing
>> --------------------------------------------------
>> Principal, Cris Ewing, Developer LLC
>> http://www.crisewing.com
>> [email protected]
>> 1.206.724.2112
>>
>>
>>
>>
>> --
>> Maria Mckinley
>> Software Developer with Bonus SysAdmin Experience
>> www.mariakathryn.net
>> www.linkedin.com/in/mariamckinley
>>
>>
>>
>>
>> --
>> Maria Mckinley
>> Software Developer with Bonus SysAdmin Experience
>> www.mariakathryn.net
>> www.linkedin.com/in/mariamckinley
>
>
>
>
> --
> Maria Mckinley
> Software Developer with Bonus SysAdmin Experience
> www.mariakathryn.net
> www.linkedin.com/in/mariamckinley