Message256030
I've been playing around with this. My implementation is basically the naive:
def __getitem__(self, value):
return self.group(value)
I have the following tests passing:
def test_match_getitem(self):
pat = re.compile('(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?')
m = pat.match('a')
self.assertEqual(m['a1'], 'a')
self.assertEqual(m['b2'], None)
self.assertEqual(m['c3'], None)
self.assertEqual(m[0], 'a')
self.assertEqual(m[1], 'a')
self.assertEqual(m[2], None)
self.assertEqual(m[3], None)
with self.assertRaises(IndexError):
m['X']
m = pat.match('ac')
self.assertEqual(m['a1'], 'a')
self.assertEqual(m['b2'], None)
self.assertEqual(m['c3'], 'c')
self.assertEqual(m[0], 'ac')
self.assertEqual(m[1], 'a')
self.assertEqual(m[2], None)
self.assertEqual(m[3], 'c')
with self.assertRaises(IndexError):
m['X']
# Cannot assign.
with self.assertRaises(TypeError):
m[0] = 1
# No len().
self.assertRaises(TypeError, len, m)
But because I'm just calling group(), you'll notice a few oddities. Namely:
- indexing with integers works, as would group(i). See the discussion of omitting len().
- for defined but missing group names, you get None instead of KeyError
- for invalid group names, you get IndexError instead of KeyError
I can't decide if these are good (because they're consistent with group()), or bad (because they're surprising. I'm interested in your opinions.
I'll attach the patch when I'm at a better computer. |
|
Date |
User |
Action |
Args |
2015-12-06 20:54:29 | eric.smith | set | recipients:
+ eric.smith, barry, rhettinger, ezio.melotti, mrabarnett, serhiy.storchaka |
2015-12-06 20:54:29 | eric.smith | set | messageid: <1449435269.76.0.456221508117.issue24454@psf.upfronthosting.co.za> |
2015-12-06 20:54:29 | eric.smith | link | issue24454 messages |
2015-12-06 20:54:29 | eric.smith | create | |
|