I create a tree with anytree library. I want to be able to modify it, then export - save it to disk, and import it back with the modifications. For instance, the example tree:
udo = Node("Udo")
marc = Node("Marc", parent=udo)
lian = Node("Lian", parent=marc)
dan = Node("Dan", parent=udo)
jet = Node("Jet", parent=dan)
jan = Node("Jan", parent=dan)
joe = Node("Joe", parent=dan)
Udo
├── Marc
│ └── Lian
└── Dan
├── Jet
├── Jan
└── Joe
I can modify it, for instance cutting Dan off and adding a children to Marc
dan.parent = None
bonny = Node ("Bonny", parent = marc)
Udo
└── Marc
├── Lian
└── Bonny
But when I export the tree to json and then import it back, the only node I seem to be able to reference is the root. So I can't do this kind of modification anymore because the variable names like dan
or marc
are not there, ie, I don't find a way to reference a node. Am I missing anything?
with open ('cajon/anytreeexample.json', 'r+', encoding = 'utf-8') as f:
datos = importer.read(f)
print (datos)
This means that after importing the tree what you have is just a root node
AnyNode(name='Udo') # Udo is the root
From here you can get Udo's children, and the children's children like
marc, dan = udo.children
lian = marc.children
jet, jan, joe = dan.children
But they are not working as a node
print (lian.parent)
AttributeError: 'tuple' object has no attribute 'parent'
And seems you cannot attach a children to them, which is my main purpose with this structure:
sonny = AnyNode("Sonny", parent = lian)
TypeError: __init__() got multiple values for argument 'parent'
So my question is, is there a way to load the json saved tree into a proper anytree structure, where you can append new nodes?
variable = value
, there is no other way. "Also it seems like some are not working as a node".children
returns a tuple, not a node. "And seems I also don't know how to add children" How aboutsonny = Node("Sonny", parent = dan)
? – Tenon.children
always return a tuple of nodes, exporting has nothing to do with it. In order to appendnewnode
undernodeX
, assuming bothnewnode
andnodeX
are nodes, you just donewnode.parent = nodeX
. – Tenon,
– Dardar