Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions docs/src/whatsnew/latest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ This document explains the changes made to Iris for this release
representation of the 64-bit non-cryptographic hash of the object using the
extremely fast `xxhash`_ hashing algorithm. (:pull:`4020`)

#. `@rcomer`_ implemented a string method for metadata classes, so printing

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps explicitly clarify, with string -> __str__ ?

these objects skips metadata elements that are set to None. (:pull:`4040`)


🐛 Bugs Fixed
=============
Expand Down
9 changes: 9 additions & 0 deletions lib/iris/common/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,15 @@ def __ne__(self, other):

return result

def __str__(self):
field_strings = []
for field in self._fields:
value = getattr(self, field)
if value is not None:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rcomer I think that we could be even more liberal here and ignore empty strings perhaps, but definitely and empty dict for the attributes member.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It took me longer than I care to admit to figure out the logic for this 😆

field_strings.append(f"{field}={value}")

return f"{type(self).__name__}({', '.join(field_strings)})"

def _api_common(
self, other, func_service, func_operation, action, lenient=None
):
Expand Down