caesar0301 / treelib

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

provide node data in dictionary (and json) #35

Closed jcwright77 closed 9 years ago

jcwright77 commented 9 years ago

Hi, I've recently been using treelib/pytree in a larger project. We mainly use the .json() functionality to return subtrees that are parsable into menus. Recently one developer wanted to also access other node data in the menu data structure. I noticed that the .data attribute is dropped in dictionary construction. I method patched a class instance to provide this. I'm wondering if you would consider adding it to the code.

def _to_dict(self, nid=None, key=None, reverse=False):
    """transform self into a dict"""

    nid = self.root if (nid is None) else nid
    #print('adding',nid,self[nid].data)
    tree_dict = {self[nid].tag: { "children":[] , "data":self[nid].data } }

    if self[nid].expanded:
        queue = [self[i] for i in self[nid].fpointer]
        key = (lambda x: x) if (key is None) else key
        queue.sort(key=key, reverse=reverse)

        for elem in queue:
            tree_dict[self[nid].tag]["children"].append(
                self.to_dict(elem.identifier))
        if tree_dict[self[nid].tag]["children"] == []:
            tree_dict = {self[nid].tag: { "data":self[nid].data } }
        return tree_dict
Natim commented 9 years ago

Please send a PR and add a test for it. Le 1 août 2014 20:52, "jcwright77" notifications@github.com a écrit :

Hi, I've recently been using treelib/pytree in a larger project. We mainly use the .json() functionality to return subtrees that are parsable into menus. Recently one developer wanted to also access other node data in the menu data structure. I noticed that the .data attribute is dropped in dictionary construction. I method patched a class instance to provide this. I'm wondering if you would consider adding it to the code.

def _to_dict(self, nid=None, key=None, reverse=False): """transform self into a dict"""

nid = self.root if (nid is None) else nid
#print('adding',nid,self[nid].data)
tree_dict = {self[nid].tag: { "children":[] , "data":self[nid].data } }

if self[nid].expanded:
    queue = [self[i] for i in self[nid].fpointer]
    key = (lambda x: x) if (key is None) else key
    queue.sort(key=key, reverse=reverse)

    for elem in queue:
        tree_dict[self[nid].tag]["children"].append(
            self.to_dict(elem.identifier))
    if tree_dict[self[nid].tag]["children"] == []:
        tree_dict = {self[nid].tag: { "data":self[nid].data } }
    return tree_dict

— Reply to this email directly or view it on GitHub https://github.com/caesar0301/pyTree/issues/35.

caesar0301 commented 9 years ago

@jcwright77 I add your patch triggered through parameter with_data of to_json().

You can refer usage to Example 10.

jcwright77 commented 9 years ago

thanks that was fast, thanks. On Aug 4, 2014, at 5:53 AM, X. Chen wrote:

@jcwright77 I add your patch triggered through parameter with_data of to_json().

You can refer usage to Example 10.

— Reply to this email directly or view it on GitHub.