[Tutor] Really miss the C preprocessor!!

Gil Cosson gilcosson_2000 at yahoo.com
Sat Mar 6 06:13:49 CET 2010


I have some code to perform an exponential backoff. I am interacting with a few different web services. Checking for a specific Exception and then re-invoking the failed call after waiting a bit. I don't want to  code the backoff in ten different places. Really miss the C pre-processor.

I am tempted to try something like the following, but I understand that eval should be a last resort.

Any pythonic best practice for this situation?:

from  time import sleep
from random import randint

def exponentialbackoff(exception,numretrys,somecode):
    currentIteration=0
    SuccessfulCall=False
    rc=None
    globalnamespace=getglobals(,globals)
    while currentiteration<numretrys:
        try:
             rc=eval(somecode,globalnamespace)
            SuccessfulCall=True
            break
        except exception:
            currentiteration = currentiteration+1
            sleepytime=randint(0,4**currentiteration)
            sleep(sleepytime)
    return (SuccessfulCall,rc)
---

Gil Cosson

Bremerton, Washington

360 620 0431


      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100305/8e64f335/attachment.html>


More information about the Tutor mailing list