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

Added print kwargs to print options. #286

Merged
merged 1 commit into from
Aug 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions bigtree/tree/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def print_tree(
attr_omit_null: bool = False,
attr_bracket: List[str] = ["[", "]"],
style: Union[str, Iterable[str], BasePrintStyle] = "const",
**print_kwargs,
) -> None:
"""Print tree to console, starting from `tree`.

Expand All @@ -90,6 +91,8 @@ def print_tree(
- (BasePrintStyle): `ANSIPrintStyle`, `ASCIIPrintStyle`, `ConstPrintStyle`, `ConstBoldPrintStyle`, `RoundedPrintStyle`,
`DoublePrintStyle` style or inherit from `BasePrintStyle`

Remaining kwargs are passed without modification to python's `print` function.

Examples:
**Printing tree**

Expand Down Expand Up @@ -188,6 +191,19 @@ def print_tree(
| `-- e
`-- c

**Printing to a file**

>>> import io
>>> output = io.StringIO()
>>> print_tree(root, file=output)
>>> string = output.getvalue()
>>> print(string)
a
├── b
│ ├── d
│ └── e
└── c

Args:
tree (Node): tree to print
node_name_or_path (str): node to print from, becomes the root node of printing
Expand Down Expand Up @@ -232,7 +248,7 @@ def print_tree(
if attr_str:
attr_str = f" {attr_bracket_open}{attr_str}{attr_bracket_close}"
node_str = f"{_node.node_name}{attr_str}"
print(f"{pre_str}{fill_str}{node_str}")
print(f"{pre_str}{fill_str}{node_str}", **print_kwargs)


def yield_tree(
Expand Down Expand Up @@ -416,6 +432,7 @@ def hprint_tree(
max_depth: int = 0,
intermediate_node_name: bool = True,
style: Union[str, Iterable[str], BaseHPrintStyle] = "const",
**print_kwargs,
) -> None:
"""Print tree in horizontal orientation to console, starting from `tree`.

Expand All @@ -430,6 +447,8 @@ def hprint_tree(
- (BaseHPrintStyle): `ANSIHPrintStyle`, `ASCIIHPrintStyle`, `ConstHPrintStyle`, `ConstBoldHPrintStyle`,
`RoundedHPrintStyle`, `DoubleHPrintStyle` style or inherit from BaseHPrintStyle

Remaining kwargs are passed without modification to python's `print` function.

Examples:
**Printing tree**

Expand Down Expand Up @@ -504,6 +523,17 @@ def hprint_tree(
- a -+ \\- e
\\- c

**Printing to a file**
>>> import io
>>> output = io.StringIO()
>>> hprint_tree(root, file=output)
>>> string = output.getvalue()
>>> print(string)
┌─ d
┌─ b ─┤
─ a ─┤ └─ e
└─ c

Args:
tree (Node): tree to print
node_name_or_path (str): node to print from, becomes the root node of printing
Expand All @@ -518,7 +548,7 @@ def hprint_tree(
max_depth=max_depth,
style=style,
)
print("\n".join(result))
print("\n".join(result), **print_kwargs)


def hyield_tree(
Expand Down
Loading