Handling an connection error with Twython

Peter J. Holzer hjp-python at hjp.at
Sat Jun 1 14:12:07 EDT 2019


On 2019-05-25 13:46:40 +0200, Cecil Westerhof wrote:
> Just changing the while loop to a for loop did not make sense to me,
> but this does. I now have:
>     max_tries = 5
>     for current_try in range(1, max_tries):
>         try:
>             posted = twitter.update_status(status = message,
>                                            in_reply_to_status_id = message_id,
>                                            trim_user = True)
>             return posted['id']
>         except TwythonError as e:
>             if not 'Temporary failure in name resolution' in e.msg:
>                 raise
>             timed_message('Failed on try: {0} of {1}'.format(current_try, max_tries))
>             if current_try == max_tries:
                 ^^^^^^^^^^^^^^^^^^^^^^^^
                 This will never be true because the loop will exit
                 before current_try reaches max_tries.
>                 print('Could not get a connection to the internet: exiting')
>                 deinit(2)
                Also you don't seem to stop here (unless deinit raises
                an exception)

>             time.sleep(60)
>     raise RuntimeError('Should not get here')
                         ^^^^^^^^^^^^^^^^^^^^^
      So if you can't get a status update you will reach this point.

        hp

-- 
   _  | Peter J. Holzer    | we build much bigger, better disasters now
|_|_) |                    | because we have much more sophisticated
| |   | hjp at hjp.at         | management tools.
__/   | http://www.hjp.at/ | -- Ross Anderson <https://www.edge.org/>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20190601/6c8c68ba/attachment.sig>


More information about the Python-list mailing list