From 0391d056604458068132313730b9d36be055087b Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Mon, 15 Apr 2024 06:58:50 +0200 Subject: Adding upstream version 1.7.0. Signed-off-by: Daniel Baumann --- treelib/plugins.py | 35 ----------------------------------- treelib/tree.py | 41 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 36 deletions(-) delete mode 100644 treelib/plugins.py (limited to 'treelib') 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 -- cgit v1.2.3