From d87c848599f370986996a823244829d655382ae7 Mon Sep 17 00:00:00 2001 From: William Rusnack Date: Fri, 11 Nov 2016 10:34:58 -0500 Subject: [PATCH 1/5] tree.node is a @property method and does not need a function call () at the end. Fixed this in the documentations --- docs/source/pyapi.rst | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/docs/source/pyapi.rst b/docs/source/pyapi.rst index 5f6c83d..03de410 100644 --- a/docs/source/pyapi.rst +++ b/docs/source/pyapi.rst @@ -151,6 +151,10 @@ Instance attributes: with ``.`` and ``=`` operator respectively. +.. method:: nodes + + Return a dict form of nodes in a tree: {id: node_instance} + **Instance methods**: @@ -243,11 +247,6 @@ Instance attributes: Move node (source) from its parent to another parent (destination). -.. method:: nodes() - - Return a dict form of nodes in a tree: {id: node_instance} - - .. method:: parent (nid) Obtain specific node's parent (Node instance). Return None if the parent is From 449cb9dab1e496f364382f10b1e216a5cee6f986 Mon Sep 17 00:00:00 2001 From: William Rusnack Date: Fri, 11 Nov 2016 11:06:34 -0500 Subject: [PATCH 2/5] Solved Exception being thrown when tree.show() is called on a empyty tree --- tests/test_treelib.py | 27 +++++++++++++++++++++------ treelib/tree.py | 7 +++++-- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/tests/test_treelib.py b/tests/test_treelib.py index 28352b4..aaf8219 100644 --- a/tests/test_treelib.py +++ b/tests/test_treelib.py @@ -286,11 +286,20 @@ def __init__(self, color): def test_show_data_property(self): new_tree = Tree() - class Flower(object): - def __init__(self, color): - self.color = color - new_tree.create_node("Jill", "jill", data=Flower("white")) - new_tree.show(data_property="color") + + sys.stdout = open(os.devnull, "w") # stops from printing to console + + try: + new_tree.show() + + class Flower(object): + def __init__(self, color): + self.color = color + new_tree.create_node("Jill", "jill", data=Flower("white")) + new_tree.show(data_property="color") + finally: + sys.stdout.close() + sys.stdout = sys.__stdout__ # stops from printing to console def test_level(self): self.assertEqual(self.tree.level('hárry'), 0) @@ -312,7 +321,13 @@ def test_print_backend(self): assert str(self.tree) == encode(expected_result) def test_show(self): - self.tree.show() + sys.stdout = open(os.devnull, "w") # stops from printing to console + + try: + self.tree.show() + finally: + sys.stdout.close() + sys.stdout = sys.__stdout__ # stops from printing to console def tearDown(self): self.tree = None diff --git a/treelib/tree.py b/treelib/tree.py index 5c1b46e..2d8f2e2 100644 --- a/treelib/tree.py +++ b/treelib/tree.py @@ -644,8 +644,11 @@ def show(self, nid=None, level=ROOT, idhidden=True, filter=None, def write(line): self.reader += line.decode('utf-8') + "\n" - self.__print_backend(nid, level, idhidden, filter, - key, reverse, line_type, data_property, func=write) + try: + self.__print_backend(nid, level, idhidden, filter, + key, reverse, line_type, data_property, func=write) + except NodeIDAbsentError: + print('Tree is empty') print(self.reader) From d8418c6af56d4396fb4f1cddc2a9aa92feb9078b Mon Sep 17 00:00:00 2001 From: William Rusnack Date: Fri, 11 Nov 2016 14:34:48 -0500 Subject: [PATCH 3/5] Set default encoding --- tests/test_treelib.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_treelib.py b/tests/test_treelib.py index aaf8219..e305346 100644 --- a/tests/test_treelib.py +++ b/tests/test_treelib.py @@ -321,7 +321,9 @@ def test_print_backend(self): assert str(self.tree) == encode(expected_result) def test_show(self): + reload(sys) sys.stdout = open(os.devnull, "w") # stops from printing to console + sys.setdefaultencoding('utf-8') try: self.tree.show() From 0087191ae1a7b984864930d7e43958bda1f59ea4 Mon Sep 17 00:00:00 2001 From: William Rusnack Date: Fri, 11 Nov 2016 14:41:04 -0500 Subject: [PATCH 4/5] Now passes with 2 and 3 --- tests/test_treelib.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/test_treelib.py b/tests/test_treelib.py index e305346..04b7967 100644 --- a/tests/test_treelib.py +++ b/tests/test_treelib.py @@ -321,9 +321,10 @@ def test_print_backend(self): assert str(self.tree) == encode(expected_result) def test_show(self): - reload(sys) + if sys.version_info.major < 3: + reload(sys) + sys.setdefaultencoding('utf-8') sys.stdout = open(os.devnull, "w") # stops from printing to console - sys.setdefaultencoding('utf-8') try: self.tree.show() From 7de712b8dd06387c936175238c64338b9d8c4dde Mon Sep 17 00:00:00 2001 From: William Rusnack Date: Sun, 13 Nov 2016 10:46:24 -0500 Subject: [PATCH 5/5] Fixed tests failing on version 2.6 --- tests/test_treelib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_treelib.py b/tests/test_treelib.py index 04b7967..7d42b27 100644 --- a/tests/test_treelib.py +++ b/tests/test_treelib.py @@ -321,7 +321,7 @@ def test_print_backend(self): assert str(self.tree) == encode(expected_result) def test_show(self): - if sys.version_info.major < 3: + if sys.version_info[0] < 3: reload(sys) sys.setdefaultencoding('utf-8') sys.stdout = open(os.devnull, "w") # stops from printing to console