Skip to content

Commit

Permalink
docs: more tips and tricks for custom classes
Browse files Browse the repository at this point in the history
  • Loading branch information
kayjan committed Nov 4, 2024
1 parent 4e85c7a commit 4e124ef
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
37 changes: 37 additions & 0 deletions docs/others/work_with_classes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Working with Classes

Custom classes can be assigned to Node as `data` attribute, or any other attribute name.

```python
from bigtree import Node


class Folder:
def __init__(self, name: str):
self.name = name

def __str__(self):
return f"Folder<{self.name}>"

class File:
def __init__(self, name: str):
self.name = name

def __str__(self):
return f"File<{self.name}>"

folder_documents = Node("My Documents", data=Folder("Documents"))
file_photo1 = Node("photo1.jpg", data=File("photo.jpg"))
file_photo2 = Node("photo2.jpg", data=File("photo.jpg"))
folder_documents.children = [file_photo1, file_photo2]

folder_documents.show()
# My Documents
# ├── photo1.jpg
# └── photo2.jpg

folder_documents.show(alias="data")
# Folder<Documents>
# ├── File<photo.jpg>
# └── File<photo.jpg>
```
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ nav:
- others/nodes.md
- others/merging_trees.md
- others/weighted_trees.md
- others/work_with_classes.md

theme:
name: material
Expand Down

0 comments on commit 4e124ef

Please sign in to comment.