Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #41415 > unrolled thread

What are some other way to rewrite this if block?

Started bySantosh Kumar <sntshkmr60@gmail.com>
First post2013-03-18 19:26 +0530
Last post2013-03-19 02:24 +1100
Articles 7 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  What are some other way to rewrite this if block? Santosh Kumar <sntshkmr60@gmail.com> - 2013-03-18 19:26 +0530
    Re: What are some other way to rewrite this if block? Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-03-18 16:10 +0200
      Re: What are some other way to rewrite this if block? Duncan Booth <duncan.booth@invalid.invalid> - 2013-03-18 14:43 +0000
        Re: What are some other way to rewrite this if block? Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-03-18 16:53 +0200
    Re: What are some other way to rewrite this if block? "Yves S. Garret" <yoursurrogategod@gmail.com> - 2013-03-18 07:18 -0700
    Re: What are some other way to rewrite this if block? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-18 15:10 +0000
      Re: What are some other way to rewrite this if block? Chris Angelico <rosuav@gmail.com> - 2013-03-19 02:24 +1100

#41415 — What are some other way to rewrite this if block?

FromSantosh Kumar <sntshkmr60@gmail.com>
Date2013-03-18 19:26 +0530
SubjectWhat are some other way to rewrite this if block?
Message-ID<mailman.3444.1363614983.2939.python-list@python.org>
This simple script is about a public transport, here is the code:

def report_status(should_be_on, came_on):
    if should_be_on < 0.0 or should_be_on > 24.0 or came_on < 0.0 or
came_on > 24.0:
        return 'time not in range'
    elif should_be_on == came_on:
        return 'on time'
    elif should_be_on > came_on:
        return 'early'
    elif should_be_on < came_on:
        return 'delayed'
    else:
        return 'something might be wrong'

print(report_status(123, 12.0))

I am looking forward of make the line starting with `if` short.

Any tips are welcome.

[toc] | [next] | [standalone]


#41421

FromJussi Piitulainen <jpiitula@ling.helsinki.fi>
Date2013-03-18 16:10 +0200
Message-ID<qotzjy0vlxc.fsf@ruuvi.it.helsinki.fi>
In reply to#41415
Santosh Kumar <sntshkmr60@gmail.com> writes:

> This simple script is about a public transport, here is the code:
> 
> def report_status(should_be_on, came_on):
>     if should_be_on < 0.0 or should_be_on > 24.0 or came_on < 0.0 or
> came_on > 24.0:
>         return 'time not in range'
>     elif should_be_on == came_on:
>         return 'on time'
>     elif should_be_on > came_on:
>         return 'early'
>     elif should_be_on < came_on:
>         return 'delayed'
>     else:
>         return 'something might be wrong'
> 
> print(report_status(123, 12.0))
> 
> I am looking forward of make the line starting with `if` short.
> 
> Any tips are welcome.

A double tip:

if (not (0.0 <= should_be_on <= 24.0) or
    not (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.

[toc] | [prev] | [next] | [standalone]


#41429

FromDuncan Booth <duncan.booth@invalid.invalid>
Date2013-03-18 14:43 +0000
Message-ID<XnsA18795D8AD5C5duncanbooth@127.0.0.1>
In reply to#41421
Jussi Piitulainen <jpiitula@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

[toc] | [prev] | [next] | [standalone]


#41431

FromJussi Piitulainen <jpiitula@ling.helsinki.fi>
Date2013-03-18 16:53 +0200
Message-ID<qottxo8vjwg.fsf@ruuvi.it.helsinki.fi>
In reply to#41429
Duncan Booth writes:
> Jussi Piitulainen 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):
>         ...

I'd still prefer to split the line, especially considering the fact
that the request was to make the line shorter:

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

[toc] | [prev] | [next] | [standalone]


#41424

From"Yves S. Garret" <yoursurrogategod@gmail.com>
Date2013-03-18 07:18 -0700
Message-ID<3dcc50c9-5909-4b10-9eb4-fd0cc5d98f35@googlegroups.com>
In reply to#41415
On Monday, March 18, 2013 9:56:16 AM UTC-4, Santosh Kumar wrote:
> This simple script is about a public transport, here is the code:
> 
> 
> 
> def report_status(should_be_on, came_on):
> 
>     if should_be_on < 0.0 or should_be_on > 24.0 or came_on < 0.0 or
> 
> came_on > 24.0:
> 
>         return 'time not in range'
> 
>     elif should_be_on == came_on:
> 
>         return 'on time'
> 
>     elif should_be_on > came_on:
> 
>         return 'early'
> 
>     elif should_be_on < came_on:
> 
>         return 'delayed'
> 
>     else:
> 
>         return 'something might be wrong'
> 
> 
> 
> print(report_status(123, 12.0))
> 
> 
> 
> I am looking forward of make the line starting with `if` short.
> 
> 
> 
> Any tips are welcome.

If you have a lot of conditions to check, you can't really get around it.

[toc] | [prev] | [next] | [standalone]


#41433

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-03-18 15:10 +0000
Message-ID<51472e56$0$6599$c3e8da3$5496439d@news.astraweb.com>
In reply to#41415
On Mon, 18 Mar 2013 19:26:16 +0530, Santosh Kumar wrote:

> This simple script is about a public transport, here is the code:
> 
> def report_status(should_be_on, came_on):
>     if should_be_on < 0.0 or should_be_on > 24.0 or came_on < 0.0 or
> came_on > 24.0:
>         return 'time not in range'
>     elif should_be_on == came_on:
>         return 'on time'
>     elif should_be_on > came_on:
>         return 'early'
>     elif should_be_on < came_on:
>         return 'delayed'
>     else:
>         return 'something might be wrong'


Untested, but something like this should work. It adjusts for the times 
which fall either side of midnight, it optionally allows for some "slop" 
in deciding whether an arrival was on time or not (defaults to 0).


def report(scheduled, actual, slop=0.0):
    """Report whether actual time of arrival is on time, late, early 
    or cancelled.

    Pass actual=None for cancelled services, otherwise both scheduled and 
    actual must be given as float hours between 0 and 24. E.g. to indicate
    a time of 06:30, pass 6.5.

    Optional argument slop is a non-negative time in hours which services
    are allowed to be late or early and still count as "on time". E.g. if 
    a service is allowed to arrive with two minutes either side of the
    scheduled time and still count as on time, pass slop=2.0/60. The slop
    defaults to 0.0.

    """
    if not 0 <= scheduled < 24:
        raise ValueError('scheduled time out of range')
    if not 0 <= slop < 24:
        raise ValueError('slop out of range')
    if actual is None:
        return "service cancelled"
    if not 0 <= actual < 24:
        raise ValueError('actual arrival time out of range')
    diff = actual - scheduled
    # Adjust for discontinuity around midnight. We assume that arrivals
    # are never more than 12 hours away from their scheduled times.
    if diff < -12:
        # For example, scheduled=23:55, actual=00:05 gives diff of -23:50
        diff += 24
    elif diff > 12:
        diff -= 24
    if diff < -slop:
        return "early"
    elif diff > slop:
        return "late"
    else:
        return "on time"


One disadvantage with the function as written is that you have to give 
times as floats between 0 and 24, instead of a more natural HH:MM syntax. 
For example, 11:07am would have to be given as 11.116666666666667. 

Another weakness is that any slop allowance is symmetrical (e.g. you 
can't allow trains to arrive up to one minute early or five minutes late 
to be counted as "on time", the two figures have to be equal).

A third weakness is that you can't allow for arrivals more than 12 hours 
early or late.



> I am looking forward of make the line starting with `if` short.

:-)



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#41434

FromChris Angelico <rosuav@gmail.com>
Date2013-03-19 02:24 +1100
Message-ID<mailman.3457.1363620300.2939.python-list@python.org>
In reply to#41433
On Tue, Mar 19, 2013 at 2:10 AM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> A third weakness is that you can't allow for arrivals more than 12 hours
> early or late.

Oh, but that'll NEVER happen.

Oh wait, I've been on a service that was 12 hours late.

Is there any chance that you could do your times of arrival and
expectation in, say, integer seconds since 1970?

ChrisA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web