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

python: improve handling of !d conversion specifier in output formats #5169

Merged
merged 6 commits into from May 11, 2023

Conversation

grondo
Copy link
Contributor

@grondo grondo commented May 11, 2023

This PR fixes a few annoyances when using the !d conversion specifier in output formats as discussed over in #5055

  • A !d converted timestamp now renders as an empty string for a timestamp of 0.0
  • The h suffix can now be used with !d to replace the empty timestamp with a hyphen -
  • Currently !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.

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.
Copy link
Member

@chu11 chu11 left a 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.

Comment on lines 940 to 948
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
Copy link
Member

@chu11 chu11 May 11, 2023

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' '

Copy link
Contributor Author

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.
@codecov
Copy link

codecov bot commented May 11, 2023

Codecov Report

Merging #5169 (5283771) into master (e3c8698) will decrease coverage by 0.03%.
The diff coverage is 100.00%.

❗ Current head 5283771 differs from pull request most recent head c73aa4f. Consider uploading reports for the commit c73aa4f to get more accurate results

@@            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     
Impacted Files Coverage Δ
src/bindings/python/flux/util.py 95.21% <100.00%> (+0.05%) ⬆️

... and 8 files with indirect coverage changes

@grondo
Copy link
Contributor Author

grondo commented May 11, 2023

Since this is approved I'll set MWP.

@mergify mergify bot merged commit 5b0897b into flux-framework:master May 11, 2023
30 of 32 checks passed
@grondo grondo deleted the utildatetime-hyphen branch May 11, 2023 22:45
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.

None yet

2 participants