Skip to content

Commit

Permalink
closes #50: scrub newlines from entries
Browse files Browse the repository at this point in the history
  • Loading branch information
dave-doty committed Mar 29, 2022
1 parent 50e7c00 commit 9b07638
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/alhambra/mixes.py
Expand Up @@ -506,6 +506,8 @@ def _formatter(
) -> str:
if isinstance(x, (int, str)):
out = str(x)
if tablefmt in _TABLEFMTS_NEWLINES_SIGNIFICANT:
out = out.replace("\n", " ")
elif x is None:
out = ""
elif isinstance(x, float):
Expand Down Expand Up @@ -2719,6 +2721,14 @@ def _html_row_with_attrs(
html_with_borders_tablefmt,
]

_TABLEFMTS_NEWLINES_SIGNIFICANT = [
"github",
"pipe",
"simple",
"grid",
"rst",
]


@attrs.define()
class PlateMap:
Expand Down Expand Up @@ -2770,7 +2780,7 @@ def to_table(
.. code-block:: python
plate_maps = mix.plate_maps()
maps_strs = '\n\n'.join(plate_map.to_table())
maps_strs = '\n\n'.join(plate_map.to_table() for plate_map in plate_maps)
from IPython.display import display, Markdown
display(Markdown(maps_strs))
Expand All @@ -2781,6 +2791,10 @@ def to_table(
which creates a Markdown format. To create other formats such as HTML, change the value of
`tablefmt`; see https://github.com/astanin/python-tabulate#readme for other possible formats.
If `tablefmt` is one in which newlines are significant (e.g., "pipe" or "rst"),
then any strings that end up in the final returned value have newlines replaced with spaces
to prevent them from disrupting the formatting.
:param well_marker:
By default the strand's name is put in the relevant plate entry. If `well_marker` is specified
and is a string, then that string is put into every well with a strand in the plate map instead.
Expand Down Expand Up @@ -2856,6 +2870,10 @@ def to_table(
well_marker_to_use = well_marker
elif callable(well_marker):
well_marker_to_use = well_marker(well_str)

if tablefmt in _TABLEFMTS_NEWLINES_SIGNIFICANT:
well_marker_to_use = well_marker_to_use.replace("\n", " ")

table[r][c] = well_marker_to_use
if not well_pos.is_last():
well_pos = well_pos.advance()
Expand Down

0 comments on commit 9b07638

Please sign in to comment.