0

Is there a way to test when a program's recursion limit is reached? For example:

def funct():
    print "Hello"
    #Some code for testing the recursion limit.
        #Some more code to execute

If you are wondering why I want to know this, here is why: I want to hide an Easter egg in a program, but I can't do that until I found out how to do what I am asking. So, how can I test when the recursion limit is reached? Is there even a way?

4
  • 1
    Mind sharing with us why on earth you would want to test out that limit? Commented Apr 19, 2014 at 18:12
  • @Assaf_Lavie wait, what? I said the reason as to why I wanted to know this. Commented Apr 19, 2014 at 18:19
  • You want users to reach the easter egg? Or developers who read the code? I can't think of a good reason to actually allow production code to reach a state where it emits a stack overflow exception. Just use Konami code or something :) Commented Apr 19, 2014 at 18:28
  • @Assaf_Lavie it is the users who will reach the Easter egg. It would be something funny like printing a fake error message, or something like that. Commented Apr 19, 2014 at 20:28

2 Answers 2

4

You can test for the RuntimeError like any other error, with try:

def funct():
    ...
    try:
       funct()
    except RuntimeError as e:
        if "recursion" in e.message:
            print("Easter egg!")

Note that I've added an extra check that the error is warning about recursion, so you don't prevent any other RuntimeErrors from going about their business.

Sign up to request clarification or add additional context in comments.

Comments

0

Incase you are setting recursion measures on OpenAI here is the code snippet

from openai import OpenAI, RateLimitError
import time

api_key = "your_api_key_here"
client = OpenAI(api_key)

try:
    # Your OpenAI API request here
    response = client.embeddings.create(...)
except RateLimitError as e:
    print(f"Rate limit exceeded. Waiting for {e.reset} seconds.")
    time.sleep(e.reset)
    # Retry your request or handle accordingly

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.