summaryrefslogtreecommitdiffstats
path: root/treelib
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-15 04:58:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-15 04:58:55 +0000
commit1c6e96e7ddaca0fd0f8295ac5531c161d14501cb (patch)
tree9843972f964a3f3e91236f282748d49b02dbab0e /treelib
parentReleasing debian version 1.6.4-3. (diff)
downloadtreelib-1c6e96e7ddaca0fd0f8295ac5531c161d14501cb.tar.xz
treelib-1c6e96e7ddaca0fd0f8295ac5531c161d14501cb.zip
Merging upstream version 1.7.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'treelib')
-rw-r--r--treelib/plugins.py35
-rw-r--r--treelib/tree.py41
2 files changed, 40 insertions, 36 deletions
diff --git a/treelib/plugins.py b/treelib/plugins.py
deleted file mode 100644
index 0736c73..0000000
--- a/treelib/plugins.py
+++ /dev/null
@@ -1,35 +0,0 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-# Copyright (C) 2011
-# Brett Alistair Kromkamp - brettkromkamp@gmail.com
-# Copyright (C) 2012-2017
-# Xiaming Chen - chenxm35@gmail.com
-# and other contributors.
-# All rights reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-"""
-This is a public location to maintain contributed
-utilities to extend the basic Tree class.
-
-Deprecated! We prefer a unified processing of Tree object.
-"""
-from __future__ import unicode_literals
-
-from .misc import deprecated
-
-
-@deprecated(alias="tree.to_graphviz()")
-def export_to_dot(tree, filename=None, shape="circle", graph="digraph"):
- """Exports the tree in the dot format of the graphviz software"""
- tree.to_graphviz(filename=filename, shape=shape, graph=graph)
diff --git a/treelib/tree.py b/treelib/tree.py
index 7d92297..1cc9ac8 100644
--- a/treelib/tree.py
+++ b/treelib/tree.py
@@ -929,7 +929,7 @@ class Tree(object):
print("Tree is empty")
if stdout:
- print(self._reader)
+ print(self._reader.encode("utf-8"))
else:
return self._reader
@@ -1128,3 +1128,42 @@ class Tree(object):
print(f.getvalue())
f.close()
+
+ @classmethod
+ def from_map(cls, child_parent_dict, id_func=None, data_func=None):
+ """
+ takes a dict with child:parent, and form a tree
+ """
+ tree = Tree()
+ if tree is None or tree.size() > 0:
+ raise ValueError("need to pass in an empty tree")
+ id_func = id_func if id_func else lambda x: x
+ data_func = data_func if data_func else lambda x: None
+ parent_child_dict = {}
+ root_node = None
+ for k, v in child_parent_dict.items():
+ if v is None and root_node is None:
+ root_node = k
+ elif v is None and root_node is not None:
+ raise ValueError("invalid input, more than 1 child has no parent")
+ else:
+ if v in parent_child_dict:
+ parent_child_dict[v].append(k)
+ else:
+ parent_child_dict[v] = [k]
+ if root_node is None:
+ raise ValueError("cannot find root")
+
+ tree.create_node(root_node, id_func(root_node), data=data_func(root_node))
+ queue = [root_node]
+ while len(queue) > 0:
+ parent_node = queue.pop()
+ for child in parent_child_dict.get(parent_node, []):
+ tree.create_node(
+ child,
+ id_func(child),
+ parent=id_func(parent_node),
+ data=data_func(child),
+ )
+ queue.append(child)
+ return tree