If statement with or operator

Peter Pearson pkpearson at nowhere.invalid
Wed Feb 22 12:38:37 EST 2017


On Wed, 22 Feb 2017 22:33:31 +0530, Ganesh Pal wrote:
[snip]
> I  need suggestion on the if statement in the below code  , all that I was
> trying to do was to add a check  i.e if any one of the functions return
> True then  break the loop.
>
>  end_time = time.time() + 300
>     umount_completed = False
>     while time.time() < end_time:
>         if attempt_umount() is True or df_output_lines() is True:
>            logging.info("umount completed sucessfully")
>            umount_completed = True
>            break
>         time.sleep(15)
>     assert umount_completed, "ERROR: The umount failed Exiting"

The if statement is healthy and reasonable.  If you have a guarantee
that attempt_umount and df_output_lines return only True or False,
then this would be equivalent:

    if  attempt_umount() or df_output_lines():

However, at the bottom, checking for completion with "assert" is (as
I understand it) weak because assertions may be ignored in optimized
situations.  I'd raise RuntimeError.

Also, an consider this alternative loop structure:

    while not (attempt_umount() or df_output_lines()):
        if end_time < time.time():
            raise RuntimeError("The umount failed.  Exiting.")
        time.sleep(15)
    logging.info("umount completed successfully")

-- 
To email me, substitute nowhere->runbox, invalid->com.



More information about the Python-list mailing list