From b8723db9e1bde6af9d0d41bd8b939f15b654c813 Mon Sep 17 00:00:00 2001 From: Anton Sarukhanov <code@ant.sr> Date: Wed, 11 Dec 2019 22:04:06 -0500 Subject: [PATCH] Exclude redundant and likely-wrong formats. --- whatformat/util/whatdateformat.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/whatformat/util/whatdateformat.py b/whatformat/util/whatdateformat.py index 0fc5eb3..e069bcb 100644 --- a/whatformat/util/whatdateformat.py +++ b/whatformat/util/whatdateformat.py @@ -1,6 +1,7 @@ """Function(s) for getting a format string from a date string.""" import re +from copy import copy from itertools import product from collections import defaultdict, Counter from typing import Dict, Iterator, List @@ -15,9 +16,11 @@ FORMAT_DIRECTIVES = [ DATE_TOKEN_REGEX = re.compile(r'([\w]+)|([\W]+)') -# pylint: disable=too-many-locals -def get_all_format_strings(date_string, allow_reuse=False, - no_zero=False) -> Iterator[str]: +# pylint: disable=too-many-locals,too-many-branches +def _get_all_format_strings(date_string, allow_reuse=False, + include_day_of_year=False, + include_week_number=False, + no_zero=False) -> Iterator[str]: """Yield date format strings which match date_string.""" try: parsed_datetime = parse(date_string) @@ -26,7 +29,14 @@ def get_all_format_strings(date_string, allow_reuse=False, return tokens = DATE_TOKEN_REGEX.findall(date_string) possible_matches = defaultdict(list) # type: Dict[str, List[str]] - for test_format in FORMAT_DIRECTIVES: + format_directives = copy(FORMAT_DIRECTIVES) + if not include_day_of_year: + format_directives.remove('%-j') + format_directives.remove('%j') + if not include_week_number: + for directive in ('%W', '%w', '%U'): + format_directives.remove(directive) + for test_format in format_directives: test_string = parsed_datetime.strftime(test_format) possible_matches[test_string.lower()].append(test_format) possible_parts = [] @@ -46,7 +56,7 @@ def get_all_format_strings(date_string, allow_reuse=False, if not allow_reuse: counts = Counter(sequence) max_count = max( - counts[diretive] for diretive in FORMAT_DIRECTIVES) + counts[diretive] for diretive in format_directives) if max_count > 1: continue format_string = "".join(sequence) @@ -77,4 +87,4 @@ def _remove_extra_nlz(combinations) -> Iterator[List[str]]: def get_unique_format_strings(date_string, allow_reuse=False) -> List[str]: """Return date format strings matching date_string, without duplicates.""" - return list(set(get_all_format_strings(date_string, allow_reuse))) + return list(set(_get_all_format_strings(date_string, allow_reuse))) -- GitLab