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

Add a doc example for an io.ascii writer with fixed width and commented header #17630

Merged
merged 3 commits into from
Jan 15, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
isort on doc examples and uniform use of empty lines approaching PEP 8
Co-authored-by: P. L. Lim <2090236+pllim@users.noreply.github.com>
  • Loading branch information
hamogu and pllim authored Jan 14, 2025
commit 49a6a8c8a2578003c7d0f5f2c8b2239f68a630a3
6 changes: 5 additions & 1 deletion docs/io/ascii/read.rst
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,7 @@ the basic reader, but header and data start in different lines of the file::

>>> # Note: NoHeader is already included in astropy.io.ascii for convenience.
>>> from astropy.io.ascii.basic import BasicHeader, BasicData, Basic
hamogu marked this conversation as resolved.
Show resolved Hide resolved
>>>
>>> class NoHeaderHeader(BasicHeader):
... """Reader for table header without a header
...
Expand Down Expand Up @@ -751,13 +752,15 @@ commented. So, we now want to make a writer that can write this format; for this
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
Copy link
Member

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.

hamogu marked this conversation as resolved.
Show resolved Hide resolved
>>>
>>> class FixedWidthDataCommentedHeaderData(FixedWidthData):
... def write(self, lines):
... lines = super().write(lines)
... lines[0] = self.write_comment + lines[0]
... for i in range(1, len(lines)):
... lines[i] = ' ' * len(self.write_comment) + lines[i]
... return lines
hamogu marked this conversation as resolved.
Show resolved Hide resolved
>>>
>>> class FixedWidthCommentedHeader(FixedWidth):
... _format_name = "fixed_width_commented_header"
... _description = "Fixed width with commented header"
Expand All @@ -771,8 +774,8 @@ This new format is automatically added to the list of formats that can be read b
the :ref:`io_registry` (note that our format has no mechanism to write out the units):

>>> import sys
>>> from astropy.table import Table
>>> import astropy.units as u
>>> from astropy.table import Table
>>> tab = Table({'v': [15.4, 223.45] * u.km/u.s, 'type': ['star', 'jet']})
>>> tab.write(sys.stdout, format='ascii.fixed_width', delimiter=None)
v type
Expand Down Expand Up @@ -801,6 +804,7 @@ of a reader, and then to modify the properties of this one reader instance
in a function::

>>> from astropy.io import ascii
hamogu marked this conversation as resolved.
Show resolved Hide resolved
>>>
>>> def read_rdb_table(table):
... reader = ascii.Basic()
... reader.header.splitter.delimiter = '\t'
Expand Down
Loading