From 8cccc1c56a46fa6a4f7e628256cfbe1b3765366d Mon Sep 17 00:00:00 2001 From: mecaneer23 Date: Mon, 20 May 2024 16:27:23 -0500 Subject: [PATCH] feat: add _get_max_column_widths still doesn't work because _get_delimiter_locations doesn't support stripping spaces --- src/md_to_py.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/md_to_py.py b/src/md_to_py.py index 74a4e32..eaf41b2 100644 --- a/src/md_to_py.py +++ b/src/md_to_py.py @@ -2,7 +2,8 @@ Convert a MarkDown table to a formatted Python list. """ -from itertools import repeat +from itertools import pairwise, repeat, starmap +from operator import sub from typing import Callable, Iterable, Iterator, Sequence, TypeVar T = TypeVar("T") @@ -60,16 +61,30 @@ def _get_column_widths( def _get_delimiter_locations(rows: Sequence[str], delimiter: str = "|") -> Iterator[int]: + """ + Yield an iterator of delimiter locations. + + The length of the iterator should be equal to any row.count(delimiter) + """ + + if len(delimiter) != 1: + raise ValueError( + f"`delimiter` must be one character, is {len(delimiter)} characters", + ) + if delimiter == " ": + raise ValueError("`delimiter` cannot be a space") + remaining_rows = len(rows) location_number = [0 for _ in range(remaining_rows)] column = 1 + location_number[1] = rows[0].count(delimiter) for pos in range(len(max(rows, key=len))): for index, row in enumerate(rows): + if index == 1 or pos > len(row): + continue if pos == len(row): remaining_rows -= 1 continue - if pos > len(row): - continue if row[pos] == delimiter: location_number[index] += 1 if all(num >= column for num in location_number): @@ -77,8 +92,9 @@ def _get_delimiter_locations(rows: Sequence[str], delimiter: str = "|") -> Itera yield pos -def _get_max_column_widths(): - raise NotImplementedError() +def _get_max_column_widths(rows: Sequence[str], delimiter: str = "|") -> Iterator[int]: + for item in starmap(sub, pairwise(_get_delimiter_locations(rows, delimiter))): + yield -item - 1 def _pad_columns(row: str, widths: tuple[int, ...] | int, delimiter: str = "|") -> str: