Skip to content

Commit 77e1a00

Browse files
author
hvalev
committed
improved validations for padding
1 parent 0308597 commit 77e1a00

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

py_markdown_table/markdown_table.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def __update_meta_params(self):
144144
self.var_row_sep = self.__get_row_sep_str()
145145
self.var_row_sep_last = self.__get_row_sep_last()
146146

147-
def __validate_parameters(self):
147+
def __validate_parameters(self): # noqa: C901
148148
valid_values = {
149149
"row_sep": ["always", "topbottom", "markdown", None],
150150
"emoji_spacing": ["mono", None],
@@ -155,41 +155,49 @@ def __validate_parameters(self):
155155
"padding_weight": ["left", "right", "centerleft", "centerright"],
156156
}
157157

158+
# Validate fixed value attributes
158159
for attr, values in valid_values.items():
159160
if getattr(self, attr) not in values:
160161
raise ValueError(f"{attr} value of '{getattr(self, attr)}' is not valid. Possible values are {values}.")
161-
162+
163+
# Validate padding_weight
162164
if isinstance(self.padding_weight, dict):
163-
for attr, values in valid_dict_values.items():
164-
for key, value in dict(getattr(self, attr)).items():
165-
if value not in values:
166-
raise ValueError(f"padding_weight[{key}] value of '{value}' is not valid. Possible values are {values}.")
165+
for key, value in self.padding_weight.items():
166+
if value not in valid_dict_values["padding_weight"]:
167+
raise ValueError(f"padding_weight[{key}] value of '{value}' is not valid. Possible values are {valid_dict_values['padding_weight']}.")
167168
else:
168169
raise ValueError(f"padding_weight value of '{self.padding_weight}' is not valid.")
169170

171+
# Validate padding_width
170172
if isinstance(self.padding_width, dict):
171173
for key, value in self.padding_width.items():
172-
if not isinstance(value, int) and (value < 0 and value > 100000):
173-
raise ValueError(f"padding_width[{key}] value of '{value}' is not valid. Possible ranges of values are 0 < and > 100000.")
174+
if not isinstance(value, int) or not (0 <= value < 100000):
175+
raise ValueError(f"padding_width[{key}] value of '{value}' is not valid. Possible range is 0 <= value < 100000.")
174176
else:
175177
raise ValueError(f"padding_width value of '{self.padding_width}' is not valid.")
176178

177-
if not isinstance(self.padding_char, (str, dict)) or len(self.padding_char) != 1:
179+
# Validate padding_char
180+
if not isinstance(self.padding_char, (str, dict)) or (isinstance(self.padding_char, str) and len(self.padding_char) != 1):
178181
raise ValueError(f"padding_char value of '{self.padding_char}' is not valid. Please use a single character string.")
179182

183+
# Validate float_rounding
180184
if not isinstance(self.float_rounding, (type(None), int)):
181185
raise ValueError(f"float_rounding value of '{self.float_rounding}' is not valid. Please use an integer or leave as None.")
182186

187+
# Validate multiline
183188
if not isinstance(self.multiline, (type(None), dict)):
184189
raise ValueError(f"multiline value of '{self.multiline}' is not valid. Please use a dict or leave as None.")
185190

191+
# Validate multiline_delimiter
186192
if not isinstance(self.multiline_delimiter, str) or len(self.multiline_delimiter) != 1:
187193
raise ValueError(f"multiline_delimiter value of '{self.multiline_delimiter}' is not valid. Please use a single character string.")
188194

195+
# Validate quote
189196
if not isinstance(self.quote, bool):
190197
raise ValueError(f"quote value of '{self.quote}' is not valid. Please use a boolean.")
191198

192199

200+
193201
def __validate_data(self, data):
194202
# Check if all dictionaries in self.data have uniform keys
195203
keys = set(data[0].keys()) # Use set for fast lookup

0 commit comments

Comments
 (0)