Skip to main content

Powerful and Lightweight Python Tree Data Structure..

Project description

https://badge.fury.io/py/anytree.svg https://travis-ci.org/c0fec0de/anytree.svg?branch=master https://coveralls.io/repos/github/c0fec0de/anytree/badge.svg https://readthedocs.org/projects/anytree/badge/?version=2.4.1 https://codeclimate.com/github/c0fec0de/anytree.png https://img.shields.io/pypi/pyversions/anytree.svg https://landscape.io/github/c0fec0de/anytree/master/landscape.svg?style=flat https://img.shields.io/badge/code%20style-pep8-brightgreen.svg https://img.shields.io/badge/code%20style-pep257-brightgreen.svg

Documentation

The Documentation is hosted on http://anytree.readthedocs.io/en/2.4.1/

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.exporter import DotExporter
>>> # graphviz needs to be installed for the next line!
>>> DotExporter(udo).to_picture("tree.png")
http://anytree.readthedocs.io/en/latest/_images/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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

anytree-2.4.1.tar.gz (18.2 kB view details)

Uploaded Source

File details

Details for the file anytree-2.4.1.tar.gz.

File metadata

  • Download URL: anytree-2.4.1.tar.gz
  • Upload date:
  • Size: 18.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for anytree-2.4.1.tar.gz
Algorithm Hash digest
SHA256 313502f41cbc1c5dc2a9744a13d3cef5ebc8812efa1f08161c59c3015d24a4e5
MD5 75884652157e58064a653ee7b5e3270f
BLAKE2b-256 cbdb59b0af46350e924bd045cc0df537a4837ec03cc4de5e8848c7f88420fa1f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page