FAQ
Hello

As a newbie, it's pretty likely that there's a smarter way to do this,
so I'd like to check with the experts:

I need to try calling a function 5 times. If successful, move on; If
not, print an error message, and exit the program:

=====
success = None

for i in range(5):
#Try to fetch public IP
success = CheckIP()
if success:
break

if not success:
print "Exiting."
sys.exit()
=====

Thank you.

Search Discussions

  • Tim Chase at Nov 19, 2008 at 2:23 pm

    I need to try calling a function 5 times. If successful, move on; If
    not, print an error message, and exit the program:

    success = None
    for i in range(5):
    #Try to fetch public IP
    success = CheckIP()
    if success:
    break
    if not success:
    print "Exiting."
    sys.exit()
    Though a bit of an abuse, you can use

    if not any(CheckIP() for _ in range(5)):
    print "Exiting"
    sys.exit()

    (this assumes Python2.5, but the any() function is easily
    recreated per the docs at [1]; and also assumes the generator
    creation of 2.4, so this isn't as useful in 2.3 and below)

    Alternatively, you can use the for/else structure:

    for i in range(5):
    if CheckIP():
    break
    else:
    print "Exiting"
    sys.exit()

    -tkc

    [1]
    http://www.python.org/doc/2.5.2/lib/built-in-funcs.html#l2h-10





    .
  • Boris Borcic at Nov 20, 2008 at 5:45 pm

    Tim Chase wrote:

    success = None
    for i in range(5):
    #Try to fetch public IP
    success = CheckIP()
    if success:
    break
    if not success:
    print "Exiting."
    sys.exit()
    Though a bit of an abuse, you can use

    if not any(CheckIP() for _ in range(5)):
    print "Exiting"
    sys.exit()
    I don't see why you speak of abuse, bit of abuse would be, say if you replaced
    range(5) by '12345' to win a char; but otoh I think you misspelled any() for all().

    Cheers BB
  • Tim Chase at Nov 20, 2008 at 6:13 pm

    success = None
    for i in range(5):
    #Try to fetch public IP
    success = CheckIP()
    if success:
    break
    if not success:
    print "Exiting."
    sys.exit()
    Though a bit of an abuse, you can use

    if not any(CheckIP() for _ in range(5)):
    print "Exiting"
    sys.exit()
    I don't see why you speak of abuse, bit of abuse would be, say if you replaced
    range(5) by '12345' to win a char; but otoh I think you misspelled any() for all().
    The OP's code break'ed (broke?) upon the first success, rather
    than checking all of them. Thus, it would be any() rather than
    all(). Using all() would require 5 successful calls to
    CheckIP(), rather than one-out-of-five successful calls.

    As for abuse, the "for _ in iterable" always feels a little hokey
    to me. It works, but feels warty.

    -tkc
  • Boris Borcic at Nov 20, 2008 at 9:18 pm

    Tim Chase wrote:
    success = None
    for i in range(5):
    #Try to fetch public IP
    success = CheckIP()
    if success:
    break
    if not success:
    print "Exiting."
    sys.exit()
    Though a bit of an abuse, you can use

    if not any(CheckIP() for _ in range(5)):
    print "Exiting"
    sys.exit()
    I don't see why you speak of abuse, bit of abuse would be, say if you
    replaced range(5) by '12345' to win a char; but otoh I think you
    misspelled any() for all().
    The OP's code break'ed (broke?) upon the first success, rather than
    checking all of them. Thus, it would be any() rather than all(). Using
    all() would require 5 successful calls to CheckIP(), rather than
    one-out-of-five successful calls.
    Right. So it could also be written " if all(not CheckIP()... ". Perhaps more
    closely re-telling the OP's ?
    As for abuse, the "for _ in iterable" always feels a little hokey to
    me. It works, but feels warty.
    I guess this means you did not learn Prolog before Python ?

    Cheers, BB
  • Nicholas Ferenc Fabry at Nov 19, 2008 at 2:31 pm

    On Nov 19, 2008, at 09:09, Gilles Ganault wrote:

    Hello

    As a newbie, it's pretty likely that there's a smarter way to do this,
    so I'd like to check with the experts:

    I need to try calling a function 5 times. If successful, move on; If
    not, print an error message, and exit the program:

    =====
    success = None

    for i in range(5):
    #Try to fetch public IP
    success = CheckIP()
    if success:
    break

    if not success:
    print "Exiting."
    sys.exit()
    =====
    A little simpler:

    for i in range(5):
    if CheckIP():
    break
    else:
    print "Exiting."
    sys.exit()

    The else part will only fire if the for finishes without breaking.
    Hope this helps a bit...


    Nick Fabry
  • Sion Arrowsmith at Nov 19, 2008 at 2:37 pm

    Gilles Ganault wrote:
    As a newbie, it's pretty likely that there's a smarter way to do this,
    so I'd like to check with the experts:

    I need to try calling a function 5 times. If successful, move on; If
    not, print an error message, and exit the program:

    =====
    success = None

    for i in range(5):
    #Try to fetch public IP
    success = CheckIP()
    if success:
    break

    if not success:
    print "Exiting."
    sys.exit()
    =====
    for i in range(5):
    if CheckIP():
    break
    else:
    print "Exiting."
    sys.exit()

    Note very carefully that the "else" goes with the "for" and not the "if".

    --
    \S -- siona at chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
    "Frankly I have no feelings towards penguins one way or the other"
    -- Arthur C. Clarke
    her nu become? se bera eadward ofdun hl?ddre heafdes b?ce bump bump bump
  • Gilles Ganault at Nov 19, 2008 at 3:21 pm

    On 19 Nov 2008 14:37:06 +0000 (GMT), Sion Arrowsmith wrote:
    Note very carefully that the "else" goes with the "for" and not the "if".
    Thanks guys.
  • George Sakkis at Nov 19, 2008 at 4:05 pm

    On Nov 19, 10:21?am, Gilles Ganault wrote:

    On 19 Nov 2008 14:37:06 +0000 (GMT), Sion Arrowsmith

    wrote:
    Note very carefully that the "else" goes with the "for" and not the "if".
    Thanks guys.
    And if you end up doing this for several different functions, you can
    factor it out with the following decorator:

    class MaxRetriesExceededError(Exception):
    pass

    def retry(n):
    def decorator(f):
    def wrapper(*args, **kwds):
    for i in xrange(n):
    r = f(*args, **kwds)
    if r: return r
    raise MaxRetriesExceededError
    return wrapper
    return decorator

    If the number of retries is fixed and known at "compile" time, you can
    use the standard decorator syntax:

    @retry(5)
    def CheckIP():
    ...

    If not, just decorate it explicitly at runtime:

    def CheckIP():
    ...

    n = int(raw_input('Give number of retries:'))
    CheckIP = retry(n)(CheckIP)


    HTH,
    George
  • Steve Holden at Nov 20, 2008 at 6:28 pm

    Gilles Ganault wrote:
    Hello

    As a newbie, it's pretty likely that there's a smarter way to do this,
    so I'd like to check with the experts:

    I need to try calling a function 5 times. If successful, move on; If
    not, print an error message, and exit the program:

    =====
    success = None

    for i in range(5):
    #Try to fetch public IP
    success = CheckIP()
    if success:
    break

    if not success:
    print "Exiting."
    sys.exit()
    Use the for statement's "else" clause: it's there to allow you to
    specify code to be executed only when the loop terminates normally.

    for i in range(5):
    if CheckIP():
    break
    else:
    sys.exit("Could not verify IP address")
    ... remainder of program ...

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC http://www.holdenweb.com/
  • Banibrata Dutta at Nov 20, 2008 at 6:35 pm

    On Thu, Nov 20, 2008 at 11:58 PM, Steve Holden wrote:

    Gilles Ganault wrote:
    Hello

    As a newbie, it's pretty likely that there's a smarter way to do this,
    so I'd like to check with the experts:

    I need to try calling a function 5 times. If successful, move on; If
    not, print an error message, and exit the program:

    =====
    success = None

    for i in range(5):
    #Try to fetch public IP
    success = CheckIP()
    if success:
    break

    if not success:
    print "Exiting."
    sys.exit()
    Use the for statement's "else" clause: it's there to allow you to
    specify code to be executed only when the loop terminates normally.

    for i in range(5):
    if CheckIP():
    break
    else:
    sys.exit("Could not verify IP address")
    ... remainder of program ...
    and possibly use 'xrange(5)' instead of 'range(5)' ...

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedNov 19, '08 at 2:09p
activeNov 20, '08 at 9:18p
posts11
users8
websitepython.org

People

Translate

site design / logo © 2022 Grokbase