@@ -25,6 +25,8 @@ def print_tree(
2525 max_depth : int = 0 ,
2626 all_attrs : bool = False ,
2727 attr_list : Iterable [str ] | None = None ,
28+ attr_format : str = "{k}={v}" ,
29+ attr_sep : str = ", " ,
2830 attr_omit_null : bool = False ,
2931 attr_bracket : Collection [str ] = ("[" , "]" ),
3032 style : str | Iterable [str ] | constants .BasePrintStyle = "const" ,
@@ -36,6 +38,7 @@ def print_tree(
3638 - Able to select which node to print from, resulting in a subtree, using `node_name_or_path`
3739 - Able to customise for maximum depth to print, using `max_depth`
3840 - Able to choose which attributes to show or show all attributes, using `all_attrs` and `attr_list`
41+ - For showing attributes, able to customise the format of attributes and separator of attributes
3942 - Able to omit showing of attributes if it is null, using `attr_omit_null`
4043 - Able to customise open and close brackets if attributes are shown, using `attr_bracket`
4144 - Able to customise style, to choose from str, list[str], or inherit from constants.BasePrintStyle, using `style`
@@ -93,6 +96,13 @@ def print_tree(
9396 │ └── e [age=35]
9497 └── c [age=60]
9598
99+ >>> print_tree(root, attr_list=["name", "age"], attr_format="{k}:{v}", attr_sep="; ")
100+ a [name:a; age:90]
101+ ├── b [name:b; age:65]
102+ │ ├── d [name:d; age:40]
103+ │ └── e [name:e; age:35]
104+ └── c [name:c; age:60]
105+
96106 >>> print_tree(root, attr_list=["age"], attr_bracket=["*(", ")"])
97107 a *(age=90)
98108 ├── b *(age=65)
@@ -175,6 +185,9 @@ def print_tree(
175185 max_depth: maximum depth of tree to print, based on `depth` attribute
176186 all_attrs: indicator to show all attributes, overrides `attr_list` and `attr_omit_null`
177187 attr_list: node attributes to print
188+ attr_format: if attributes are displayed, the format in which to display, uses k,v to correspond to
189+ attribute name and attribute value
190+ attr_list_sep: if attributes are displayed, the separator of attributes, defaults to comma
178191 attr_omit_null: indicator whether to omit showing of null attributes
179192 attr_bracket: open and close bracket for `all_attrs` or `attr_list`
180193 style: style of print
@@ -195,21 +208,25 @@ def print_tree(
195208 attr_bracket_open , attr_bracket_close = attr_bracket
196209 if all_attrs :
197210 attrs = _node .describe (exclude_attributes = ["name" ], exclude_prefix = "_" )
198- attr_str_list = [f" { k } = { v } " for k , v in attrs ]
211+ attr_str_list = [attr_format . format ( k = k , v = v ) for k , v in attrs ]
199212 else :
200213 if attr_omit_null :
201214 attr_str_list = [
202- f"{ attr_name } ={ _node .get_attr (attr_name )} "
215+ attr_format .replace ("{k}" , attr_name ).replace (
216+ "{v}" , str (_node .get_attr (attr_name ))
217+ )
203218 for attr_name in attr_list
204219 if not common .isnull (_node .get_attr (attr_name ))
205220 ]
206221 else :
207222 attr_str_list = [
208- f"{ attr_name } ={ _node .get_attr (attr_name )} "
223+ attr_format .replace ("{k}" , attr_name ).replace (
224+ "{v}" , str (_node .get_attr (attr_name ))
225+ )
209226 for attr_name in attr_list
210227 if hasattr (_node , attr_name )
211228 ]
212- attr_str = ", " .join (attr_str_list )
229+ attr_str = attr_sep .join (attr_str_list )
213230 if attr_str :
214231 attr_str = f" { attr_bracket_open } { attr_str } { attr_bracket_close } "
215232 name_str = _node .get_attr (alias ) or _node .node_name
0 commit comments