sys.setrecursionlimit() and regular expressions
Peter Otten
__peter__ at web.de
Thu Sep 30 08:10:38 EDT 2010
Santiago Caracol wrote:
>
>> 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.
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
More information about the Python-list
mailing list