Ask Your Question
3

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

asked 2012-06-14 15:55:30 +0100

kcrisman gravatar image

updated 2012-06-14 15:58:52 +0100

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?

edit retag flag offensive close merge delete

Comments

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

John Palmieri gravatar imageJohn Palmieri ( 2012-06-19 02:45:48 +0100 )edit

See [here](http://www.sagemath.org/doc/faq/faq-usage.html#i-have-type-issues-using-scipy-cvxopt-or-numpy-from-sage).

bk322 gravatar imagebk322 ( 2012-06-19 06:39:28 +0100 )edit

@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 gravatar imagekcrisman ( 2012-06-19 09:35:55 +0100 )edit

3 Answers

Sort by ยป oldest newest most voted
3

answered 2012-06-14 15:57:23 +0100

kcrisman gravatar image

updated 2012-06-14 15:57:58 +0100

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.

edit flag offensive delete link more

Comments

or the shorter: matches.group(1r)

vdelecroix gravatar imagevdelecroix ( 2013-08-27 23:51:23 +0100 )edit
4

answered 2012-06-18 23:49:01 +0100

benjaminfjones gravatar image

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'
edit flag offensive delete link more
1

answered 2013-08-27 23:30:53 +0100

nbruin gravatar image

updated 2013-08-27 23:38:10 +0100

To some extent, this might be a flaw in python's interface. There's an ambiguity to what matches.group is supposed to do:

matches.group(i)

should have the same effect as

matches.groups()[i]

or as

matches.groupdict()[i]

depending on the nature of i. As it happens, the C code decides on whether i passes PyInt_Check (i.e., really is an integer), so if i is Integer(1) then i is interpreted as a key into explicitly named groups. Unfortunately, the rules are a little strict for what the names of named groups can be, so if i is an Integer, it will never be valid as such:

sage: matches = re.match("(?:foo)(?P<1>bar)(?P<2>sws)?",ST)
error: bad character in group name

It's documented that group names must follow the same rules as valid python identifiers. Valid use is:

sage: matches = re.match("(?:foo)(?P<a>bar)(?P<b>sws)?",ST)
sage: matches.group('a')
'bar'

so the problem is a muddled interface: matches.group(i) must choose whether to try to interpret i as an integer or as a string. There is of course no good answer if i is neither and indeed, the code hardly tries anything.

In the last example, matches.group(i) should work for any i that compares equal to a or b and hashes consistently with that.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2012-06-14 15:55:30 +0100

Seen: 1,561 times

Last updated: Aug 27 '13