Powerful and Lightweight Python Tree Data Structure with various plugins.
Project description
Documentation
The Documentation is hosted on http://anytree.readthedocs.io/en/latest/
Getting started
Usage is simple.
Construction
>>> from anytree import Node, RenderTree >>> 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)
Node
>>> print(udo) Node('/Udo') >>> print(joe) Node('/Udo/Dan/Joe')
Tree
>>> for pre, fill, node in RenderTree(udo): ... print("%s%s" % (pre, node.name)) Udo ├── Marc │ └── Lian └── Dan ├── Jet ├── Jan └── Joe
>>> from anytree.dotexport import RenderTreeGraph >>> # graphviz needs to be installed for the next line! >>> RenderTreeGraph(root).to_picture("tree.png")
Manipulation
A second tree:
>>> mary = Node("Mary") >>> urs = Node("Urs", parent=mary) >>> chris = Node("Chris", parent=mary) >>> marta = Node("Marta", parent=mary) >>> print(RenderTree(mary)) Node('/Mary') ├── Node('/Mary/Urs') ├── Node('/Mary/Chris') └── Node('/Mary/Marta')
Append:
>>> udo.parent = mary >>> print(RenderTree(mary)) Node('/Mary') ├── Node('/Mary/Urs') ├── Node('/Mary/Chris') ├── Node('/Mary/Marta') └── Node('/Mary/Udo') ├── Node('/Mary/Udo/Marc') │ └── Node('/Mary/Udo/Marc/Lian') └── Node('/Mary/Udo/Dan') ├── Node('/Mary/Udo/Dan/Jet') ├── Node('/Mary/Udo/Dan/Jan') └── Node('/Mary/Udo/Dan/Joe')
Subtree rendering:
>>> print(RenderTree(marc)) Node('/Mary/Udo/Marc') └── Node('/Mary/Udo/Marc/Lian')
Cut:
>>> dan.parent = None >>> print(RenderTree(dan)) Node('/Dan') ├── Node('/Dan/Jet') ├── Node('/Dan/Jan') └── Node('/Dan/Joe')
Installation
To install the anytree module run:
pip install anytree
If you do not have write-permissions to the python installation, try:
pip install anytree --user
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
anytree-1.0.4.tar.gz
(9.0 kB
view details)
File details
Details for the file anytree-1.0.4.tar.gz
.
File metadata
- Download URL: anytree-1.0.4.tar.gz
- Upload date:
- Size: 9.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ce2dfd438e91a5d4e61164c111cd84fc1bd1f4c7c6d3d1439f8767fbfa68a10f |
|
MD5 | 39f420979367f050c6b63ea2446aef78 |
|
BLAKE2b-256 | 84bede81d82099c40278b3033add4dd2b31d6d7d0295fb428bb307b45a6d59fc |