Skip to content

Commit

Permalink
Merge pull request #66 from BebeSparkelSparkel/master
Browse files Browse the repository at this point in the history
Fixed Documentation and tree.show()
  • Loading branch information
Xiaming Chen committed Nov 21, 2016
2 parents bd53bbd + 7de712b commit b5374cd
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
9 changes: 4 additions & 5 deletions docs/source/pyapi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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**:

Expand Down Expand Up @@ -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
Expand Down
30 changes: 24 additions & 6 deletions tests/test_treelib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -312,7 +321,16 @@ def test_print_backend(self):
assert str(self.tree) == encode(expected_result)

def test_show(self):
self.tree.show()
if sys.version_info[0] < 3:
reload(sys)
sys.setdefaultencoding('utf-8')
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
Expand Down
7 changes: 5 additions & 2 deletions treelib/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit b5374cd

Please sign in to comment.