Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 18 additions & 0 deletions examples/newline_scrubbing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import alhambra.mixes as am

if __name__ == '__main__':

s1 = am.Strand('s1', '100 uM', plate='plate 1', well='A1')
s2 = am.Strand('s2', '100 uM', plate='plate 1', well='A2')
mix1 = am.Mix(name='my mix',
actions=[am.MultiFixedConcentration(components=[s1, s2], fixed_concentration='10uM')],
fixed_total_volume='100 uL',
test_tube_name='my\nmix\n1')
# print(mix1)
# print('*' * 99)
mix2 = am.Mix(name='my mix',
actions=[am.MultiFixedConcentration(components=[mix1, s1, s2], fixed_concentration='1uM')],
fixed_total_volume='100 uL',
test_tube_name='my\nmix\n2')
t = mix2.table()
print(t)
20 changes: 19 additions & 1 deletion src/alhambra/mixes.py
Original file line number Diff line number Diff line change
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