@@ -53,12 +53,19 @@ def __init__(
53
53
self .quote = True
54
54
self .skip_data_validation = skip_data_validation
55
55
56
- self .__update_meta_params ()
57
56
self .__validate_parameters ()
58
-
59
- if not skip_data_validation :
57
+
58
+ if not self . skip_data_validation :
60
59
self .__validate_data (data )
61
60
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
+
62
69
def set_params (
63
70
self ,
64
71
row_sep : str = "always" ,
@@ -129,11 +136,17 @@ def set_params(
129
136
if isinstance (padding_weight , str ):
130
137
self .padding_weight = {key : padding_weight for key in self .data [0 ].keys ()}
131
138
132
- self . __update_meta_params ()
139
+
133
140
self .__validate_parameters ()
141
+
134
142
if not self .skip_data_validation :
135
143
self .__validate_data (self .data )
136
144
145
+ self .__update_meta_params ()
146
+
147
+ if self .multiline :
148
+ self .__validate_multiline (self .data )
149
+
137
150
return self
138
151
139
152
def __update_meta_params (self ):
@@ -203,29 +216,30 @@ def __validate_parameters(self): # noqa: C901
203
216
204
217
def __validate_data (self , data ):
205
218
# 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 ())
207
220
for item in data :
208
221
if not isinstance (item , dict ):
209
222
raise TypeError ("Each element in data must be a dictionary." )
210
223
if set (item .keys ()) != keys :
211
224
raise ValueError ("Dictionary keys are not uniform across data variable." )
212
225
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." )
229
243
230
244
def __get_padding (self ):
231
245
"""Calculate table-wide padding."""
@@ -255,13 +269,6 @@ def __get_row_sep_str(self):
255
269
row_sep_str += "+"
256
270
return row_sep_str
257
271
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
-
265
272
def __get_margin (self , margin , key ):
266
273
# get column-specific alignment based on the column key (header)
267
274
if self .padding_weight [key ] == "left" :
@@ -284,7 +291,7 @@ def __get_row(self, item):
284
291
for key in self .data [0 ].keys ():
285
292
if len (item [key ]) > self .var_padding [key ]:
286
293
multiline = True
287
- if ' \n ' in item [key ]:
294
+ if " \n " in item [key ]:
288
295
multiline = True
289
296
290
297
if multiline :
@@ -311,7 +318,7 @@ def __get_normal_row(self, item):
311
318
row += "|"
312
319
return row
313
320
314
- def __get_multiline_row (self , item ):
321
+ def __get_multiline_row (self , item ): # noqa: C901
315
322
multiline_items = {}
316
323
317
324
# Helper function to process each element and split by emojis if present
0 commit comments