Skip to content

Commit

Permalink
SUBSTITUTION pattern does not keep scope. fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ranchodeluxe committed Feb 26, 2016
1 parent e22a025 commit 31d3da7
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
11 changes: 5 additions & 6 deletions datefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ class DateFinder(object):

## These tokens can be in original text but dateutil
## won't handle them without modification
SUBSTITUTION_CHAR = " $ "
REPLACEMENTS = dict( [(key, SUBSTITUTION_CHAR) for key in EXTRA_TOKENS_PATTERN.split("|") if key not in ['t','z']] )
REPLACEMENTS = dict( [(key, " $ ") for key in EXTRA_TOKENS_PATTERN.split("|") if key not in ['t','z']] )

TIMEZONE_REPLACEMENTS = {
"pacific": "PST",
Expand All @@ -118,7 +117,7 @@ class DateFinder(object):
def find_dates(self, text, source=False, index=False, strict=False):

lazy_stack = []
extracted_date_iter = iter(self.extract_date_strings(text, strict=strict))
extracted_date_iter = self.extract_date_strings(text, strict=strict)

try:
lazy_stack.append( next(extracted_date_iter) )
Expand Down Expand Up @@ -218,9 +217,9 @@ def parse_date_string(self, date_string, captures, strict=False):
## return from here and add results to the lazy_stack in previous call
## then these results will be processed again one at a time
return second_pass_matches
elif len(second_pass_matches) == 1:
## replace our substitution pattern
date_string = re.sub(self.SUBSTITUTION_CHAR, " ", second_pass_matches[0][0])

## replace our substitution pattern
date_string = re.sub(r'\$', " ", date_string)

## Match strings must be at least 3 characters long
## < 3 tends to be garbage
Expand Down
2 changes: 1 addition & 1 deletion tests/test_find_and_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

@pytest.mark.parametrize('date_string, expected_replaced_string, captures, expected_tz_string', [
('due on Tuesday Jul 22, 2014 eastern standard time',
' tuesday jul 22, 2014 eastern ',
' $ $ tuesday jul 22, 2014 eastern $ $ ',
{'timezones':['EST']},
'EST',
)
Expand Down
8 changes: 4 additions & 4 deletions tests/test_parse_date_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@
@pytest.mark.parametrize('date_string, expected_parse_arg, expected_captures, expected_date', [
(
'due on Tuesday Jul 22, 2014 eastern standard time',
'tuesday jul 22, 2014',
' tuesday jul 22, 2014 ',
{ 'timezones': ['eastern'] },
datetime(2014, 7, 22).replace(tzinfo=tz.gettz('EST'))
),
(
'Friday pAcific stanDard time',
'friday',
'friday ',
{ 'timezones': ['pacific'] },
parser.parse('friday').replace(tzinfo=tz.gettz('PST'))
),
(
'13/03/2014 Central Daylight Savings Time',
'13/03/2014',
'13/03/2014 ',
{ 'timezones': ['central'] },
datetime(2014, 3, 13).replace(tzinfo=tz.gettz('CST'))
),
Expand All @@ -35,7 +35,7 @@
# and returns naive datetime objects
(
' on 12/24/2015 at 2pm ',
'12/24/2015 at 2pm',
' 12/24/2015 2pm',
{ 'timezones': [] },
datetime(2015, 12, 24, 14, 0)
),
Expand Down

0 comments on commit 31d3da7

Please sign in to comment.