Skip to content

Commit e1f587a

Browse files
committed
fix flake8 complexity complaint
1 parent b44007d commit e1f587a

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

toolium/utils/dataset.py

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,29 @@ def _get_substring_replacement(type_mapping_match_group):
299299
return replace_param
300300

301301

302+
def _get_format_with_number_of_decimals(base):
303+
"""
304+
Get the format and the number of decimals from the base string.
305+
"""
306+
def _is_only_date(base):
307+
return 'TODAY' in base
308+
309+
def _default_format(base):
310+
date_format = '%d/%m/%Y' if language == 'es' else '%Y/%m/%d'
311+
if _is_only_date(base):
312+
return date_format
313+
return f'{date_format} %H:%M:%S'
314+
315+
format_matcher = re.search(r'\((.*)\)', base)
316+
if format_matcher and len(format_matcher.groups()) == 1:
317+
time_format = format_matcher.group(1)
318+
decimal_matcher = re.search(r'%(\d+)f', time_format)
319+
if decimal_matcher and len(decimal_matcher.groups()) == 1:
320+
return time_format.replace(decimal_matcher.group(0), '%f'), int(decimal_matcher.group(1))
321+
return time_format, None
322+
return _default_format(base), None
323+
324+
302325
def _replace_param_date(param, language):
303326
"""
304327
Transform param value in a date after applying the specified delta.
@@ -324,33 +347,15 @@ def _offset_datetime(amount, units):
324347
the_units = units.lower()
325348
return now + datetime.timedelta(**dict([(the_units, the_amount)]))
326349

327-
def _is_only_date(base):
328-
return 'TODAY' in base
329-
330-
def _default_format(base):
331-
date_format = '%d/%m/%Y' if language == 'es' else '%Y/%m/%d'
332-
if _is_only_date(base):
333-
return date_format
334-
return f'{date_format} %H:%M:%S'
335-
336-
def _get_format(base):
337-
format_matcher = re.search(r'\((.*)\)', base)
338-
if format_matcher and len(format_matcher.groups()) == 1:
339-
decimal_matcher = re.search(r'%(\d+)f', format_matcher.group(1))
340-
if decimal_matcher and len(decimal_matcher.groups()) == 1:
341-
return format_matcher.group(1).replace(decimal_matcher.group(0), '%f'), int(decimal_matcher.group(1))
342-
return format_matcher.group(1), None
343-
return _default_format(base), None
344-
345350
matcher = _date_matcher()
346351
if not matcher:
347352
return param, False
348353

349354
base, amount, units = list(matcher.groups())
350-
format_str, decimal_places = _get_format(base)
355+
format_str, number_of_decimals = _get_format_with_number_of_decimals(base)
351356
date = _offset_datetime(amount, units)
352-
if decimal_places:
353-
decimals = f"{date.microsecond / 1_000_000:.{decimal_places}f}"[2:]
357+
if number_of_decimals:
358+
decimals = f"{date.microsecond / 1_000_000:.{number_of_decimals}f}"[2:]
354359
format_str = format_str.replace("%f", decimals)
355360
return date.strftime(format_str), True
356361

0 commit comments

Comments
 (0)