Comments (10)
There are 3 types of information that should be stored to serialize/deserialize a tree
instance: tree
information, node
information, nodes
hierarchy.
More specifically:
tree
identifier
node
"hierarchy" (nodesbpointer
/fpointers
)node
base attributes:tag
,identifier
node
data
(requires contraints since some objects aren't serializable: eg pythonset
for json serialization)
Then in case of inheritance:tree
node_class
in case of node class inheriting fromtreelib.Node
tree
other attributes in case of tree class inheriting fromtreelib.Tree
node
other attributes in case of node class inheriting fromtreelib.Node
Without going into the details of a specific output format, an approach allowing inheritance could be to have distinct methods that can be overriden:
treelib.Tree
_serialize_metadata
method, serializingtree
information (identifier, tree other attributes in case of inheritance)treelib.Tree
_serialize_hierarchy
method, serializing hierarchy (extracted from bpointer/fpointers)treelib.Node
_serialize_node
method, serializing node information (tag
,identifier
,data
etc)
Note: for those not requiring a specific serialization format, consider using python pickle
module: https://docs.python.org/3/library/pickle.html
from treelib.
Did anything come of this? Is there a way to population a tree in treelib with a JSON? Thanks.
from treelib.
Would a general interface not just mean a couple of:
to_dict()
,to_json()
,to_graphviz()
, ... instance methods.- along with
from_dict()
,from_json()
,from_graphviz()
, ... classmethods.
Most of these methods exist already, so you'd just have to name them properly. Furthermore they should have the same signature.
Or do you think about putting these functions into new modules? For example:
treelib.save.to_dict(tree, ...)
,treelib.save.to_json(tree, ...)
,treelib.save.to_graphviz(tree, ...)
treelib.load.from_dict(dict_)
,treelib.load.from_json(json_file)
,treelib.load.from_graphviz(dot_file)
I could work on that, if you need help.
from treelib.
I'll drop another exporting function here, just in case somebody wants to do the same. I wanted to convert a tree into binary tree, using the left-child-right-sibling method. As treelib can't distinguish between left and right childs I used the binarytree package.
import binarytree as bt
def to_left_child_right_sibling(tree: tl.Tree) -> Tuple[bt.Node, Dict[int, str]]:
""" Converts a treelib.Tree object to a binarytree.
The binarytree package is used for storing the new LCRS-binary tree, as
Treelib trees can't distinguish between left and right children. The
binarytree.Node class expects numeric node values (identifiers), the
tags/labels/names of the nodes are returned in a dictionary.
"""
def to_lcrs(tree: tl.Tree, root_id: int = None) -> bt.Node:
"""Recursivly constructs a lcrs tree starting from node at root_id"""
if root_id is None:
root_id = tree.root
# construct a root node
root = bt.Node(root_id)
# if it does not have any children, we return it (recursion end)
if not tree.children(root_id):
return root
# otherwise we recursivly construct lcrs trees of every child ...
sub_trees = [to_lcrs(tree, child_id) for child_id in tree[root_id].fpointer]
# ... and link them together as right childs
for i in range(1, len(sub_trees)):
sub_trees[i - 1].right = sub_trees[i]
# the first lcrs tree is now the left child of our root
root.left = sub_trees[0] if len(sub_trees) > 0 else None
return root
id2name = {i: node.tag for i, node in tree.nodes.items()}
root = to_lcrs(tree)
return root, id2name
from treelib.
are there function to load json data to tree yet ?
from treelib.
I think it would be appropriate to implement right away #95 (ability to export to stream) into the solution of this issue.
@villmow are you still interested into working on that subject or do you need help?
from treelib.
I didn't know the graphviz
dot
format, but from what I understand I think we shouldn't try to handle this in the same way that yaml
json
formats, since it is much less generic.
For json
/yaml
and such, we could have some kind of common _export
method, whose goal would be to provide a serializable python object, and then apply either a JSON or YAML serializer.
from treelib.
@caesar0301 before I go further and implement the json/yaml serialization with stream output, do you have an opinion on this design: #133
from treelib.
Hello, has from_json
or anything similar been implemented yet? since this request is still open I assume no?
from treelib.
https://anytree.readthedocs.io/en/latest/index.html
from treelib.
Related Issues (20)
- Create nodes with children instead of the parent HOT 2
- Create Tree object from Json HOT 3
- Cannot understand how to use 'successors' HOT 1
- Any method to add weight to edges? HOT 1
- how to replace a node with a new tree HOT 2
- how to insert a node in a tree? HOT 2
- Possible memory leak due to deepcopy ?
- Modifying a node's identifier with the . and = operator breaks the tree
- Update python version support! HOT 2
- Get truncated tree at a given depth
- Finding the nodes between two nodes HOT 1
- Problem exporting to JSON and dict
- potential bug for data parameter in Node
- Give treelib a performance improvement and persistence using leveldb/rocksdb HOT 1
- Treelib not working any longer on Google colab or https://jupyter.org/ HOT 3
- Render tree expanding only ancestors of given node
- tree.show prints binary literal to stdout HOT 3
- [Win] pip install treelib not working | Unable to download treelib library
- get error when do Huffman Coding
- change the keys in _nodes
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from treelib.