| 1 |
******************** |
| 2 |
Any Python Tree Data |
| 3 |
******************** |
| 4 |
|
| 5 |
.. image:: https://badge.fury.io/py/anytree.svg |
| 6 |
:target: https://badge.fury.io/py/anytree |
| 7 |
|
| 8 |
.. image:: https://img.shields.io/pypi/dm/anytree.svg?label=pypi%20downloads |
| 9 |
:target: https://pypi.python.org/pypi/anytree |
| 10 |
|
| 11 |
.. image:: https://readthedocs.org/projects/anytree/badge/?version=latest |
| 12 |
:target: https://anytree.readthedocs.io/en/latest/?badge=latest |
| 13 |
|
| 14 |
.. image:: https://coveralls.io/repos/github/c0fec0de/anytree/badge.svg |
| 15 |
:target: https://coveralls.io/github/c0fec0de/anytree |
| 16 |
|
| 17 |
.. image:: https://readthedocs.org/projects/anytree/badge/?version=stable |
| 18 |
:target: https://anytree.readthedocs.io/en/stable/ |
| 19 |
|
| 20 |
.. image:: https://img.shields.io/pypi/pyversions/anytree.svg |
| 21 |
:target: https://pypi.python.org/pypi/anytree |
| 22 |
|
| 23 |
.. image:: https://img.shields.io/github/contributors/c0fec0de/anytree.svg |
| 24 |
:target: https://github.com/c0fec0de/anytree/graphs/contributors/ |
| 25 |
|
| 26 |
.. image:: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square |
| 27 |
:target: http://makeapullrequest.com |
| 28 |
|
| 29 |
.. image:: https://img.shields.io/github/issues-pr/c0fec0de/anytree.svg |
| 30 |
:target: https://github.com/c0fec0de/anytree/pulls |
| 31 |
|
| 32 |
.. image:: https://img.shields.io/github/issues-pr-closed/c0fec0de/anytree.svg |
| 33 |
:target: https://github.com/c0fec0de/anytree/pulls?q=is%3Apr+is%3Aclosed |
| 34 |
|
| 35 |
Simple, lightweight and extensible Tree_ data structure. |
| 36 |
|
| 37 |
Feel free to share_ info about your anytree project. |
| 38 |
|
| 39 |
.. _share: https://github.com/c0fec0de/anytree/issues/34 |
| 40 |
|
| 41 |
.. toctree:: |
| 42 |
:maxdepth: 2 |
| 43 |
|
| 44 |
installation |
| 45 |
intro |
| 46 |
api |
| 47 |
importer |
| 48 |
exporter |
| 49 |
tricks |
| 50 |
|
| 51 |
|
| 52 |
Links |
| 53 |
===== |
| 54 |
|
| 55 |
* Documentation_ |
| 56 |
* GitHub_ |
| 57 |
* PyPI_ |
| 58 |
* Changelog_ |
| 59 |
* Issues_ |
| 60 |
* Contributors_ |
| 61 |
* If you enjoy anytree_ |
| 62 |
|
| 63 |
.. image:: https://cdn.buymeacoffee.com/buttons/default-orange.png |
| 64 |
:width: 150 |
| 65 |
:target: https://www.buymeacoffee.com/1oYX0sw |
| 66 |
|
| 67 |
Feel free to share_ info about your anytree project. |
| 68 |
|
| 69 |
.. _anytree: https://anytree.readthedocs.io/en/stable/ |
| 70 |
.. _Documentation: https://anytree.readthedocs.io/en/stable/ |
| 71 |
.. _GitHub: https://github.com/c0fec0de/anytree |
| 72 |
.. _PyPI: https://pypi.org/project/anytree/ |
| 73 |
.. _Changelog: https://github.com/c0fec0de/anytree/releases |
| 74 |
.. _Issues: https://github.com/c0fec0de/anytree/issues |
| 75 |
.. _Contributors: https://github.com/c0fec0de/anytree/graphs/contributors |
| 76 |
.. _share: https://github.com/c0fec0de/anytree/issues/34 |
| 77 |
.. _Tree: https://en.wikipedia.org/wiki/Tree_(data_structure) |
| 78 |
|
| 79 |
Getting started |
| 80 |
=============== |
| 81 |
|
| 82 |
.. _getting_started: |
| 83 |
|
| 84 |
Usage is simple. |
| 85 |
|
| 86 |
**Construction** |
| 87 |
|
| 88 |
>>> from anytree import Node, RenderTree |
| 89 |
>>> udo = Node("Udo") |
| 90 |
>>> marc = Node("Marc", parent=udo) |
| 91 |
>>> lian = Node("Lian", parent=marc) |
| 92 |
>>> dan = Node("Dan", parent=udo) |
| 93 |
>>> jet = Node("Jet", parent=dan) |
| 94 |
>>> jan = Node("Jan", parent=dan) |
| 95 |
>>> joe = Node("Joe", parent=dan) |
| 96 |
|
| 97 |
**Node** |
| 98 |
|
| 99 |
>>> print(udo) |
| 100 |
Node('/Udo') |
| 101 |
>>> print(joe) |
| 102 |
Node('/Udo/Dan/Joe') |
| 103 |
|
| 104 |
**Tree** |
| 105 |
|
| 106 |
>>> for pre, fill, node in RenderTree(udo): |
| 107 |
... print("%s%s" % (pre, node.name)) |
| 108 |
Udo |
| 109 |
├── Marc |
| 110 |
│ └── Lian |
| 111 |
└── Dan |
| 112 |
├── Jet |
| 113 |
├── Jan |
| 114 |
└── Joe |
| 115 |
|
| 116 |
For details see :any:`Node` and :any:`RenderTree`. |
| 117 |
|
| 118 |
**Visualization** |
| 119 |
|
| 120 |
>>> from anytree.exporter import UniqueDotExporter |
| 121 |
>>> # graphviz needs to be installed for the next line! |
| 122 |
>>> UniqueDotExporter(udo).to_picture("udo.png") # doctest: +SKIP |
| 123 |
|
| 124 |
.. image:: static/udo.png |
| 125 |
|
| 126 |
The :any:`UniqueDotExporter` can be started at any node and has various formatting hookups: |
| 127 |
|
| 128 |
>>> UniqueDotExporter(dan, |
| 129 |
... nodeattrfunc=lambda node: "fixedsize=true, width=1, height=1, shape=diamond", |
| 130 |
... edgeattrfunc=lambda parent, child: "style=bold" |
| 131 |
... ).to_picture("dan.png") # doctest: +SKIP |
| 132 |
|
| 133 |
.. image:: static/dan.png |
| 134 |
|
| 135 |
**Manipulation** |
| 136 |
|
| 137 |
A second tree: |
| 138 |
|
| 139 |
>>> mary = Node("Mary") |
| 140 |
>>> urs = Node("Urs", parent=mary) |
| 141 |
>>> chris = Node("Chris", parent=mary) |
| 142 |
>>> marta = Node("Marta", parent=mary) |
| 143 |
>>> print(RenderTree(mary)) |
| 144 |
Node('/Mary') |
| 145 |
├── Node('/Mary/Urs') |
| 146 |
├── Node('/Mary/Chris') |
| 147 |
└── Node('/Mary/Marta') |
| 148 |
|
| 149 |
Append: |
| 150 |
|
| 151 |
>>> udo.parent = mary |
| 152 |
>>> print(RenderTree(mary)) |
| 153 |
Node('/Mary') |
| 154 |
├── Node('/Mary/Urs') |
| 155 |
├── Node('/Mary/Chris') |
| 156 |
├── Node('/Mary/Marta') |
| 157 |
└── Node('/Mary/Udo') |
| 158 |
├── Node('/Mary/Udo/Marc') |
| 159 |
│ └── Node('/Mary/Udo/Marc/Lian') |
| 160 |
└── Node('/Mary/Udo/Dan') |
| 161 |
├── Node('/Mary/Udo/Dan/Jet') |
| 162 |
├── Node('/Mary/Udo/Dan/Jan') |
| 163 |
└── Node('/Mary/Udo/Dan/Joe') |
| 164 |
|
| 165 |
Subtree rendering: |
| 166 |
|
| 167 |
>>> print(RenderTree(marc)) |
| 168 |
Node('/Mary/Udo/Marc') |
| 169 |
└── Node('/Mary/Udo/Marc/Lian') |
| 170 |
|
| 171 |
Cut/Delete: |
| 172 |
|
| 173 |
>>> dan.parent = None |
| 174 |
>>> print(RenderTree(dan)) |
| 175 |
Node('/Dan') |
| 176 |
├── Node('/Dan/Jet') |
| 177 |
├── Node('/Dan/Jan') |
| 178 |
└── Node('/Dan/Joe') |
| 179 |
|
| 180 |
>>> print(RenderTree(mary)) |
| 181 |
Node('/Mary') |
| 182 |
├── Node('/Mary/Urs') |
| 183 |
├── Node('/Mary/Chris') |
| 184 |
├── Node('/Mary/Marta') |
| 185 |
└── Node('/Mary/Udo') |
| 186 |
└── Node('/Mary/Udo/Marc') |
| 187 |
└── Node('/Mary/Udo/Marc/Lian') |
| 188 |
|
| 189 |
**Extending any python class to become a tree node** |
| 190 |
|
| 191 |
The entire tree magic is encapsulated by :any:`NodeMixin`, |
| 192 |
add it as base class and the class becomes a tree node: |
| 193 |
|
| 194 |
>>> from anytree import NodeMixin, RenderTree |
| 195 |
>>> class MyBaseClass(object): # Just an example of a base class |
| 196 |
... foo = 4 |
| 197 |
>>> class MyClass(MyBaseClass, NodeMixin): # Add Node feature |
| 198 |
... def __init__(self, name, length, width, parent=None, children=None): |
| 199 |
... super(MyClass, self).__init__() |
| 200 |
... self.name = name |
| 201 |
... self.length = length |
| 202 |
... self.width = width |
| 203 |
... self.parent = parent |
| 204 |
... if children: # set children only if given |
| 205 |
... self.children = children |
| 206 |
|
| 207 |
Just set the `parent` attribute to reflect the tree relation: |
| 208 |
|
| 209 |
>>> my0 = MyClass('my0', 0, 0) |
| 210 |
>>> my1 = MyClass('my1', 1, 0, parent=my0) |
| 211 |
>>> my2 = MyClass('my2', 0, 2, parent=my0) |
| 212 |
|
| 213 |
>>> for pre, fill, node in RenderTree(my0): |
| 214 |
... treestr = u"%s%s" % (pre, node.name) |
| 215 |
... print(treestr.ljust(8), node.length, node.width) |
| 216 |
my0 0 0 |
| 217 |
├── my1 1 0 |
| 218 |
└── my2 0 2 |
| 219 |
|
| 220 |
The `children` can be used likewise: |
| 221 |
|
| 222 |
>>> my0 = MyClass('my0', 0, 0, children=[ |
| 223 |
... MyClass('my1', 1, 0), |
| 224 |
... MyClass('my2', 0, 2), |
| 225 |
... ]) |
| 226 |
|
| 227 |
>>> for pre, fill, node in RenderTree(my0): |
| 228 |
... treestr = u"%s%s" % (pre, node.name) |
| 229 |
... print(treestr.ljust(8), node.length, node.width) |
| 230 |
my0 0 0 |
| 231 |
├── my1 1 0 |
| 232 |
└── my2 0 2 |