From da028192c960d009c7c9a8b2064bc6e37f58f955 Mon Sep 17 00:00:00 2001 From: Holger Frey Date: Wed, 19 Jan 2022 14:57:35 +0100 Subject: [PATCH] added excel2pylist --- README.md | 2 +- pyproject.toml | 1 + work_helpers/excel2pylist.py | 59 ++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 work_helpers/excel2pylist.py diff --git a/README.md b/README.md index 39d83c7..af14ff0 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Some helper scripts for the day to day work with Ubuntu in WSL2 - **sg_list_frms**: collect gitea issues and related frm filenames - **xls2changelog**: reformat copied excel data to the safeguard changelog format - **xls2markdown**: reformat copied excel data to a markdown table - + - **xls2pylist**: reformat copied excel data to a python list Proposed workflow for Safeguard projects diff --git a/pyproject.toml b/pyproject.toml index 2760bb0..d03f488 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,6 +22,7 @@ requires = [ [tool.flit.scripts] xls2changelog = "work_helpers.excel2changelog:cli" xls2markdown = "work_helpers.excel2markdown:cli" +xls2pylist = "work_helpers.excel2pylist:cli" sg_mbp_release = "work_helpers.sg_mbp_release:sg_mbp_release" sg_mbp_new_version = "work_helpers.sg_mbp_release:sg_mbp_new_version" sg_list_frms = "work_helpers.sg_frm_list:cli" diff --git a/work_helpers/excel2pylist.py b/work_helpers/excel2pylist.py new file mode 100644 index 0000000..5feb6e9 --- /dev/null +++ b/work_helpers/excel2pylist.py @@ -0,0 +1,59 @@ +import click +import pyperclip + +def _strip_parts(iterable): + return [item.strip() for item in iterable] + +def _remove_item_two(iterable): + """ remove the second item from parts, it's the probe name """ + copied = iterable.copy() + copied.pop(1) # zero based index + return copied + +def _replace_empty_strings(iterable, replacement="None"): + return [i or replacement for i in iterable] + +def prepare(text): + lines = text.splitlines() + + # replacing the german decimal separator with the standard english dot + correct_separator = (l.replace(",", ".") for l in lines) + parted = (l.split("\t") for l in correct_separator) + stripped = (_strip_parts(l) for l in parted) + no_probe_name = (_remove_item_two(l) for l in stripped) + return [_replace_empty_strings(l) for l in no_probe_name] + + +def get_cell_lengths(table): + # get the max lenght of each column + in_columns = zip(*table) + columns_lenghts = (map(len, c) for c in in_columns) + return [max(c) for c in columns_lenghts] + + +def pad_field(index, t): + value, length = t + if index == 0: + return value.rjust(length) + else: + return value.ljust(length) + +def pad_fields(iterable, lengths): + return [pad_field(i, t) for i, t in enumerate(zip(iterable, lengths))] + +def build_list(table, lengths): + padded = (pad_fields(l) for l in lines) + padded_lines = (", ".join(l) for l in padded) + lines_as_list = (f" [{l}], # noqa: E201, E202, E501," for l in padded_lines) + list_content = "\n".join(lines_as_list) + return f"[\n{list_content}\n]\n" + + +@click.command() +def cli(): + xls = pyperclip.paste() + table = split_table(xls) + lengths = get_cell_lengths(table) + result = build_list(table, lengths) + print("copied to clipboard") + pyperclip.copy(result) \ No newline at end of file