Skip to content

metadata string reduction#4040

Merged
bjlittle merged 2 commits intoSciTools:masterfrom
rcomer:metadata-str
Mar 1, 2021
Merged

metadata string reduction#4040
bjlittle merged 2 commits intoSciTools:masterfrom
rcomer:metadata-str

Conversation

@rcomer
Copy link
Member

@rcomer rcomer commented Feb 26, 2021

🚀 Pull Request

Description

Suppose I make, for example, a coordinate, and print it

coord = iris.coords.DimCoord(range(3), long_name='foo', units='m')
print(coord)

I get info about what it contains:

DimCoord(array([0, 1, 2]), standard_name=None, units=Unit('m'), long_name='foo')

However, if I just print the metadata,

print(coord.metadata)

I see all the unset stuff too, which I'm not really interested in:

DimCoordMetadata(standard_name=None, long_name='foo', var_name=None, units=Unit('m'), attributes={}, coord_system=None, climatological=False, circular=False)

For differences, it's hard to see at a glance what is actually different:

coord2 = iris.coords.DimCoord(range(3), long_name='foo', units='m', attributes=dict(bar=1), var_name='wibble')
print(coord2.metadata.difference(coord.metadata))
DimCoordMetadata(standard_name=None, long_name=None, var_name=('wibble', None), units=None, attributes=({'bar': 1}, {}), coord_system=None, climatological=None, circular=None)

With my branch, these outputs become:

DimCoordMetadata(long_name=foo, units=m, attributes={}, climatological=False, circular=False)
DimCoordMetadata(var_name=('wibble', None), attributes=({'bar': 1}, {}))

Note that, if you do want to see all the unset metadata, you can still use the repr. We could potentially also skip empty attributes dictionaries for the str.

Does something like this need tests? I had a peek at the coord string tests, but they seem to be focussed on getting date formats right.


Consult Iris pull request check list

@bjlittle
Copy link
Member

bjlittle commented Mar 1, 2021

@rcomer Hey, thanks for pushing this 👍

I'm about to make a similar change to _DimensionalMetadata for having a slimmer __str__ as opposed to an unambiguous __repr__ ... btw it will also allow you to keep data lazy rather realizing it just to print it.

@bjlittle bjlittle self-requested a review March 1, 2021 09:24
Copy link
Member

@bjlittle bjlittle left a comment

Choose a reason for hiding this comment

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

@rcomer Awesome, thanks! 😄

Just a couple of suggestions... also is you wanted to add some specific tests to exercise this, then that would be great 👍

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
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__ ?

field_strings = []
for field in self._fields:
value = getattr(self, field)
if value is not None:
Copy link
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
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 😆

@rcomer
Copy link
Member Author

rcomer commented Mar 1, 2021

Hi @bjlittle, I've addressed those specific comments. Do you have anything particular in mind for tests? I haven't come up with any prime candidate for "this might go wrong", so was just thinking of checking the method works without crashing for one example out of each subclass. It would be good to know that's the Right Way Forward before embarking on that though.

@rcomer
Copy link
Member Author

rcomer commented Mar 1, 2021

I can confirm that this method gets called indirectly by 60 other tests because, well, I broke them!

@bjlittle
Copy link
Member

bjlittle commented Mar 1, 2021

@rcomer I think this is totally fine TBH.

I may add two iris.common.metadata.BaseMetadata tests just to be explicit.

I'll push that separately, if you fancy reviewing and merged?

@bjlittle bjlittle merged commit 7f67d1f into SciTools:master Mar 1, 2021
@bjlittle
Copy link
Member

bjlittle commented Mar 1, 2021

@rcomer See #4043 👍

@rcomer
Copy link
Member Author

rcomer commented Mar 1, 2021

Sweet. Thanks @bjlittle!

@rcomer rcomer deleted the metadata-str branch March 1, 2021 14:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants