Might be because you're using generators? make sure to "materialize" the generator. with for example list() or for .. in
yield will pause until resumed. so list(trie.items()) or whatever. On Thu, Aug 29, 2013 at 8:55 PM, Maria McKinley <mar...@mariakathryn.net>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 <jos...@mammalia.net>wrote: > >> Have you tried defining __repr__? >> >> Joseph >> >> On Aug 29, 2013, at 7:56 PM, Maria McKinley <mar...@mariakathryn.net> >> 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 < >> mor...@systems-deployment.com> 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 <mar...@mariakathryn.net >>> > 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 <c...@crisewing.com> 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 >>>>> c...@crisewing.com >>>>> 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 >