anytree


Nameanytree JSON
Version 2.12.1 PyPI version JSON
download
home_page
SummaryPowerful and Lightweight Python Tree Data Structure with various plugins
upload_time2023-11-16 21:53:02
maintainer
docs_urlNone
authorc0fec0de
requires_python>=3.7.2,<4
licenseApache-2.0
keywords tree tree data treelib tree walk tree structure
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            .. image:: https://badge.fury.io/py/anytree.svg
    :target: https://badge.fury.io/py/anytree

.. image:: https://img.shields.io/pypi/dm/anytree.svg?label=pypi%20downloads
   :target: https://pypi.python.org/pypi/anytree

.. image:: https://readthedocs.org/projects/anytree/badge/?version=latest
    :target: https://anytree.readthedocs.io/en/latest/?badge=latest

.. image:: https://coveralls.io/repos/github/c0fec0de/anytree/badge.svg
    :target: https://coveralls.io/github/c0fec0de/anytree

.. image:: https://readthedocs.org/projects/anytree/badge/?version=stable
    :target: https://anytree.readthedocs.io/en/stable

.. image:: https://api.codeclimate.com/v1/badges/e6d325d6fd23a93aab20/maintainability
   :target: https://codeclimate.com/github/c0fec0de/anytree/maintainability
   :alt: Maintainability

.. image:: https://img.shields.io/pypi/pyversions/anytree.svg
   :target: https://pypi.python.org/pypi/anytree

.. image:: https://img.shields.io/badge/code%20style-pep8-brightgreen.svg
   :target: https://www.python.org/dev/peps/pep-0008/

.. image:: https://img.shields.io/badge/code%20style-pep257-brightgreen.svg
   :target: https://www.python.org/dev/peps/pep-0257/

.. image:: https://img.shields.io/badge/linter-pylint-%231674b1?style=flat
   :target: https://www.pylint.org/

.. image:: https://img.shields.io/badge/code%20style-black-000000.svg
   :target: https://github.com/psf/black

.. image:: https://img.shields.io/github/contributors/c0fec0de/anytree.svg
   :target: https://github.com/c0fec0de/anytree/graphs/contributors/

.. image:: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square
   :target: http://makeapullrequest.com

.. image:: https://img.shields.io/github/issues-pr/c0fec0de/anytree.svg
   :target: https://github.com/c0fec0de/anytree/pulls

.. image:: https://img.shields.io/github/issues-pr-closed/c0fec0de/anytree.svg
   :target: https://github.com/c0fec0de/anytree/pulls?q=is%3Apr+is%3Aclosed


Links
=====

* Documentation_
* PyPI_
* GitHub_
* Changelog_
* Issues_
* Contributors_
* If you enjoy anytree_

.. image:: https://github.com/c0fec0de/anytree/raw/devel/docs/static/buymeacoffee.png
   :target: https://www.buymeacoffee.com/1oYX0sw

.. _anytree: https://anytree.readthedocs.io/en/stable/
.. _Documentation: https://anytree.readthedocs.io/en/stable/
.. _PyPI: https://pypi.org/project/anytree
.. _GitHub: https://github.com/c0fec0de/anytree
.. _Changelog: https://github.com/c0fec0de/anytree/releases
.. _Issues: https://github.com/c0fec0de/anytree/issues
.. _Contributors: https://github.com/c0fec0de/anytree/graphs/contributors

.. _Node: https://anytree.readthedocs.io/en/stable/api/anytree.node.html#anytree.node.node.Node
.. _RenderTree: https://anytree.readthedocs.io/en/stable/api/anytree.render.html#anytree.render.RenderTree
.. _UniqueDotExporter: https://anytree.readthedocs.io/en/stable/exporter/dotexporter.html#anytree.exporter.dotexporter.UniqueDotExporter
.. _NodeMixin: https://anytree.readthedocs.io/en/stable/api/anytree.node.html#anytree.node.nodemixin.NodeMixin
.. _Importers: https://anytree.readthedocs.io/en/stable/importer.html
.. _Exporters: https://anytree.readthedocs.io/en/stable/exporter.html

Getting started
---------------

.. _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

For details see Node_ and RenderTree_.

**Visualization**

>>> from anytree.exporter import UniqueDotExporter
>>> # graphviz needs to be installed for the next line!
>>> UniqueDotExporter(udo).to_picture("udo.png")

.. image:: https://anytree.readthedocs.io/en/latest/_images/udo.png

The UniqueDotExporter_ can be started at any node and has various formatting hookups:

>>> UniqueDotExporter(dan,
...                   nodeattrfunc=lambda node: "fixedsize=true, width=1, height=1, shape=diamond",
...                   edgeattrfunc=lambda parent, child: "style=bold"
... ).to_picture("dan.png")

.. image:: https://anytree.readthedocs.io/en/latest/_images/dan.png

There are various other Importers_ and Exporters_.

**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')

**Extending any python class to become a tree node**

The entire tree magic is encapsulated by NodeMixin_
add it as base class and the class becomes a tree node:

>>> from anytree import NodeMixin, RenderTree
>>> class MyBaseClass(object):  # Just an example of a base class
...     foo = 4
>>> class MyClass(MyBaseClass, NodeMixin):  # Add Node feature
...     def __init__(self, name, length, width, parent=None, children=None):
...         super(MyClass, self).__init__()
...         self.name = name
...         self.length = length
...         self.width = width
...         self.parent = parent
...         if children:
...             self.children = children

Just set the `parent` attribute to reflect the tree relation:

>>> my0 = MyClass('my0', 0, 0)
>>> my1 = MyClass('my1', 1, 0, parent=my0)
>>> my2 = MyClass('my2', 0, 2, parent=my0)

>>> for pre, fill, node in RenderTree(my0):
...     treestr = u"%s%s" % (pre, node.name)
...     print(treestr.ljust(8), node.length, node.width)
my0      0 0
├── my1  1 0
└── my2  0 2

The `children` can be used likewise:

>>> my0 = MyClass('my0', 0, 0, children=[
...     MyClass('my1', 1, 0),
...     MyClass('my2', 0, 2),
... ])

>>> for pre, fill, node in RenderTree(my0):
...     treestr = u"%s%s" % (pre, node.name)
...     print(treestr.ljust(8), node.length, node.width)
my0      0 0
├── my1  1 0
└── my2  0 2

Documentation
-------------

Please see the Documentation_ for all details.

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

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "anytree",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7.2,<4",
    "maintainer_email": "",
    "keywords": "tree,tree data,treelib,tree walk,tree structure",
    "author": "c0fec0de",
    "author_email": "c0fec0de@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/f9/44/2dd9c5d0c3befe899738b930aa056e003b1441bfbf34aab8fce90b2b7dea/anytree-2.12.1.tar.gz",
    "platform": null,
    "description": ".. image:: https://badge.fury.io/py/anytree.svg\n    :target: https://badge.fury.io/py/anytree\n\n.. image:: https://img.shields.io/pypi/dm/anytree.svg?label=pypi%20downloads\n   :target: https://pypi.python.org/pypi/anytree\n\n.. image:: https://readthedocs.org/projects/anytree/badge/?version=latest\n    :target: https://anytree.readthedocs.io/en/latest/?badge=latest\n\n.. image:: https://coveralls.io/repos/github/c0fec0de/anytree/badge.svg\n    :target: https://coveralls.io/github/c0fec0de/anytree\n\n.. image:: https://readthedocs.org/projects/anytree/badge/?version=stable\n    :target: https://anytree.readthedocs.io/en/stable\n\n.. image:: https://api.codeclimate.com/v1/badges/e6d325d6fd23a93aab20/maintainability\n   :target: https://codeclimate.com/github/c0fec0de/anytree/maintainability\n   :alt: Maintainability\n\n.. image:: https://img.shields.io/pypi/pyversions/anytree.svg\n   :target: https://pypi.python.org/pypi/anytree\n\n.. image:: https://img.shields.io/badge/code%20style-pep8-brightgreen.svg\n   :target: https://www.python.org/dev/peps/pep-0008/\n\n.. image:: https://img.shields.io/badge/code%20style-pep257-brightgreen.svg\n   :target: https://www.python.org/dev/peps/pep-0257/\n\n.. image:: https://img.shields.io/badge/linter-pylint-%231674b1?style=flat\n   :target: https://www.pylint.org/\n\n.. image:: https://img.shields.io/badge/code%20style-black-000000.svg\n   :target: https://github.com/psf/black\n\n.. image:: https://img.shields.io/github/contributors/c0fec0de/anytree.svg\n   :target: https://github.com/c0fec0de/anytree/graphs/contributors/\n\n.. image:: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square\n   :target: http://makeapullrequest.com\n\n.. image:: https://img.shields.io/github/issues-pr/c0fec0de/anytree.svg\n   :target: https://github.com/c0fec0de/anytree/pulls\n\n.. image:: https://img.shields.io/github/issues-pr-closed/c0fec0de/anytree.svg\n   :target: https://github.com/c0fec0de/anytree/pulls?q=is%3Apr+is%3Aclosed\n\n\nLinks\n=====\n\n* Documentation_\n* PyPI_\n* GitHub_\n* Changelog_\n* Issues_\n* Contributors_\n* If you enjoy anytree_\n\n.. image:: https://github.com/c0fec0de/anytree/raw/devel/docs/static/buymeacoffee.png\n   :target: https://www.buymeacoffee.com/1oYX0sw\n\n.. _anytree: https://anytree.readthedocs.io/en/stable/\n.. _Documentation: https://anytree.readthedocs.io/en/stable/\n.. _PyPI: https://pypi.org/project/anytree\n.. _GitHub: https://github.com/c0fec0de/anytree\n.. _Changelog: https://github.com/c0fec0de/anytree/releases\n.. _Issues: https://github.com/c0fec0de/anytree/issues\n.. _Contributors: https://github.com/c0fec0de/anytree/graphs/contributors\n\n.. _Node: https://anytree.readthedocs.io/en/stable/api/anytree.node.html#anytree.node.node.Node\n.. _RenderTree: https://anytree.readthedocs.io/en/stable/api/anytree.render.html#anytree.render.RenderTree\n.. _UniqueDotExporter: https://anytree.readthedocs.io/en/stable/exporter/dotexporter.html#anytree.exporter.dotexporter.UniqueDotExporter\n.. _NodeMixin: https://anytree.readthedocs.io/en/stable/api/anytree.node.html#anytree.node.nodemixin.NodeMixin\n.. _Importers: https://anytree.readthedocs.io/en/stable/importer.html\n.. _Exporters: https://anytree.readthedocs.io/en/stable/exporter.html\n\nGetting started\n---------------\n\n.. _getting_started:\n\nUsage is simple.\n\n**Construction**\n\n>>> from anytree import Node, RenderTree\n>>> udo = Node(\"Udo\")\n>>> marc = Node(\"Marc\", parent=udo)\n>>> lian = Node(\"Lian\", parent=marc)\n>>> dan = Node(\"Dan\", parent=udo)\n>>> jet = Node(\"Jet\", parent=dan)\n>>> jan = Node(\"Jan\", parent=dan)\n>>> joe = Node(\"Joe\", parent=dan)\n\n**Node**\n\n>>> print(udo)\nNode('/Udo')\n>>> print(joe)\nNode('/Udo/Dan/Joe')\n\n**Tree**\n\n>>> for pre, fill, node in RenderTree(udo):\n...     print(\"%s%s\" % (pre, node.name))\nUdo\n\u251c\u2500\u2500 Marc\n\u2502   \u2514\u2500\u2500 Lian\n\u2514\u2500\u2500 Dan\n    \u251c\u2500\u2500 Jet\n    \u251c\u2500\u2500 Jan\n    \u2514\u2500\u2500 Joe\n\nFor details see Node_ and RenderTree_.\n\n**Visualization**\n\n>>> from anytree.exporter import UniqueDotExporter\n>>> # graphviz needs to be installed for the next line!\n>>> UniqueDotExporter(udo).to_picture(\"udo.png\")\n\n.. image:: https://anytree.readthedocs.io/en/latest/_images/udo.png\n\nThe UniqueDotExporter_ can be started at any node and has various formatting hookups:\n\n>>> UniqueDotExporter(dan,\n...                   nodeattrfunc=lambda node: \"fixedsize=true, width=1, height=1, shape=diamond\",\n...                   edgeattrfunc=lambda parent, child: \"style=bold\"\n... ).to_picture(\"dan.png\")\n\n.. image:: https://anytree.readthedocs.io/en/latest/_images/dan.png\n\nThere are various other Importers_ and Exporters_.\n\n**Manipulation**\n\nA second tree:\n\n>>> mary = Node(\"Mary\")\n>>> urs = Node(\"Urs\", parent=mary)\n>>> chris = Node(\"Chris\", parent=mary)\n>>> marta = Node(\"Marta\", parent=mary)\n>>> print(RenderTree(mary))\nNode('/Mary')\n\u251c\u2500\u2500 Node('/Mary/Urs')\n\u251c\u2500\u2500 Node('/Mary/Chris')\n\u2514\u2500\u2500 Node('/Mary/Marta')\n\nAppend:\n\n>>> udo.parent = mary\n>>> print(RenderTree(mary))\nNode('/Mary')\n\u251c\u2500\u2500 Node('/Mary/Urs')\n\u251c\u2500\u2500 Node('/Mary/Chris')\n\u251c\u2500\u2500 Node('/Mary/Marta')\n\u2514\u2500\u2500 Node('/Mary/Udo')\n    \u251c\u2500\u2500 Node('/Mary/Udo/Marc')\n    \u2502   \u2514\u2500\u2500 Node('/Mary/Udo/Marc/Lian')\n    \u2514\u2500\u2500 Node('/Mary/Udo/Dan')\n        \u251c\u2500\u2500 Node('/Mary/Udo/Dan/Jet')\n        \u251c\u2500\u2500 Node('/Mary/Udo/Dan/Jan')\n        \u2514\u2500\u2500 Node('/Mary/Udo/Dan/Joe')\n\nSubtree rendering:\n\n>>> print(RenderTree(marc))\nNode('/Mary/Udo/Marc')\n\u2514\u2500\u2500 Node('/Mary/Udo/Marc/Lian')\n\nCut:\n\n>>> dan.parent = None\n>>> print(RenderTree(dan))\nNode('/Dan')\n\u251c\u2500\u2500 Node('/Dan/Jet')\n\u251c\u2500\u2500 Node('/Dan/Jan')\n\u2514\u2500\u2500 Node('/Dan/Joe')\n\n**Extending any python class to become a tree node**\n\nThe entire tree magic is encapsulated by NodeMixin_\nadd it as base class and the class becomes a tree node:\n\n>>> from anytree import NodeMixin, RenderTree\n>>> class MyBaseClass(object):  # Just an example of a base class\n...     foo = 4\n>>> class MyClass(MyBaseClass, NodeMixin):  # Add Node feature\n...     def __init__(self, name, length, width, parent=None, children=None):\n...         super(MyClass, self).__init__()\n...         self.name = name\n...         self.length = length\n...         self.width = width\n...         self.parent = parent\n...         if children:\n...             self.children = children\n\nJust set the `parent` attribute to reflect the tree relation:\n\n>>> my0 = MyClass('my0', 0, 0)\n>>> my1 = MyClass('my1', 1, 0, parent=my0)\n>>> my2 = MyClass('my2', 0, 2, parent=my0)\n\n>>> for pre, fill, node in RenderTree(my0):\n...     treestr = u\"%s%s\" % (pre, node.name)\n...     print(treestr.ljust(8), node.length, node.width)\nmy0      0 0\n\u251c\u2500\u2500 my1  1 0\n\u2514\u2500\u2500 my2  0 2\n\nThe `children` can be used likewise:\n\n>>> my0 = MyClass('my0', 0, 0, children=[\n...     MyClass('my1', 1, 0),\n...     MyClass('my2', 0, 2),\n... ])\n\n>>> for pre, fill, node in RenderTree(my0):\n...     treestr = u\"%s%s\" % (pre, node.name)\n...     print(treestr.ljust(8), node.length, node.width)\nmy0      0 0\n\u251c\u2500\u2500 my1  1 0\n\u2514\u2500\u2500 my2  0 2\n\nDocumentation\n-------------\n\nPlease see the Documentation_ for all details.\n\nInstallation\n------------\n\nTo install the `anytree` module run::\n\n    pip install anytree\n\nIf you do not have write-permissions to the python installation, try::\n\n    pip install anytree --user\n",
    "bugtrack_url": null,
    "license": "Apache-2.0",
    "summary": "Powerful and Lightweight Python Tree Data Structure with various plugins",
    "version": "2.12.1",
    "project_urls": null,
    "split_keywords": [
        "tree",
        "tree data",
        "treelib",
        "tree walk",
        "tree structure"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "6afbff946843e6b55ae9fda84df3964d6c233cd2261dface789f5be02ab79bc5",
                "md5": "dae89e11a91c5f9a93b65c096b8d27bd",
                "sha256": "5ea9e61caf96db1e5b3d0a914378d2cd83c269dfce1fb8242ce96589fa3382f0"
            },
            "downloads": -1,
            "filename": "anytree-2.12.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "dae89e11a91c5f9a93b65c096b8d27bd",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7.2,<4",
            "size": 44914,
            "upload_time": "2023-11-16T21:53:00",
            "upload_time_iso_8601": "2023-11-16T21:53:00.317855Z",
            "url": "https://files.pythonhosted.org/packages/6a/fb/ff946843e6b55ae9fda84df3964d6c233cd2261dface789f5be02ab79bc5/anytree-2.12.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f9442dd9c5d0c3befe899738b930aa056e003b1441bfbf34aab8fce90b2b7dea",
                "md5": "7838794202ed2b82711d81895323f367",
                "sha256": "244def434ccf31b668ed282954e5d315b4e066c4940b94aff4a7962d85947830"
            },
            "downloads": -1,
            "filename": "anytree-2.12.1.tar.gz",
            "has_sig": false,
            "md5_digest": "7838794202ed2b82711d81895323f367",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7.2,<4",
            "size": 31110,
            "upload_time": "2023-11-16T21:53:02",
            "upload_time_iso_8601": "2023-11-16T21:53:02.263628Z",
            "url": "https://files.pythonhosted.org/packages/f9/44/2dd9c5d0c3befe899738b930aa056e003b1441bfbf34aab8fce90b2b7dea/anytree-2.12.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-11-16 21:53:02",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "anytree"
}
        
Elapsed time: 0.14550s