From d15eeb1f9fefb44d9bd38e2e1f5dbc7cec260f31 Mon Sep 17 00:00:00 2001 From: KV Date: Sun, 3 Jan 2021 05:51:02 +0100 Subject: [PATCH] Build output string in one big expression Build output string in component_table_entry() as the similar strings in generate_bom(). Repeating a couple of minor if-expressions is small cost to obtain a more compact and readable main expression. --- src/wireviz/wv_bom.py | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/src/wireviz/wv_bom.py b/src/wireviz/wv_bom.py index 17f090a2..cb9a76a9 100644 --- a/src/wireviz/wv_bom.py +++ b/src/wireviz/wv_bom.py @@ -145,25 +145,18 @@ def component_table_entry( mpn: Optional[str] = None, ) -> str: """Return a diagram node table row string with an additional component.""" - output = f'{qty}' - if unit: - output += f' {unit}' - output += f' x {type}' - # print an extra line with part and manufacturer information if provided manufacturer_str = manufacturer_info_field(manufacturer, mpn) - if pn or manufacturer_str: - output += '
' - if pn: - output += f'P/N: {pn}' - if manufacturer_str: - output += ', ' - if manufacturer_str: - output += manufacturer_str - output = html_line_breaks(output) + output = (f'{qty}' + + (f' {unit}' if unit else '') + + f' x {type}' + + ('
' if pn or manufacturer_str else '') + + (f'P/N: {pn}' if pn else '') + + (', ' if pn and manufacturer_str else '') + + (manufacturer_str or '')) # format the above output as left aligned text in a single visible cell # indent is set to two to match the indent in the generated html table return f''' - +
{output}{html_line_breaks(output)}
''' def manufacturer_info_field(manufacturer: Optional[str], mpn: Optional[str]) -> Optional[str]: