def test_no_spaces_strict_parsing(date_string, expected_result): parser = DateDataParser(settings={ 'PARSERS': ['no-spaces-time'], 'STRICT_PARSING': False }) assert parser.get_date_data(date_string)['date_obj'] == expected_result parser = DateDataParser(settings={ 'PARSERS': ['no-spaces-time'], 'STRICT_PARSING': True }) assert parser.get_date_data(date_string)['date_obj'] is None
def get_mount_number(name: str) -> int: """ Get month number by name Use `dateparser` for cross-platform solution. Develop on Mac os, use on Windows Because native solution have different name in module calendar, for example on Windows `Январь` on Mac Os `января` """ ddp = DateDataParser(languages=['ru']) date_data = ddp.get_date_data(f'1 {name}') return date_data.date_obj.month
class InputParser: LANGUAGES = ["en"] DATE_PARSER_SETTINGS = { "STRICT_PARSING": False, "NORMALIZE": True, "RETURN_AS_TIMEZONE_AWARE": True, "PREFER_DATES_FROM": "past", } def __init__(self): self._date_parser = DateDataParser(languages=self.LANGUAGES, settings=self.DATE_PARSER_SETTINGS) def _as_datetime(self, date_time): dt_obj = self._date_parser.get_date_data(date_time) if dt_obj: return dt_obj["date_obj"] def parse_datetime(self, date_time): dt = date_time.strip() if not dt: return None parsed_dt = self._as_datetime(dt) if not parsed_dt: raise InvalidDateTimeError if parsed_dt > self._as_datetime("now"): raise DateTimeInFutureError return parsed_dt def parse(self, work_desc): if "@" in work_desc: work, _, date_time = work_desc.rpartition("@") else: work, date_time = (work_desc, "") work = work.strip() if not work: raise InvalidWorkError date_time = self.parse_datetime(date_time) return work, date_time
def test_confidence_threshold_setting_is_applied(): ddp = DateDataParser(detect_languages_function=detect_languages, settings={'LANGUAGE_DETECTION_CONFIDENCE_THRESHOLD': 0.6}) assert ddp.get_date_data('21/06/2020').locale == 'en' ddp2 = DateDataParser(detect_languages_function=detect_languages, settings={'LANGUAGE_DETECTION_CONFIDENCE_THRESHOLD': 0.4}) assert ddp2.get_date_data('21/06/2020').locale == 'fr'