How to use the dateparser.conf.apply_settings function in dateparser

To help you get started, we’ve selected a few dateparser examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github scrapinghub / dateparser / tests / test_settings.py View on Github external
def test_apply_settings_should_return_default_settings_when_no_settings_are_supplied_to_the_decorated_function(self):
        test_func = apply_settings(test_function)
        self.assertEqual(test_func(), self.default_settings)
github scrapinghub / dateparser / tests / test_settings.py View on Github external
def test_apply_settings_should_not_create_new_settings_when_same_settings_are_supplied_to_the_decorated_function_more_than_once(self):
        test_func = apply_settings(test_function)
        settings_once = test_func(settings={'PREFER_DATES_FROM': 'past'})
        settings_twice = test_func(settings={'PREFER_DATES_FROM': 'past'})
        self.assertEqual(settings_once, settings_twice)
github scrapinghub / dateparser / tests / test_settings.py View on Github external
def test_error_is_raised_when_none_is_passed_in_settings(self):
        test_func = apply_settings(test_function)
        with self.assertRaisesRegexp(TypeError, 'Invalid.*None\}'):
            test_func(settings={'PREFER_DATES_FROM': None})

        with self.assertRaisesRegexp(TypeError, 'Invalid.*None\}'):
            test_func(settings={'TIMEZONE': None})

        with self.assertRaisesRegexp(TypeError, 'Invalid.*None\}'):
            test_func(settings={'TO_TIMEZONE': None})
github scrapinghub / dateparser / tests / test_settings.py View on Github external
def test_apply_settings_should_return_default_settings_when_called_with_no_settings_after_once_called_with_settings_supplied_to_the_decorated_function(self):
        test_func = apply_settings(test_function)
        settings_once = test_func(settings={'PREFER_DATES_FROM': 'past'})
        settings_twice = test_func()
        self.assertNotEqual(settings_once, self.default_settings)
        self.assertEqual(settings_twice, self.default_settings)
github scrapinghub / dateparser / tests / test_languages.py View on Github external
    @apply_settings
    def given_settings(self, settings=None):
        self.settings = settings
github scrapinghub / dateparser / tests / test_settings.py View on Github external
def test_apply_settings_should_return_non_default_settings_when_settings_are_supplied_to_the_decorated_function(self):
        test_func = apply_settings(test_function)
        self.assertNotEqual(test_func(settings={'PREFER_DATES_FROM': 'past'}), self.default_settings)
github scrapinghub / dateparser / dateparser / date_parser.py View on Github external
    @apply_settings
    def parse(self, date_string, settings=None):
        date_string = six.text_type(date_string)

        if not date_string.strip():
            raise ValueError("Empty string")

        date_string = strip_braces(date_string)
        date_string, ptz = pop_tz_offset_from_string(date_string)

        date_obj, period = parse(date_string, settings=settings)

        _settings_tz = settings.TIMEZONE.lower()

        if ptz:
            date_obj = ptz.localize(date_obj)
            if 'local' not in _settings_tz:
github scrapinghub / dateparser / dateparser / search / search.py View on Github external
    @apply_settings
    def search_dates(self, text, languages=None, settings=None):
        """
        Find all substrings of the given string which represent date and/or time and parse them.

        :param text:
            A string in a natural language which may contain date and/or time expressions.
        :type text: str|unicode
        :param languages:
            A list of two letters language codes.e.g. ['en', 'es']. If languages are given, it will not attempt
            to detect the language.
        :type languages: list
        :param settings:
               Configure customized behavior using settings defined in :mod:`dateparser.conf.Settings`.
        :type settings: dict

        :return: a dict mapping keys to two letter language code and a list of tuples of pairs:
github scrapinghub / dateparser / dateparser / date.py View on Github external
    @apply_settings
    def __init__(self, languages=None, locales=None, region=None, try_previous_locales=True,
                 use_given_order=False, settings=None):

        if not isinstance(languages, (list, tuple, Set)) and languages is not None:
            raise TypeError("languages argument must be a list (%r given)" % type(languages))

        if not isinstance(locales, (list, tuple, Set)) and locales is not None:
            raise TypeError("locales argument must be a list (%r given)" % type(locales))

        if not isinstance(region, six.string_types) and region is not None:
            raise TypeError("region argument must be str or unicode (%r given)" % type(region))

        if not isinstance(try_previous_locales, bool):
            raise TypeError("try_previous_locales argument must be a boolean (%r given)"
                            % type(try_previous_locales))
github scrapinghub / dateparser / dateparser / search / text_detection.py View on Github external
    @apply_settings
    def _best_language(self, date_string,  settings=None):
        self.character_check(date_string, settings)
        date_string = normalize_unicode(date_string.lower())
        if len(self.languages) == 1:
            return self.languages[0].shortname
        applicable_languages = []
        for language in self.languages:
            num_words = language.count_applicability(
                date_string, strip_timezone=False, settings=settings)
            if num_words[0] > 0 or num_words[1] > 0:
                applicable_languages.append((language.shortname, num_words))
            else:
                num_words = language.count_applicability(
                    date_string, strip_timezone=True, settings=settings)
                if num_words[0] > 0 or num_words[1] > 0:
                    applicable_languages.append((language.shortname, num_words))