Ask Your Question
1

re.complile failure in a SageWS (Vertical Bar character)

asked 2015-02-17 07:29:24 +0100

I was trying to use a Python Cookbook recipe (https://www.safaribooksonline.com/lib...) for multiple regular expression matches and substitutions in a Sagemath Cloud Sage Worksheet (final usage is for altering the __latex_/_repr_ attributes of an object). The code from the cookbook is below:

import re
def multiple_replace(txt, adict):
    rx = re.compile('|'.join(map(re.escape, adict)))
    def one_xlat(match):
        return adict[match.group(0)]
    return rx.sub(one_xlat, txt)


txt = "Larry Wall is the creator of Perl"
adict = {
  "Larry Wall" : "Guido van Rossum",
  "creator" : "Benevolent Dictator for Life",
  "Perl" : "Python",
}

print(multiple_replace(txt, adict))

When I run this code in the Worksheet I get the error:

Error in lines 13-13
Traceback (most recent call last):
  File "/projects/PROJECT/sagemathcloud/sage_server.py", line 873, in execute
    exec compile(block+'\n', '', 'single') in namespace, locals
  File "", line 1, in <module>
  File "", line 5, in multiple_replace
  File "", line 4, in one_xlat
IndexError: no such group

If I run the same code on a Sagemath Cloud IPython Notebook, Windows/Linux Python 2.7 session, or Windows/Linux Python 3.4 session the code outputs the correct result.

I started debugging and found that Sage doesn't like the '|' in the compiled string. If I change the character to another (e.g. ',') I no longer get the error ... though the code no longer functions with anything other than the '|'. I also tried to substitute '|' with unichr(int('007c', 16)) which results in the same error.

Has anyone seen anything like this before? I tried to search for it, but it seems so niche I didn't find any results. Any ideas for a workaround?

Thanks Matt

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2015-02-17 14:11:23 +0100

William Stein gravatar image

Fortunately (?), this is a general sage problem (nothing to do with sagemathcloud). It's caused by the sage pre-parser making the 0 a Sage integer. Either use %python or int(0) or 0r to workaround it:

https://cloud.sagemath.com/projects/4...

edit flag offensive delete link more

Comments

Hmm, that is a general-use thing, though, I'm surprised I haven't been bitten by it before. How many other such places are there like that? Note that slice notation doesn't complain about a[:1] having a Sage Integer.

kcrisman gravatar imagekcrisman ( 2015-02-17 16:57:18 +0100 )edit

@William Stein Thanks for the quick response and showing multiple solutions to my issue! From the Traceback error I would have been chasing may tail unless you intervened.

@kcrisman Ya that's odd to me as well ... maybe someone can share when the preprocessor can and can't handle the coercion.

JesterEE gravatar imageJesterEE ( 2015-02-17 22:29:54 +0100 )edit

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

Asked: 2015-02-17 07:29:24 +0100

Seen: 812 times

Last updated: Feb 17 '15