Jussi Piitulainen <jpiit...@ling.helsinki.fi> wrote:

>> Any tips are welcome.
> 
> A double tip:
> 
> if (not (0.0 <= should_be_on <= 24.0) or
>     not (0.0 <= came_on <= 24.0)):
>    ...
> 
Or even:

    if not (0.0 <= should_be_on <= 24.0 and 0.0 <= came_on <= 24.0):
        ...

> You might want to raise an exception from the range-check branch
> instead of returning a value. And raise an exception from the
> else-branch, because that branch should never be reached.

Or even lose the else branch entirely. If the code can never be reached 
then don't write it. Also you don't need 'elif' when the individual 
branches all return.

Putting that together and allowing some flexibility in the definition of 
'on time':

EARLY_DELTA = 1.0/60
LATE_DELTA = 5.0/60

def report_status(should_be_on, came_on):
    if not (0.0 <= should_be_on <= 24.0 and 0.0 <= came_on <= 24.0):
        raise ValueError('time not in range')

    if should_be_on - EARLY_DELTA <= came_on <= should_be_on + LATE_DELTA:
        return 'on time'

    if came_on > should_be_on:
        return 'delayed'

    return 'early'

Note that none of this will hande situations around midnight such as: 
should_be_on==23.5, came_on=0.5

-- 
Duncan Booth http://kupuguy.blogspot.com
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to