anynode.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # -*- coding: utf-8 -*-
  2. from .nodemixin import NodeMixin
  3. from .util import _repr
  4. class AnyNode(NodeMixin, object):
  5. def __init__(self, parent=None, children=None, **kwargs):
  6. u"""
  7. A generic tree node with any `kwargs`.
  8. Keyword Args:
  9. parent: Reference to parent node.
  10. children: Iterable with child nodes.
  11. *: Any other given attribute is just stored as object attribute.
  12. Other than :any:`Node` this class has no default idenifier.
  13. It is up to the user to use other attributes for identification.
  14. The `parent` attribute refers the parent node:
  15. >>> from anytree import AnyNode, RenderTree
  16. >>> root = AnyNode(id="root")
  17. >>> s0 = AnyNode(id="sub0", parent=root)
  18. >>> s0b = AnyNode(id="sub0B", parent=s0, foo=4, bar=109)
  19. >>> s0a = AnyNode(id="sub0A", parent=s0)
  20. >>> s1 = AnyNode(id="sub1", parent=root)
  21. >>> s1a = AnyNode(id="sub1A", parent=s1)
  22. >>> s1b = AnyNode(id="sub1B", parent=s1, bar=8)
  23. >>> s1c = AnyNode(id="sub1C", parent=s1)
  24. >>> s1ca = AnyNode(id="sub1Ca", parent=s1c)
  25. >>> root
  26. AnyNode(id='root')
  27. >>> s0
  28. AnyNode(id='sub0')
  29. >>> print(RenderTree(root))
  30. AnyNode(id='root')
  31. ├── AnyNode(id='sub0')
  32. │ ├── AnyNode(bar=109, foo=4, id='sub0B')
  33. │ └── AnyNode(id='sub0A')
  34. └── AnyNode(id='sub1')
  35. ├── AnyNode(id='sub1A')
  36. ├── AnyNode(bar=8, id='sub1B')
  37. └── AnyNode(id='sub1C')
  38. └── AnyNode(id='sub1Ca')
  39. The same tree can be constructed by using the `children` attribute:
  40. >>> root = AnyNode(id="root", children=[
  41. ... AnyNode(id="sub0", children=[
  42. ... AnyNode(id="sub0B", foo=4, bar=109),
  43. ... AnyNode(id="sub0A"),
  44. ... ]),
  45. ... AnyNode(id="sub1", children=[
  46. ... AnyNode(id="sub1A"),
  47. ... AnyNode(id="sub1B", bar=8),
  48. ... AnyNode(id="sub1C", children=[
  49. ... AnyNode(id="sub1Ca"),
  50. ... ]),
  51. ... ]),
  52. ... ])
  53. >>> print(RenderTree(root))
  54. AnyNode(id='root')
  55. ├── AnyNode(id='sub0')
  56. │ ├── AnyNode(bar=109, foo=4, id='sub0B')
  57. │ └── AnyNode(id='sub0A')
  58. └── AnyNode(id='sub1')
  59. ├── AnyNode(id='sub1A')
  60. ├── AnyNode(bar=8, id='sub1B')
  61. └── AnyNode(id='sub1C')
  62. └── AnyNode(id='sub1Ca')
  63. Node attributes can be added, modified and deleted the pythonic way:
  64. >>> root.new = 'a new attribute'
  65. >>> s0b.bar = 110 # modified
  66. >>> del s1b.bar
  67. >>> print(RenderTree(root))
  68. AnyNode(id='root', new='a new attribute')
  69. ├── AnyNode(id='sub0')
  70. │ ├── AnyNode(bar=109, foo=4, id='sub0B')
  71. │ └── AnyNode(id='sub0A')
  72. └── AnyNode(id='sub1')
  73. ├── AnyNode(id='sub1A')
  74. ├── AnyNode(bar=8, id='sub1B')
  75. └── AnyNode(id='sub1C')
  76. └── AnyNode(id='sub1Ca')
  77. """
  78. self.__dict__.update(kwargs)
  79. self.parent = parent
  80. if children:
  81. self.children = children
  82. def __repr__(self):
  83. return _repr(self)