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: move and rename some classes for reusability #4669

Merged
merged 5 commits into from
Oct 11, 2022

Conversation

grondo
Copy link
Contributor

@grondo grondo commented Oct 10, 2022

This PR moves some code around in flux.util and flux.job.info to enhance reusability (required for some follow up PRs), including some other cleanup.

In summary:

  • The JobFormatter and HeaderFormat class are embedded in the JobInfoFormatclass, but these classes are not job specific. Move JobFormatter to flux.util.UtilFormatter and embed HeaderFormat in the more general OutputFormat class.
  • Move JobDatetime, which is also not really job-specific, to flux.util.UtilDatetime, and the fsd() function from flux.job.info into flux.util.
  • Move filter_empty() to OutputFormat and modify its return type to be a new format string instance of an instance of the OutputFormat class for reusability.
  • Fix a bug in OutputFormat constructor when a format string that already has 0. prefixes is used.

Problem: The job-specific JobInfoFormat class embeds JobFormatter
and HeaderFormat classes and a filter_empty() method, all of which
are not necessarily job-specific classes and methods. This means that
other utilities and code that generate formatted output can't reuse
these classes and methods.

Move the JobFormatter class to flux.util.UtilFormatter and embed the
generic HeaderFormatter class in flux.util.OutputFormat. Also move
the useful filter_empty() method to flux.util.OutputFormat where it
can be reused.

Also move JobDatetime, which is also not really job-specific, to
flux.util.UtilDatetime, and the fsd() function from flux.job.info
into flux.util.
Problem: mypy doesn't like the lack of type information for the
UtilFormat.headings attribute. It complains:

 .../python/flux/util.py:336: error: Need type annotation for 'headings'

Add type annotation for UtilFormat.headings.
Problem: The original provided format is lost after the OutputFormat
constructor if the prepend option is used. This means the filter_empty()
method returns the prepended format string, which can cause errors
where the prepended format fields are not expected.

Save the original format string in OutputFormat and add an orig
parameter that, when true, returns the original string instead of the
modified format string.
Problem: The OutputFormat.filter_empty() method returns a new instance
of the class, but this makes it difficult to use this function from
subclasses of OutputFormat that have different constructor arguments.

Change the filter_empty() method to return the new format string,
so that the caller can construct the result with the appropriate
constructor.
Problem: The OutputFormat constructor validates that all fields in
the format are in the self.headings list, but if the supplied format
contains a "0." prefix then the fields definitely won't match and
the constructor erroneously raises an exception.

Remove any "0." prefix from format fields before checking for validity.
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!

@grondo
Copy link
Contributor Author

grondo commented Oct 11, 2022

Thanks! I'll set MWP.

@mergify mergify bot merged commit 86fee84 into flux-framework:master Oct 11, 2022
@grondo grondo deleted the python-format-refactor branch October 11, 2022 15:23
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.

2 participants