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

Refactor TimePoint getters and fix decimal quantity dump rounding #187

Merged
merged 8 commits into from
Nov 12, 2020

Conversation

MetRonnie
Copy link
Contributor

@MetRonnie MetRonnie commented Oct 16, 2020

This branch was kicking around for a while; I probably meant to create a PR earlier.

  • Closes Simplify TimePoint getters #180 - use only @property decorators for getter methods, instead of having a mix of those and a get(self, property_name) method
  • Closes TimePoint dumping buggy for decimals that round up to 1 #184 - fix bug where decimal quantities were getting rounded up to illegal values when dumping timepoints, e.g. minute of hour = 59.999999999 --> 60.0. Just truncate the quantity instead (in this specific circumstance), so it dumps as 59.999999 to 6 d.p.
  • Now prevents TimePoint from being init'd without a year (unless it's a truncated TP)

  • Change log updated
  • Unit tests included

Fix rounding bug for decimal time quantities
TimePoints are immutable now, so we don't need to worry about testing 
for mutating an attr to a bad value
@MetRonnie MetRonnie added the bug label Oct 16, 2020
@MetRonnie MetRonnie added this to the 2.1 milestone Oct 16, 2020
@MetRonnie MetRonnie self-assigned this Oct 16, 2020
metomi/isodatetime/dumpers.py Outdated Show resolved Hide resolved
@@ -880,11 +880,11 @@ class TimePoint:

Keyword arguments (usually default to None if not provided):

expanded_year_digits (default 0) - an agreed-upon number of extra
digits to represent the year, beyond the default of 4. For example,
num_expanded_year_digits (default 0) - an agreed-upon number of extra
Copy link
Member

Choose a reason for hiding this comment

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

I don't have a preference for expanded_year_digits or num_expanded_year_digits, but as this property is changing for TimePoint, should we change it in the rest of the code too? grep'ing for expanded.year.digit, looks like test__00.py, parser_spec.py, data.py, parsers.py, dumpers.py, and README.md mention expanded year digits.

Are those reference OK, in which case this rename here is necessary as this property doesn't represent TimePoint's expanded year digits? (not sure if that makes sense, sorry 😆 ), or are we going to rename them too later?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's the difference between the number of digits and the actual digits themselves

@property
def num_expanded_year_digits(self): return self._num_expanded_year_digits

@property
def expanded_year_digits(self):
"""The extra digits at the front of the expanded year, as opposed to
the number of such digits"""
return abs(self._year / 10000)

(not sure why those two are showing different indentations, they're the same)

Before, we had TimePoint.expanded_year_digits which was the number of digits, and TimePoint.get("expanded_year_digits") which was the digits themselves. The former should probably have always been called num_expanded_year_digits, whereas the latter has not been renamed (just converted to @property)

…ound up to 1

This is to prevent e.g. minute_of_hour dumping "60.0".

Tests added
Unless truncated TimePoint. Tests included.
@MetRonnie
Copy link
Contributor Author

(The tests on Travis have passed, it's just Codacy complaining about an unassigned variable or something in a test)

Copy link
Member

@oliver-sanders oliver-sanders left a comment

Choose a reason for hiding this comment

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

Looks fine, adds more breaking changes for Cylc to deal with but they are pretty small and easy to handle.

# Truncate instead of rounding up because ticking over the higher
# quantities would be complicated
return "999999"
string = "%f" % decimal
Copy link
Member

@oliver-sanders oliver-sanders Oct 26, 2020

Choose a reason for hiding this comment

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

Perhaps make it clearer that the rounding is happening implicitly in a string formatting expression (I had to look it up):

Suggested change
string = "%f" % decimal
string = "%0.6f" % decimal

Or just use round(decimal, 6)?

Comment on lines -1087 to -1089
value_error_timepoint = data.TimePoint(minute_of_hour=10)
value_error_timepoint._minute_of_hour = "1O"
self.assertRaises(ValueError, dumper.dump, value_error_timepoint, "%M")
Copy link
Member

Choose a reason for hiding this comment

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

Behaviour change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now that TimePoints are "immutable", we shouldn't have to worry about mutating a property to an invalid value

@oliver-sanders
Copy link
Member

BTW to make codacy happy just dedent that function and remove the unused self arg.

@MetRonnie
Copy link
Contributor Author

Oh, codacy only shows the newest problem it has found with the code. Last time it was failing because of this expression-not-assigned (W0106). Trouble is, if you assign it, flake8 will flag it as an unused variable!

@MetRonnie
Copy link
Contributor Author

@kinow just a reminder this is awaiting your review, no rush though

@kinow kinow merged commit cc9e794 into metomi:master Nov 12, 2020
@MetRonnie MetRonnie deleted the timepoint-getters branch November 12, 2020 20:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TimePoint dumping buggy for decimals that round up to 1 Simplify TimePoint getters
3 participants