@@ -55,7 +55,7 @@ def children(self):
55
55
"""
56
56
pass
57
57
58
- def show (self , buf = sys .stdout , offset = 0 , attrnames = False , nodenames = False , showcoord = False , _my_node_name = None ):
58
+ def show (self , buf = sys .stdout , offset = 0 , attrnames = False , showemptyattrs = True , nodenames = False , showcoord = False , _my_node_name = None ):
59
59
""" Pretty print the Node and all its attributes and
60
60
children (recursively) to a buffer.
61
61
@@ -69,6 +69,9 @@ def show(self, buf=sys.stdout, offset=0, attrnames=False, nodenames=False, showc
69
69
True if you want to see the attribute names in
70
70
name=value pairs. False to only see the values.
71
71
72
+ showemptyattrs:
73
+ False if you want to suppress printing empty attributes.
74
+
72
75
nodenames:
73
76
True if you want to see the actual node names
74
77
within their parents.
@@ -84,12 +87,13 @@ def show(self, buf=sys.stdout, offset=0, attrnames=False, nodenames=False, showc
84
87
buf .write (lead + self .__class__ .__name__ + ': ' )
85
88
86
89
if self .attr_names :
90
+ is_empty = lambda v : v is None or (hasattr (v , '__len__' ) and len (v ) == 0 )
91
+ nvlist = [(n , getattr (self ,n )) for n in self .attr_names \
92
+ if showemptyattrs or not is_empty (getattr (self ,n ))]
87
93
if attrnames :
88
- nvlist = [(n , getattr (self ,n )) for n in self .attr_names ]
89
94
attrstr = ', ' .join ('%s=%s' % nv for nv in nvlist )
90
95
else :
91
- vlist = [getattr (self , n ) for n in self .attr_names ]
92
- attrstr = ', ' .join ('%s' % v for v in vlist )
96
+ attrstr = ', ' .join ('%s' % v for (_ ,v ) in nvlist )
93
97
buf .write (attrstr )
94
98
95
99
if showcoord :
@@ -101,6 +105,7 @@ def show(self, buf=sys.stdout, offset=0, attrnames=False, nodenames=False, showc
101
105
buf ,
102
106
offset = offset + 2 ,
103
107
attrnames = attrnames ,
108
+ showemptyattrs = showemptyattrs ,
104
109
nodenames = nodenames ,
105
110
showcoord = showcoord ,
106
111
_my_node_name = child_name )
0 commit comments