0

I hope you guys are fine. I want to manipulate data which is in any-tree(python library) node
I am storing my dictionary in my node and want to take values from node and manipulate it.

My code:

data={
            "man":False,
           "goat":False,
           "lion":False,
           "grass":False
          }
udo = Node(data)
print (udo)


print (udo["man"]) 

Result:

Node("/{'man': False, 'goat': False, 'lion': False, 'grass': False}")

now if I will call object with index of Dictionary It generates an error. Error!

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-27-2d29abc19bbb> in <module>()
      8 print (udo)
      9 
---> 10 print (udo["man"])

TypeError: 'Node' object is not subscriptable

I read python any-tree documentation but didn't find any help.

Sohaib Anwaar
  • 1,517
  • 1
  • 12
  • 29

2 Answers2

0

Ohh! I got now After reading any tree documentation more carefully. this is done by the function name which is the function of node and it give the same (value, data structure.. etc) which you have assigned to the node. Code: data={ "man":False, "goat":False, "lion":False, "grass":False } udo = Node(data) print (udo)

print(udo.name["man"])

Results:

Node("/{'man': False, 'goat': False, 'lion': False, 'grass': False}")
Udo Man is False
Sohaib Anwaar
  • 1,517
  • 1
  • 12
  • 29
0

I think AnyNode is what you are looking for

from anytree import AnyNode
data = {
    "man": False,
    "goat": False,
    "lion": False,
    "grass": False
}
a = AnyNode(**data)
print(a)
AnyNode(goat=False, grass=False, lion=False, man=False)
print(a.man)
False
c0fec0de
  • 651
  • 8
  • 4