caesar0301 / treelib

An efficient implementation of tree data structure in python 2/3.
http://treelib.readthedocs.io/en/latest/
Other
801 stars 185 forks source link

tree.show prints a byte literal #82

Closed chasezheng closed 6 years ago

chasezheng commented 6 years ago

for example,

>>> from treelib import Node, Tree
>>> tree = Tree()
>>> tree.create_node("Harry", "harry")  # root node
>>> tree.create_node("Jane", "jane", parent="harry")
>>> tree.create_node("Bill", "bill", parent="harry")
>>> tree.create_node("Diane", "diane", parent="jane")
>>> tree.create_node("Mary", "mary", parent="diane")
>>> tree.create_node("Mark", "mark", parent="jane")
>>> tree.show()
b'Harry\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 Bill\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 Jane\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 Diane\n    \xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 Mary\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 Mark\n'

To fix this, replace this line https://github.com/caesar0301/treelib/blob/1200ec1d7a1c5cda4508dddbfcc0bc13545ff575/treelib/tree.py#L679 with: print(self.reader)

LeBarbouze commented 6 years ago

Hi, @chasezheng , I agree with your fix: I think the print() statement deals with encoding (well, the call to sys.stdout.write() does, it seems), so there is no need to encode before.

Maybe you could submit a PR with your fix?

Edit: I just saw the __str__ method is an alternative solution (following your example):

>>> print(tree)
Harry
├── Bill
└── Jane
    ├── Diane
    │   └── Mary
    └── Mark
alazyworkaholic commented 6 years ago

This issue drove me nuts for a couple hours until I found out it wasn't me or my system's fault. +1 for a fix please! This library is just what I needed otherwise.

caesar0301 commented 6 years ago

Thanks guys!! :+1: