Skip to content

Commit cd1e440

Browse files
authored
Merge pull request #4 from amahlaka/upgrades_1.0.2
Version 1.0.2, Minor upgrades
2 parents b93a296 + b35b981 commit cd1e440

File tree

3 files changed

+49
-14
lines changed

3 files changed

+49
-14
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "markdown_helper"
7-
version = "1.0.1"
7+
version = "1.0.2"
88
authors = [
99
{ name="Arttu Mahlakaarto", email="arttu.mahlakaarto@gmail.com" },
1010
]

src/markdown_helper/__init__.py

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,35 +72,50 @@ def add_rows(self, rows: list[dict[str, str | int | float | bool]]):
7272
for row in rows:
7373
self.add_row(row)
7474

75-
def add_row(self, row: dict[str, str | int | float | bool]):
75+
def add_row(self, row: dict[str, str | int | float | bool] | list[str]):
7676
"""Add a row to the table.
7777
7878
Args:
79-
row (dict[str, str | int | float | bool]): Row to add
79+
row (dict[str, str | int | float | bool], list[str]): Row to add
8080
"""
8181

82-
# Check that all the keys in the row are in the headers
83-
for key in row.keys():
84-
if key not in self.headers:
85-
if self.flexible_headers:
86-
self.headers.append(key)
87-
else:
88-
raise ValueError(
89-
f"Key {key} not in headers and flexible_headers is False"
90-
)
82+
# If row is a list, convert it to a dict, using the headers as keys
83+
if isinstance(row, list):
84+
if len(row) != len(self.headers):
85+
raise ValueError(
86+
f"Row length ({len(row)}) does not match header length ({len(self.headers)})"
87+
)
88+
row = dict(zip(self.headers, row))
89+
# If row is a dict, check that all the keys are in the headers, if not, raise error
90+
elif isinstance(row, dict):
91+
for key in row.keys():
92+
if key not in self.headers:
93+
if self.flexible_headers:
94+
self.headers.append(key)
95+
else:
96+
raise ValueError(
97+
f"Key {key} not in headers and flexible_headers is False"
98+
)
9199
# Check that all the headers are in the row
92100
for header in self.headers:
93101
if header not in row.keys():
94102
row[header] = ""
95103

96104
self.rows.append(row)
97105

98-
def sort_table(self):
106+
def sort_table(self, disable_convert: bool = False):
99107
"""Sort the table by the sort_key."""
100108
if self.sort_key:
101109
# If multiple sort keys are provided, prioritize the first one, then the second, etc.
102110
sort_keys = self.sort_key.split(",")
103111
for sort_key in sort_keys:
112+
if disable_convert:
113+
self.rows = sorted(
114+
self.rows,
115+
key=lambda row: row.get(sort_key, ""), # pylint: disable=cell-var-from-loop
116+
reverse=self.sort_reverse,
117+
)
118+
break
104119
if all( # pylint: disable=use-a-generator
105120
[
106121
row.get(sort_key, "")
@@ -322,7 +337,7 @@ def __init__(self, title: Header | str, **kwargs):
322337
self.title = title
323338
self.content = kwargs.get("content", "")
324339

325-
def add(self, content: str | Table | List | Image | Link):
340+
def add(self, content: str | Table | List | Image | Link | Header):
326341
"""Add content to the section.
327342
328343
Args:

tests/test_markdown.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,26 @@ def test_table_add_row():
182182
{"col 1": "item 1", "col 2": "item 2", "col 3": "item 3"}
183183
], "Table rows are incorrect."
184184

185+
def test_table_add_row_no_headers():
186+
"""
187+
This function tests the table.add_row function with a list input.
188+
"""
189+
table_1 = markdown.Table(["col 1", "col 2", "col 3"])
190+
table_1.add_row(["item 1", "item 2", "item 3"])
191+
assert (
192+
str(table_1)
193+
== "| col 1 | col 2 | col 3 |\n| --- | --- | --- |\n| item 1 | item 2 | item 3 |\n"
194+
), "String representation of table is incorrect."
195+
assert table_1.headers == [
196+
"col 1",
197+
"col 2",
198+
"col 3",
199+
], "Table columns are incorrect."
200+
assert table_1.rows == [
201+
{"col 1": "item 1", "col 2": "item 2", "col 3": "item 3"}
202+
], "Table rows are incorrect."
203+
204+
185205

186206
def test_table_sort():
187207
"""

0 commit comments

Comments
 (0)