Skip to content

Commit 1b49791

Browse files
author
hvalev
committed
split basic has-key checks from multiline checks + formatting
1 parent ca1a077 commit 1b49791

File tree

1 file changed

+37
-30
lines changed

1 file changed

+37
-30
lines changed

py_markdown_table/markdown_table.py

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,19 @@ def __init__(
5353
self.quote = True
5454
self.skip_data_validation = skip_data_validation
5555

56-
self.__update_meta_params()
5756
self.__validate_parameters()
58-
59-
if not skip_data_validation:
57+
58+
if not self.skip_data_validation:
6059
self.__validate_data(data)
6160

61+
self.__update_meta_params()
62+
63+
# we need to first update the meta_params for cell width, padding etc
64+
# prior to checking whether the data will fit for multiline rendering
65+
if self.multiline:
66+
self.__validate_multiline(self.data)
67+
68+
6269
def set_params(
6370
self,
6471
row_sep: str = "always",
@@ -129,11 +136,17 @@ def set_params(
129136
if isinstance(padding_weight, str):
130137
self.padding_weight = {key: padding_weight for key in self.data[0].keys()}
131138

132-
self.__update_meta_params()
139+
133140
self.__validate_parameters()
141+
134142
if not self.skip_data_validation:
135143
self.__validate_data(self.data)
136144

145+
self.__update_meta_params()
146+
147+
if self.multiline:
148+
self.__validate_multiline(self.data)
149+
137150
return self
138151

139152
def __update_meta_params(self):
@@ -203,29 +216,30 @@ def __validate_parameters(self): # noqa: C901
203216

204217
def __validate_data(self, data):
205218
# Check if all dictionaries in self.data have uniform keys
206-
keys = set(data[0].keys()) # Use set for fast lookup
219+
keys = set(data[0].keys())
207220
for item in data:
208221
if not isinstance(item, dict):
209222
raise TypeError("Each element in data must be a dictionary.")
210223
if set(item.keys()) != keys:
211224
raise ValueError("Dictionary keys are not uniform across data variable.")
212225

213-
if self.multiline:
214-
for i, row in enumerate(data):
215-
for key in row.keys():
216-
if key in self.var_padding:
217-
multiline_data = row[key].split(self.multiline_delimiter)
218-
multiline_max_width = max(multiline_data, key=len)
219-
if len(multiline_max_width) + self.padding_width[key] > self.var_padding[key]:
220-
raise ValueError(
221-
f"There is a contiguous string:\n"
222-
f"'{multiline_max_width}'\n"
223-
f"in the element [{i}] "
224-
f"which is longer than the allocated column width "
225-
f"for column '{key}' and padding_width '{self.padding_width[key]}'."
226-
)
227-
else:
228-
raise KeyError(f"Key '{key}' not found in var_padding.")
226+
def __validate_multiline(self, data):
227+
for i, row in enumerate(data):
228+
for key in row.keys():
229+
if key in self.var_padding:
230+
multiline_data = row[key].split(self.multiline_delimiter)
231+
multiline_max_string = max(multiline_data, key=len)
232+
multiline_max_width = len(multiline_max_string)
233+
if multiline_max_width + self.padding_width[key] > self.var_padding[key]:
234+
raise ValueError(
235+
f"There is a contiguous string:\n"
236+
f"'{multiline_max_string}'\n"
237+
f"in the element [{i}] "
238+
f"which is longer than the allocated column width "
239+
f"for column '{key}' and padding_width '{self.padding_width[key]}'."
240+
)
241+
else:
242+
raise KeyError(f"Key '{key}' not found in var_padding.")
229243

230244
def __get_padding(self):
231245
"""Calculate table-wide padding."""
@@ -255,13 +269,6 @@ def __get_row_sep_str(self):
255269
row_sep_str += "+"
256270
return row_sep_str
257271

258-
def __get_row_sep_last(self):
259-
row_sep_str_last = "+"
260-
for value in self.var_padding.values():
261-
row_sep_str_last += "-" * (value + 1)
262-
row_sep_str_last = row_sep_str_last[:-1] + "+"
263-
return row_sep_str_last
264-
265272
def __get_margin(self, margin, key):
266273
# get column-specific alignment based on the column key (header)
267274
if self.padding_weight[key] == "left":
@@ -284,7 +291,7 @@ def __get_row(self, item):
284291
for key in self.data[0].keys():
285292
if len(item[key]) > self.var_padding[key]:
286293
multiline = True
287-
if '\n' in item[key]:
294+
if "\n" in item[key]:
288295
multiline = True
289296

290297
if multiline:
@@ -311,7 +318,7 @@ def __get_normal_row(self, item):
311318
row += "|"
312319
return row
313320

314-
def __get_multiline_row(self, item):
321+
def __get_multiline_row(self, item): # noqa: C901
315322
multiline_items = {}
316323

317324
# Helper function to process each element and split by emojis if present

0 commit comments

Comments
 (0)