Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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)
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)
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})
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)
@apply_settings
def given_settings(self, settings=None):
self.settings = settings
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)
@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:
@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:
@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))
@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))