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
python: improve handling of !d
conversion specifier in output formats
#5169
Conversation
Problem: It is inconvenient that UtilDatetime objects (obtained via the `!d` conversion specifier) are treated differently than the normal datetime conversion (`!D`) when the timestamp is zero. In the first case, the datetime is rendered to the epoch date in whatever format is specified by the user, while in the second case the result is an empty string. Since a zero timestamp is considered "unset", the empty string is much more preferable here. Since the prime use case for UtilDatetime is in output format rendering for utilities, have this class return an empty string by default for zero timestamps, presuming these are unset. Fix one test in t2800-jobs-cmd.t that assumed a 00:00 result.
Problem: The `!d` conversion specifier converts timestamps to UtilDatetime objects which support a `::` separator in order to allow a format after the datetime format specification. However, since the format is applied within the UtilDatetime format() method, the `h` suffix is not supported to replace empty fields with a hyphen. Check for a `h` suffix in the UtilDatetime.format() method and replace an empty string with `-`. Ignore UtilDatetime objects where `h` is normally handled so that the suffix can later be processed in UtilDatetime.format()
Problem: The `!d` conversion specifier returns an empty string if no format is provided, which could cause confusion in casual usage. For example `{expiration!d}` returns an empty string even if there is an expiration timestamp for a job. If no format is provided, default to an ISO 8601 format "%FT%T".
Problem: The documentation of `!d` handling in flux-jobs(1) is out of date. Update the documentation to include that `!d` formats to an empty string when unset (is 0.), and that `h` is supported.
Problem: There are no unit tests for the UtilDatetime class, which makes verification of the behavior of this class difficult. Add a small set of unit tests to t/python/t0024-util.py to ensure basic proper behavior of the various formatting options for UtilDatetime objects.
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.
LGTM, just one note below on probably one more test to add.
t/t2800-jobs-cmd.t
Outdated
cat expiration.in | \ | ||
flux jobs --from-stdin -o "{expiration!d:%b%d %R::>20.20h}" \ | ||
>exp-fmth.out && | ||
test_debug "cat exp-fmt.out" && | ||
grep " EXPIRATION" exp-fmt.out && | ||
grep " $(date --date=@${exp} +%b%d\ %R)" exp-fmt.out | ||
grep " $(date --date=@${exp} +%b%d\ %R)" exp-fmt.out && | ||
test_debug "cat exp-fmt.out" && | ||
grep " EXPIRATION" exp-fmth.out && | ||
grep " $(date --date=@${exp} +%b%d\ %R)" exp-fmth.out |
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.
i think we need to add one test for !d
without any format being specified {expiration!d}
, perhaps in the test above test_expect_success 'flux-jobs --format={expiration!d:%FT%T},{t_remaining!H} works' '
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.
Ok, added a separate test for that and force pushed the result.
Problem: t2800-jobs-cmd.t does not test a couple use cases of the `!d` format specifier. Ensure `!d` works as expected when the `h` suffix is utilized, and that `!d` works when used without a format.
5283771
to
c73aa4f
Compare
Codecov Report
@@ Coverage Diff @@
## master #5169 +/- ##
==========================================
- Coverage 83.15% 83.12% -0.03%
==========================================
Files 453 453
Lines 77850 77856 +6
==========================================
- Hits 64733 64720 -13
- Misses 13117 13136 +19
|
Since this is approved I'll set MWP. |
This PR fixes a few annoyances when using the
!d
conversion specifier in output formats as discussed over in #5055!d
converted timestamp now renders as an empty string for a timestamp of0.0
h
suffix can now be used with!d
to replace the empty timestamp with a hyphen-
!d
used without a format returns an empty string, this PR adds a default format so that a ISO 8601 string is returned instead.I've also added a set of unit tests for the affected
UtilDatetime
class to ensure the above is working.