Ask Your Question

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

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

I was trying to use a Python Cookbook recipe ( 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[]
    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/", 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

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

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:

edit flag offensive delete link more


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 +0200 )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 +0200 )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


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

Seen: 711 times

Last updated: Feb 17 '15