8000 Fixes #5 by moving the list comprehension conditional to a ternary · davidblewett/rure-python@568b62e · GitHub
[go: up one dir, main page]

Skip to content

Commit 568b62e

Browse files
committed
Fixes #5 by moving the list comprehension conditional to a ternary
expression, so that non-matching groups are present but default to None.
1 parent 86c7c9f commit 568b62e

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

rure/lib.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,8 @@ def captures(self, haystack, start=0):
213213
):
214214
return self.capture_cls(*[
215215
RureMatch(match.start, match.end)
216+
if _lib.rure_captures_at(captures, i, match) else None
216217
for i in range(0, _lib.rure_captures_len(captures))
217-
if _lib.rure_captures_at(captures, i, match)
218218
])
219219

220220
@accepts_bytes
@@ -235,8 +235,8 @@ def captures_iter(self, haystack, start=0):
235235
captures):
236236
yield self.capture_cls(*[
237237
RureMatch(match.start, match.end)
238+
if _lib.rure_captures_at(captures, i, match) else None
238239
for i in range(0, _lib.rure_captures_len(captures))
239-
if _lib.rure_captures_at(captures, i, match)
240240
])
241241

242242
@accepts_bytes

rure/tests/test_regexobject.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33
from __future__ import print_function
4+
import re
45
import unittest
56

67
import rure
@@ -11,11 +12,16 @@ def test_search(self):
1112
ptn = u"(re).*(ger)"
1213
email = u"tony@tiremove_thisger.net"
1314
results = rure.search(ptn, email)
15+
stdlib_results = re.search(ptn, email)
1416
self.assertIsNotNone(results)
15-
self.assertEqual(len(results.groups()), 2)
17+
self.assertEqual(len(results.groups()),
18+
len(stdlib_results.groups()))
1619

1720
ptn = u"(re)|(ger)"
1821
results = rure.search(ptn, email)
22+
stdlib_results = re.search(ptn, email)
1923
self.assertIsNotNone(results)
20-
self.assertEqual(len(results.groups()), 1)
21-
self.assertItemsEqual(results.group(0), (u're',))
24+
self.assertEqual(len(results.groups()),
25+
len(stdlib_results.groups()))
26+
self.assertItemsEqual(results.group(0),
27+
stdlib_results.group(0))

0 commit comments

Comments
 (0)
0