-
Notifications
You must be signed in to change notification settings - Fork 148
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
Function to represent timedeltas without losing precision (precisedelta) #137
Conversation
Add MINUTES, HOURS, DAYS, MONTHS and YEARS as part of the Unit Enum and make them ordenable so MICROSECONDS < MILLISECONDS < SECONDS .. < YEARS
For backward compatibility, only allow SECONDS, MILLISECONDS and MICROSECONDS: this was the original behavior; we can allow the rest of the units later.
A timedelta now can be represented using a combination of days, hours and seconds to represent the delta precisely. >>> delta = dt.timedelta(seconds=3633, days=2, microseconds=123000) >>> precisedelta(delta) '2 days, 1 hour and 33.12 seconds' Note for translators: the ' and ' needs translation.
Codecov Report
@@ Coverage Diff @@
## master #137 +/- ##
==========================================
+ Coverage 99.28% 99.42% +0.14%
==========================================
Files 9 9
Lines 421 524 +103
==========================================
+ Hits 418 521 +103
Misses 3 3
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the tests and the docstrings, looking good!
The other docstrings (or those I've updated so far) are done following Google's style guide.
If it's not too much trouble to update following that, that'd be great. But if it doesn't really fit, you can leave them as this.
src/humanize/time.py
Outdated
|
||
def _quotient_and_remainer(value, divisor, unit, minimum_unit, suppress): | ||
"""Divide ``value`` by ``divisor`` returning the quotient and | ||
the remainer as follows: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the remainer as follows: | |
the remainder as follows: |
Also, would you be able to add tests to hit the error cases? The red lines in https://codecov.io/gh/jmoiron/humanize/pull/137/diff?src=pr&el=tree#diff-c3JjL2h1bWFuaXplL3RpbWUucHk= return NotImplemented if tmp not in (Unit.SECONDS, Unit.MILLISECONDS, Unit.MICROSECONDS):
raise ValueError("Minimum unit '%s' not supported" % minimum_unit) raise ValueError(
"Minimum unit is suppresed and not suitable replacement was found"
) if date is None:
return value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
A few more bits and pieces :)
tests/test_time.py
Outdated
assertRaises( | ||
humanize.precisedelta, ValueError, 1, minimum_unit="years", suppress=["years"] | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assertRaises( | |
humanize.precisedelta, ValueError, 1, minimum_unit="years", suppress=["years"] | |
) | |
with pytest.raises(ValueError) as e: | |
humanize.precisedelta(1, minimum_unit="years", suppress=["years"]) |
Thank you! |
Fixes #48
Changes proposed in this pull request:
humanize
(years, months, ...)Unit.YEARS < Unit.MONTHS
for example)precisedelta
function to represent timedeltas without losing precision (fixes function to print timedeltas exactly #48, supersedes Added format_timedelta and support function english_list. #57)Current limitations:
,
is used as separator. It could be easily changed but still we should consider the localization for this.and
is used to join the last item. Translation is required.Examples:
Return a precise representation of a timedelta
A custom format can be specified to control how the fractional part
is represented:
Instead, the minimum unit can be changed to have a better resolution;
the function still will readjust the unit to use the greatest of the
units that does not loose precision.
For example setting microseconds but still representing the date
with milliseconds:
If desired, some units can be suppressed: you will not see them
represented and the time of the other units will be adjusted
to keep representing the same timedelta:
Note that microseconds precision is lost if the seconds and all
the units below are suppressed: