sys.setrecursionlimit() and regular expressions

S

Santiago Caracol

Hello,

in my program I use recursive functions. A recursion limit of 10 would
be by far sufficient. Yet, I also use some (not very complicated)
regular expressions, which only compile if I set the recursion limit
to 100, or so. This means, of course, an unnecessary loss of speed.

Can I use one recursion limit for my functions and another one for the
re module?

Santiago
 
P

Peter Otten

Santiago said:
in my program I use recursive functions. A recursion limit of 10 would
be by far sufficient. Yet, I also use some (not very complicated)
regular expressions, which only compile if I set the recursion limit
to 100, or so. This means, of course, an unnecessary loss of speed.

Why do you think so? The recursion limit has no effect on the speed of your
script. It's just a number that the interpreter checks against.

Peter
 
S

Santiago Caracol

Why do you think so? The recursion limit has no effect on the speed of your
script. It's just a number that the interpreter checks against.

Yes, sorry. I was just about to explain that. The 'of course' in my
post was silly.

In MY program, the recursion limit is relevant for performance,
because I use constructs of the following kind:

def analyze(sentence):
try:
...
except RuntimeError:
...

I.e. I try to apply certain recursive rules to a sentence. If this
doesn't lead to any results (up to recursion limit N),
then I skip the analysis.
 
P

Peter Otten

Santiago said:
Yes, sorry. I was just about to explain that. The 'of course' in my
post was silly.

In MY program, the recursion limit is relevant for performance,
because I use constructs of the following kind:

def analyze(sentence):
try:
...
except RuntimeError:
...

I.e. I try to apply certain recursive rules to a sentence. If this
doesn't lead to any results (up to recursion limit N),
then I skip the analysis.

Here's a depth limitation decorator that you can apply selectively. In the
example below you can have as many gamma() calls as the interpreter's
recursion limit permits, but only a total of 10 calls of alpha() and beta().

import random

class TooDeep(Exception):
pass

_limit = 10

def limited(f):
def g(*args, **kw):
global _limit
if not _limit:
raise TooDeep
_limit -= 1
try:
return f(*args, **kw)
finally:
_limit += 1
return g

@limited
def alpha(n):
print "alpha", n
beta(n+1)

@limited
def beta(n):
print "beta", n
gamma(n+1)

def gamma(n):
print "gamma", n
random.choice([alpha, beta, gamma])(n+1)

if __name__ == "__main__":
try:
alpha(0)
except TooDeep as e:
print e

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top