[Tutor] Really miss the C preprocessor!!

2010-03-05 Thread Gil Cosson

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 currentiterationnumretrys:
        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


  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help Needed

2009-06-16 Thread Gil Cosson
I just stumbled on this

Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] on
win32
Type help, copyright, credits or license for more information.
 s=hello world
 s=s[::-1]
 print s
dlrow olleh


---

Gil Cosson

Bremerton, Washington

360 620 0431

--- On Mon, 6/15/09, Wayne sri...@gmail.com wrote:

From: Wayne sri...@gmail.com
Subject: Re: [Tutor] Help Needed
To: Raj Medhekar cosmicsan...@yahoo.com
Cc: Python Tutor tutor@python.org
Date: Monday, June 15, 2009, 5:28 PM

On Mon, Jun 15, 2009 at 3:00 PM, Raj Medhekar cosmicsan...@yahoo.com wrote:


I am looking to build a program that gets a message from the user and then 
prints it backward. I 've been at it for hours now but I can't seem to figure 
it out. I've been having trouble trying to index the message so I can print it 
out backwards. I would've posted my code but I really haven't gotten past the  
'raw_input(Enter a message: )' command line.  Any help is gladly appreciated. 
Thanks!


Python treats a string like a list/array.
In [1]: mystr = I'm not a witch!

In [2]: mystr[0]
Out[2]: 'I'

Same goes for one that's defined with raw_input:



In [4]: mystr = raw_input(She turned you into a newt? )
She turned you into a newt? I got better!

In [5]: mystr[0] + mystr[1:]
Out[5]: 'I got better!'

Python has a slice operator the colon, so mystr[0] gives me the character at 0, 
and mystr[1:] gives me everything from the first character to the end (if you 
add something after the colon it will only return whatever is up to that point:



In [10]: mystr[2:5]
Out[10]: 'got'

If you know what a loop is, you should be able to figure out how to get the 
result you're looking for.

HTH,
Wayne








-Inline Attachment Follows-

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor



  ___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor