Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-31609: lsst.verify.Measurement needs a useful __repr__() #87

Merged
merged 2 commits into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 24 additions & 1 deletion python/lsst/verify/measurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,30 @@ def identifier(self):
return self._id

def __str__(self):
return "{self.metric_name!s}: {self.quantity!s}".format(self=self)
return f"{self.metric_name!s}: {self.quantity!s}"

def __repr__(self):
metricString = str(self.metric_name)
# For readability, don't print rarely-used components if they're None
args = [repr(str(metricString)),
repr(self.quantity),
]

# invariant: self.blobs always exists and contains at least extras
if self.blobs.keys() - {metricString}:
pureBlobs = self.blobs.copy()
del pureBlobs[metricString]
args.append(f"blobs={list(pureBlobs.values())!r}")

# invariant: self.extras always exists, but may be empty
if self.extras:
args.append(f"extras={dict(self.extras)!r}")

# invariant: self.notes always exists, but may be empty
if self.notes:
args.append(f"notes={dict(self.notes)!r}")

return f"Measurement({', '.join(args)})"

def _repr_latex_(self):
"""Get a LaTeX-formatted string representation of the measurement
Expand Down
25 changes: 25 additions & 0 deletions tests/test_measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,31 @@ def test_str(self):
m = Measurement(metric, value)
self.assertEqual(str(m), "test.cmodel_mag: 1235.0 mag")

def test_repr(self):
metric = 'test.cmodel_mag'
self.assertEqual(
repr(Measurement(metric)),
"Measurement('test.cmodel_mag', None)")
value = 1235 * u.mag
self.assertEqual(
repr(Measurement(metric, value)),
"Measurement('test.cmodel_mag', <Quantity 1235. mag>)")

self.assertEqual(
repr(Measurement(metric, value, [self.blob1])),
"Measurement('test.cmodel_mag', <Quantity 1235. mag>, "
f"blobs=[{self.blob1!r}])"
)

notes = {metric + '.filter_name': 'r'}
extras = {'extra1': Datum(10. * u.arcmin, 'Extra 1')}
self.assertEqual(
repr(Measurement(metric, value, notes=notes, blobs=[self.blob1],
extras=extras)),
"Measurement('test.cmodel_mag', <Quantity 1235. mag>, "
f"blobs=[{self.blob1!r}], extras={extras!r}, notes={notes!r})"
)

def _check_yaml_round_trip(self, old_measurement):
persisted = yaml.dump(old_measurement)
new_measurement = yaml.safe_load(persisted)
Expand Down