Python thing that doesn't work in Sage, works in pure Python

i like this post (click again to cancel)
2
i dont like this post (click again to cancel)

In sage -python:

>>> import re
>>> ST = "foobarsws"
>>> matches = re.match("(?:foo)?(bar)?(sws)?",ST)
>>> matches.groups()
('bar', 'sws')
>>> matches.group(0)
'foobarsws'
>>> matches.group(1)
'bar'
>>> matches.group(2)
'sws'

In Sage:

sage: import re
sage: ST = "foobarsws"
sage: matches = re.match("(?:foo)?(bar)?(sws)?",ST)
sage: matches.groups()
('bar', 'sws')
sage: matches.group()
'foobarsws'
sage: matches.group(0)
IndexError: no such group
sage: matches.group(1)
IndexError: no such group
sage: matches.group(2)
IndexError: no such group

Any ideas?

asked Jun 14 '12

kcrisman gravatar image kcrisman
6784 14 67 152

updated Jun 14 '12

This one has gotten me, too. It's very annoying.

John Palmieri (Jun 19 '12)

See here.

bk322 (Jun 19 '12)

@bk322 - right, if I'd gotten that error I would have thought of this, but it was the IndexError that made it so weird. Because of course this error naturally appears anyway when you've tried to access a nonexistent group (like matches.group(3) would be above, I guess)

kcrisman (Jun 19 '12)

2 Answers:

i like this answer (click again to cancel)
3
i dont like this answer (click again to cancel) kcrisman has selected this answer as correct

Never mind.

sage: matches.group(int(1))
'bar'

Leaving this up for those who might stumble. I'll try to think of a better title for the question so people find it.

This seems bad, though. I suppose there isn't any way to notice when we are using "pure Python" commands and for the preparser to at least try not doing the whole Integer thing if it gets an Error of some kind? Maybe that would lead to more chaos than we'd like.

link

posted Jun 14 '12

kcrisman gravatar image kcrisman
6784 14 67 152

updated Jun 14 '12

i like this answer (click again to cancel)
3
i dont like this answer (click again to cancel)

For others that stumble across this question, you can turn the Sage preparser off by calling preparser(False). Then the code above works just fine at the Sage command line:

sage: preparser(False)
sage: import re
sage: ST = "foobarsws"
sage: matches = re.match("(?:foo)?(bar)?(sws)?",ST)
sage: matches.groups()
('bar', 'sws')
sage: matches.group()
'foobarsws'
sage: matches.group(0)
'foobarsws'
sage: matches.group(1)
'bar'
link

posted Jun 18 '12

benjaminfjones gravatar image benjaminfjones
2500 3 34 66
http://bfj7.com/

Your answer

Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
[hide preview]

Question tools

Tags:

Stats:

Asked: Jun 14 '12

Seen: 160 times

Last updated: Jun 18 '12

powered by ASKBOT version 0.7.22
Copyright Sage, 2010. Some rights reserved under creative commons license.