Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modifying the identifier of a node is not working properly #84

Closed
ghost opened this issue Nov 27, 2017 · 3 comments
Closed

Modifying the identifier of a node is not working properly #84

ghost opened this issue Nov 27, 2017 · 3 comments
Assignees

Comments

@ghost
Copy link

ghost commented Nov 27, 2017

Hello,

tree = Tree()
tree.create_node("Harry", "harry")
tree.create_node("Jane", "jane", parent="harry")
n= tree.get_node("jane")
n.identifier=("xyz")

tree.get_node("xyz") ### returns None 
tree.get_node("jane") ### return a Node object whose identifier is "xyz" ! 

Hope you can address this,

Thnaks.

@c0fec0de
Copy link
Contributor

The Tree class maintains a internal _nodes dictionary which is not updated on identifier and other changes.

You may want to use a reimplementation of treelib called anytree

from anytree import Node
from anytree import find_by_attr


root = Node('harry')
Node('jane', parent=root)
n = find_by_attr(root, 'jane')
n.name = 'xyz'

find_by_attr(root, 'xyz')  ### returns     Node('/harry/xyz')
find_by_attr(root, 'jane')  ### returns None

HTH

@ghost
Copy link
Author

ghost commented Nov 29, 2017

Perfect, used anytree and it is solved now. many thanks.

@caesar0301 caesar0301 self-assigned this Dec 1, 2017
@caesar0301
Copy link
Owner

Now treelib supports node attribute modification:

Update non-root node

from treelib import Tree, Node
tree = Tree()
tree.create_node("Harry", "harry")
tree.create_node("Jane", "jane", parent="harry")

tree.update_node("jane", identifier='xyz')
assert tree["xyz"].identifier == 'xyz'

Update root node

from treelib import Tree, Node
tree = Tree()
tree.create_node("Harry", "harry")
tree.create_node("Jane", "jane", parent="harry")

tree.update_node("harry", identifier='xyz')
assert tree.root == 'xyz'
assert tree['jane'].bpointer == 'xyz'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants