Skip to content

Commit

Permalink
Merge pull request #162 from kayjan/better-sphinx-examples
Browse files Browse the repository at this point in the history
Changed: Example for sphinx examples for merging trees and weighted t…
  • Loading branch information
kayjan authored Jan 16, 2024
2 parents a6fbf84 + 4660e02 commit 82865ba
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
Binary file modified assets/sphinx/weighted_tree.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 27 additions & 25 deletions docs/source/others/merging_trees.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,44 @@ Children of node `b` from both trees are retained as long as `merge_children=Tru
If only children of other tree is desired, set `overriding=True` instead.

```python
from bigtree import Node, print_tree, copy_nodes_from_tree_to_tree
from bigtree import Node, copy_nodes_from_tree_to_tree

# Construct trees
root1 = Node("a")
b1 = Node("b", parent=root1)
c1 = Node("c", parent=root1)
d1 = Node("d", parent=b1)
downloads_folder = Node("Downloads")
pictures_folder = Node("Pictures", parent=downloads_folder)
photo1 = Node("photo1.jpg", parent=pictures_folder)
file1 = Node("file1.doc", parent=downloads_folder)

b2 = Node("b")
e2 = Node("e", parent=b2)
documents_folder = Node("Documents")
pictures_folder = Node("Pictures", parent=documents_folder)
photo2 = Node("photo2.jpg", parent=pictures_folder)

# Validate tree structure
print_tree(root1)
# a
# ├── b
# │ └── d
# └── c
downloads_folder.show()
# Downloads
# ├── Pictures
# │ └── photo1.jpg
# └── file1.doc

print_tree(b2)
# b
# └── e
documents_folder.show()
# Documents
# └── Pictures
# └── photo2.jpg

# Merge trees
copy_nodes_from_tree_to_tree(
from_tree=b2,
to_tree=root1,
from_paths=["b"],
to_paths=["a/b"],
from_tree=documents_folder,
to_tree=downloads_folder,
from_paths=["Documents/Pictures"],
to_paths=["Downloads/Pictures"],
merge_children=True, # set overriding=True to override existing children
)

# Validate tree structure
print_tree(root1)
# a
# ├── b
# │ ├── d
# │ └── e
# └── c
downloads_folder.show()
# Downloads
# ├── Pictures
# │ ├── photo1.jpg
# │ └── photo2.jpg
# └── file1.doc
```
5 changes: 4 additions & 1 deletion docs/source/others/weighted_trees.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ class WeightedNode(Node):

@property
def edge_attr(self):
return {"label": self.weight}
"""Edge attribute for pydot diagram
Label for edge label, penwidth for edge width
"""
return {"label": self.weight, "penwidth": self.weight}

# Construct weighted tree
root = WeightedNode("a")
Expand Down

0 comments on commit 82865ba

Please sign in to comment.