Skip to content
This repository has been archived by the owner on Mar 24, 2021. It is now read-only.

Commit

Permalink
Change regex match() to search()
Browse files Browse the repository at this point in the history
re.match() only matches from the beginning of the line, but sometimes
we have matchers that rely on the caret to differentiate between
the numerator and denominator.

This commit changes to re.search(), which does not start looking
at the beginning of the string.
  • Loading branch information
alexmuller committed Dec 17, 2014
1 parent 8cb1a34 commit d15c5e2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion backdrop/transformers/tasks/rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def group_by(keys, arr):


def pattern_filter(key, pattern, datum):
return key in datum and datum[key] and pattern.match(datum[key])
return key in datum and datum[key] and pattern.search(datum[key])


def sum_matching(data, matchKey, pattern, valueKey):
Expand Down
19 changes: 19 additions & 0 deletions tests/transformers/tasks/test_rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,42 @@
"_end_at": '2013-12-02T00:00:00+00:00',
"_start_at": '2013-11-25T00:00:00+00:00',
"eventCategory": 'start',
"channel": "non-digital",
"uniqueEvents:sum": 15.0
},
{
"_end_at": '2013-12-09T00:00:00+00:00',
"_start_at": '2013-12-02T00:00:00+00:00',
"eventCategory": 'start',
"channel": "non-digital",
"uniqueEvents:sum": 25.0
},
{
"_end_at": '2013-12-02T00:00:00+00:00',
"_start_at": '2013-11-25T00:00:00+00:00',
"eventCategory": 'confirm',
"channel": "digital",
"uniqueEvents:sum": 8.0
},
{
"_end_at": '2013-12-09T00:00:00+00:00',
"_start_at": '2013-12-02T00:00:00+00:00',
"eventCategory": 'confirm',
"channel": "digital",
"uniqueEvents:sum": 12.0
},
{
"_end_at": '2013-12-02T00:00:00+00:00',
"_start_at": '2013-11-25T00:00:00+00:00',
"eventCategory": 'done',
"channel": "another-channel",
"uniqueEvents:sum": 10.0
},
{
"_end_at": '2013-12-09T00:00:00+00:00',
"_start_at": '2013-12-02T00:00:00+00:00',
"eventCategory": 'done',
"channel": "another-channel",
"uniqueEvents:sum": None
}
]
Expand Down Expand Up @@ -68,3 +74,16 @@ def test_compute(self):
is_('2013-12-02T00:00:00+00:00'))
assert_that(transformed_data[0]['rate'], is_(2.0 / 3.0))
assert_that(transformed_data[1]['rate'], is_(None))

def test_regex_matching_supports_carets(self):
# The numeratorMatcher should match just "digital"
# The denominatorMatcher should match both "digital" and "non-digital"
transformed_data = compute(data, {
"denominatorMatcher": 'digital$',
"numeratorMatcher": '^digital$',
"matchingAttribute": 'channel',
"valueAttribute": 'uniqueEvents:sum',
})

assert_that(transformed_data[0]['rate'], is_(8.0 / (8.0 + 15.0)))
assert_that(transformed_data[1]['rate'], is_(12.0 / (12.0 + 25.0)))

0 comments on commit d15c5e2

Please sign in to comment.