@@ -34,6 +34,7 @@ def print_tree(
34
34
max_depth : int = None ,
35
35
all_attrs : bool = False ,
36
36
attr_list : List [str ] = None ,
37
+ attr_omit_null : bool = True ,
37
38
attr_bracket_open : str = "[" ,
38
39
attr_bracket_close : str = "]" ,
39
40
style : str = "ansi" ,
@@ -46,6 +47,7 @@ def print_tree(
46
47
- Able to select which node to print from, resulting in a subtree, using `node_name`
47
48
- Able to customize for maximum depth to print, using `max_depth`
48
49
- Able to choose which attributes to show or show all attributes, using `attr_name_filter` and `all_attrs`
50
+ - Able to omit showing of attributes if it is null, using `attr_omit_null`
49
51
- Able to customize open and close brackets if attributes are shown
50
52
- Able to customize style, to choose from `ansi`, `ascii`, `const`, `rounded`, `double`, and `custom` style
51
53
- Default style is `ansi` style
@@ -145,6 +147,7 @@ def print_tree(
145
147
max_depth (int): maximum depth of tree to print, based on `depth` attribute, optional
146
148
all_attrs (bool): indicator to show all attributes, overrides `attr_list`
147
149
attr_list (list): list of node attributes to print, optional
150
+ attr_omit_null (bool): indicator whether to omit showing of null attributes, defaults to True
148
151
attr_bracket_open (str): open bracket for `attr_list`
149
152
attr_bracket_close (str): close bracket for `attr_list`
150
153
style (str): style of print, defaults to abstract style
@@ -169,10 +172,23 @@ def print_tree(
169
172
attr_str = ", " .join ([f"{ k } ={ v } " for k , v in attrs ])
170
173
attr_str = f" { attr_bracket_open } { attr_str } { attr_bracket_close } "
171
174
elif attr_list :
172
- attr_str = ", " .join (
173
- [f"{ attr_name } ={ _node .get_attr (attr_name )} " for attr_name in attr_list ]
174
- )
175
- attr_str = f" { attr_bracket_open } { attr_str } { attr_bracket_close } "
175
+ if attr_omit_null :
176
+ attr_str = ", " .join (
177
+ [
178
+ f"{ attr_name } ={ _node .get_attr (attr_name )} "
179
+ for attr_name in attr_list
180
+ if _node .get_attr (attr_name )
181
+ ]
182
+ )
183
+ else :
184
+ attr_str = ", " .join (
185
+ [
186
+ f"{ attr_name } ={ _node .get_attr (attr_name )} "
187
+ for attr_name in attr_list
188
+ ]
189
+ )
190
+ if attr_str :
191
+ attr_str = f" { attr_bracket_open } { attr_str } { attr_bracket_close } "
176
192
node_str = f"{ _node .node_name } { attr_str } "
177
193
print (f"{ pre_str } { fill_str } { node_str } " )
178
194
0 commit comments