-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Add a doc example for an io.ascii writer with fixed width and commented header #17630
Conversation
Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.
|
meta: I think I'm recognizing the issue as a case of the diamond problem. The root issue seems to be that header and data classes share code via inheritance, instead of composition; which is a much larger with the fundamental design. I know we won't address this anytime soon (it's a massive undertaking, in particular because of huge backward compatibility concerns), but I wanted to seize the opportunity to raise awareness on the question. Otherwise, and pragmatically, this looks good to me ! |
Somewhat out of scope, but while you are in this section... I noticed that the examples above don't actually run. They are missing the imports and the format name already exists, so you get an exception trying to register. So:
|
commented. So, we now want to make a writer that can write this format; for this example we do | ||
not bother to work out how to read this format, but just raise an error on reading: | ||
|
||
>>> from astropy.io.ascii.fixedwidth import FixedWidthData, FixedWidth |
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 style in this section for long code examples is to not use the >>>
and ...
prefixes. Then put a newline between the lines that currently have a >>>
prefix. The idea was that this code is more often copied into an editor, not IPython.
@neutrinoceros : It looks like a diamond problem, but it's not. Parsing a fixed-width table is fundamentally different because you have to parse several lines (on writing, even the entire table) to see how many characters the longest value in each column is. That makes the code so different that you can't just reuse the normal commented header. Also, for reading this problem is ill defined. What do we expect for a fixed-width-commented header?
or
or
or some other variant? Sure, you can "just try them all" but I can see already so many ambiguous cases, that it's essentially guaranteed to give wrong results in many cases. As an example for how to write, that better. Also note you can kind of make reader by composition, it doesn't have to be pure inheritance: See the latter two examples in this section: https://docs.astropy.org/en/latest/io/ascii/read.html#advanced-customization |
@taldcroft Unfortunately, the two requirements you ask for are mutually incompatible: The earlier examples are not run as doctest because they don't have the Fortunately, that's exactly why we have that little "copy" button on all code examples in the docs. It used javascript to strip markup and output from the doctest example and copies just the code itself: |
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.
Awesome, thanks!
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.
Should we be more consistent with the blankline before def
/class
across the diff? (See suggestions.)
Thanks!
... header_class = CommentedHeaderHeader | ||
... data_class = NoHeaderData | ||
|
||
**Application: Write a "fixed_width" table with a "commented_header"** |
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.
You don't want this to be a sub-section?
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 want this to be on the same level as the existing examples, and they are all just formatted like this with a bold font line.
Co-authored-by: P. L. Lim <2090236+pllim@users.noreply.github.com>
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!
…ith fixed width and commented header
…630-on-v7.0.x Backport PR #17630 on branch v7.0.x (Add a doc example for an io.ascii writer with fixed width and commented header)
Description
It has been suggested more than once (#4112 and #5922, which was closed as a dublicate) that it would be useful to write a fixed-format ASCII table with a commented header. However, as a "format" this is reqest is somewhat ambiguous, for example if the fixed format has delimiters like "|" would they be shifted by the comment symbols? Replaced by the comment symbols? What about tables with more than one header line, e.g. a second line like
--- ------- -------
that delinated the fixed-width columns. Would that line be commented or not?So, instead, I suggested in the discussion for #4112 that this might make a good example for the "advanced customization" section of the docs, and that is what this PR implements.
Fixes #4112