how to create root with treelib?

Peter Otten __peter__ at web.de
Mon Aug 21 03:45:52 EDT 2017


dieter wrote:

> Ho Yeung Lee <jobmattcon at gmail.com> writes:
> 
>> http://treelib.readthedocs.io/en/latest/examples.html
>>
>> tree = Tree()
>> #create root
>> tree.create_node((0,0), "root")
>> result = [aa[0]]
>> previousnode = (0,0)
>>
>>>>> #create root
>> ... tree.create_node((0,0), "root")
>> Traceback (most recent call last):
>>   File "<stdin>", line 2, in <module>
>>   File "C:\Python27\lib\site-packages\treelib\node.py", line 142, in
>>   __repr__
>>     "tag=%r" % self.tag,
>> TypeError: not all arguments converted during string formatting
> 
> Check the code of "treelib.node" near line 142 in order to determine
> where the value for the attribute "tag" comes from.
> 
> Your error message suggests that "tag" might have gotten its value
> from the first parameter of "create_node". In this case, "self.tag"
> would be a tuple of 2 elements and the above error would occur because
> the pattern has a specification for a single element.
> In this case, do not pass tuples as first parameter to "create_node"
> (but strings).

While the author of the library probably didn't expect tag to be anything 
but a string the problem seems to affect the node's repr() only.
Once you replace the naked tag

>>     "tag=%r" % self.tag,

in treelib/node.py 

with a one-tuple

"tag=%r" % (self.tag,),

the problem should be fixed. Or you avoid the method invocation in the 
interactive interpreter by assigning to a dummy variable

>>> dummy = tree.create_node((0,0), "root")

No, repr(), no exception ;)

As MRAB says -- don't forget to report the bug to give the author a chance 
to fix it, once and for all.




More information about the Python-list mailing list