@@ -224,7 +224,7 @@ def children(self):
224224 """
225225 pass
226226
227- def show(self, buf=sys.stdout, offset=0, attrnames=False, nodenames=False, showcoord=False, _my_node_name=None):
227+ def show(self, buf=sys.stdout, offset=0, attrnames=False, showemptyattrs=True, nodenames=False, showcoord=False, _my_node_name=None):
228228 """ Pretty print the Node and all its attributes and
229229 children (recursively) to a buffer.
230230
@@ -238,6 +238,9 @@ def show(self, buf=sys.stdout, offset=0, attrnames=False, nodenames=False, showc
238238 True if you want to see the attribute names in
239239 name=value pairs. False to only see the values.
240240
241+ showemptyattrs:
242+ False if you want to suppress printing empty attributes.
243+
241244 nodenames:
242245 True if you want to see the actual node names
243246 within their parents.
@@ -253,12 +256,13 @@ def show(self, buf=sys.stdout, offset=0, attrnames=False, nodenames=False, showc
253256 buf.write(lead + self.__class__.__name__+ ': ')
254257
255258 if self.attr_names:
259+ is_empty = lambda v: v is None or (hasattr(v, '__len__') and len(v) == 0)
260+ nvlist = [(n, getattr(self,n)) for n in self.attr_names \
261+ if showemptyattrs or not is_empty(getattr(self,n))]
256262 if attrnames:
257- nvlist = [(n, getattr(self,n)) for n in self.attr_names]
258263 attrstr = ', '.join('%s=%s' % nv for nv in nvlist)
259264 else:
260- vlist = [getattr(self, n) for n in self.attr_names]
261- attrstr = ', '.join('%s' % v for v in vlist)
265+ attrstr = ', '.join('%s' % v for (_,v) in nvlist)
262266 buf.write(attrstr)
263267
264268 if showcoord:
@@ -270,6 +274,7 @@ def show(self, buf=sys.stdout, offset=0, attrnames=False, nodenames=False, showc
270274 buf,
271275 offset=offset + 2,
272276 attrnames=attrnames,
277+ showemptyattrs=showemptyattrs,
273278 nodenames=nodenames,
274279 showcoord=showcoord,
275280 _my_node_name=child_name)
0 commit comments