[Edu-sig] self within re.sub
Bruno Vernier
vernier@vc.bc.ca
Tue, 6 Feb 2001 15:06:10 -0800
Hello again,
I am stuck with this issue while building the python embeded wiki pages:
how do I pass "self" to the method in re.sub and keep the match variable?
here is the simplified code:
rpython = rexec.REexec()
sub process(match):
ctag = match.groups()
# process here requires stuff from self,
# namely rpython.r_eval(content of py element)
return #evaluated python or latex snippets
pattern = '(py|python|latex|sql)'
tag = re.compile(pattern)
text = "<py>x=3;y=2</py> <latex>\x</latex> <py>x=4</py> <latex>\x</latex>"
text = tag.sub(process,text)
THE PROBLEM is that last line. The above works but if I want to avoid GLOBAL
variables (which I do because this is in a zope environment and if I use
globals, simultaneous connections cause the variable to step over itself), I
need to pass SELF to process.
if I use:
text(tag,process(self or any arguments),text))
then that works too but I lose the match variable. In other words, I am
forced to choose between the match or any other arguments.
Why can't I have them both? I've struggled with this for 2 month, searched
high and low on the internet and found vaguely similar problems being described
but no solution proposed.
p.s. I know I can use re.sub("pattern",method(arguments),text) but this is
not suitable because the order of processing matters in this case ... so I
need it all in one method. Am I making sense? The above snippet needs to
display a 3 and then a 4. Doing it with re.sub would cause a 4 and then a 4.
explanation: I want to parse <python> elements and then display the
variables processed by a restricted python environment into <latex> elements
by re-using the same REexec object (which is accessible only via self)
it works well with global variables on
http://ess.vancouver.bc.ca/zope/bruno/eduml but not when many students are
accessing these zope pages at the same time ... because they somehow share
these global variables for a split second.
Bruno