Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

Package to draw tree structures (tree linux command style)

Project description

lxtree

PyPI version PyPI version

Package to draw tree structures (tree linux command style)


Basic usage

Build the tree structure using TreeNodes. A TreeNode can contain other TreeNodes to represent the tree. When builded you car get a string representation of it, or get string of a sub-tree using some child as root.

Code samples

from lxtree import TreeNode


# Creating root
root = TreeNode('root')

# Adding a list of nodes
root.children = [TreeNode('branch1'), TreeNode('branch3')]

# Insert node
root.insert_child(TreeNode('branch2'), 1)

# Appending to a branch 1 by index
root[0].append_child(TreeNode('branch11'))

# Using index to assign children to branch11
root[0][0] = [TreeNode('leaf111'), TreeNode('leaf112')]

# Use `set_children` to add nodes as argv
root[1].set_children(TreeNode('leaf21'), TreeNode('leaf22'), TreeNode('leaf23'))

# Print tree
print(root)
Creating all structure at once
root = TreeNode('root').set_children(
  TreeNode('branch1').append_child(
      TreeNode('branch11').set_children(
          TreeNode('leaf111'),
          TreeNode('leaf112')
      )
  ),
  TreeNode('branch2').set_children(
      TreeNode('leaf21'),
      TreeNode('leaf22'),
      TreeNode('leaf23')
  ),
  TreeNode('branch3')
)

print(root)
Creating the tree from a dict
tree_data = {
  'root': {
      'branch1': {
        'branch11': {
          'leaf111': None,
          'leaf112': None
        }
      },
      'branch2': {
        'leaf21': None,
        'leaf22': None,
        'leaf23': None
      },
      'branch3': None
  }
}

print(TreeNode.tree_from_dict(tree_data))

Output

tree_output

Project details


Release history Release notifications

Download files

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

Files for lxtree, version 0.1.0
Filename, size File type Python version Upload date Hashes
Filename, size lxtree-0.1.0-py3-none-any.whl (4.6 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size lxtree-0.1.0.tar.gz (4.3 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page