Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support to print multiple data properties in Tree.show() #191

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@ treelib

Tree implementation in python: simple for you to use.

## Modifications

```sh
treelib/tree.py/Tree.show()
```

The original treelib only supports `data_property` argument as a `string`, and prints the corresponding attribute of the node data. This modification allows `data_property` argument to be a `list`, and prints the corresponding attributes in the list. For instance:

```python
sent_trees[5].show(data_property=["text", "dep_"])
```

------

[![Build Status](https://travis-ci.org/caesar0301/treelib.svg?branch=master)](https://travis-ci.org/caesar0301/treelib)
[![Documentation Status](https://readthedocs.org/projects/treelib/badge/?version=latest)](http://treelib.readthedocs.io/en/latest/?badge=latest)
[![Status](https://img.shields.io/pypi/status/treelib.svg)](https://pypi.python.org/pypi/treelib)
Expand Down
20 changes: 15 additions & 5 deletions treelib/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,21 @@ def __print_backend(self, nid=None, level=ROOT, idhidden=True, filter=None,
# Factory for proper get_label() function
if data_property:
if idhidden:
def get_label(node):
return getattr(node.data, data_property)
if type(data_property) is str:
def get_label(node):
return getattr(node.data, data_property)
elif type(data_property) is list:
def get_label(node):
properties = ", ".join(["%s: %s" % (d, getattr(node.data, d)) for d in data_property])
return properties
else:
def get_label(node):
return "%s[%s]" % (getattr(node.data, data_property), node.identifier)
if type(data_property) is str:
def get_label(node):
return "%s[%s]" % (getattr(node.data, data_property), node.identifier)
elif type(data_property) is list:
def get_label(node):
properties = ", ".join(["%s: %s" % (d, getattr(node.data, d)) for d in data_property])
return "%s[%s]" % (properties, node.identifier)
else:
if idhidden:
def get_label(node):
Expand Down Expand Up @@ -809,7 +819,7 @@ def show(self, nid=None, level=ROOT, idhidden=True, filter=None,
:param key: the ``key`` param for sorting :class:`Node` objects in the same level.
:param reverse: the ``reverse`` param for sorting :class:`Node` objects in the same level.
:param line_type:
:param data_property: the property on the node data object to be printed.
:param data_property: the property on the node data object to be printed. # modified: Now data_property can be either a str or a list of strs.
:return: None
"""
self._reader = ""
Expand Down