network programming without goto (refactoring)

Jeff Shannon jeff at ccvcorp.com
Fri Nov 26 17:52:37 EST 2004


Roy Smith wrote:

>What you want to do is refactor this into a number of smaller functions, 
>each one encapsulating one piece of the puzzle.  To start, I'd build a 
>function which handled the multiple connection attempts to a given host.  
>Maybe something like this:
>
>#
># Untested code -- this is just to give you some ideas
># and get you thinking in the right direction
>#
>def connectWithRetries (host, port, retryLimit):
>   attempts = 0
>   while attempts < retryLimit:
>      try:
>         connection = zoom.Connection (host, port)
>         return connection
>      except ConnectionError:
>         attempts += 1
>   throw ConnectionError
>  
>

IMO, Roy's advice here is spot-on, with one minor technical detail.  In 
Python, exceptions use 'raise', not 'throw'...

Jeff Shannon
Technician/Programmer
Credit International





More information about the Python-list mailing list