Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #41423
| Path | csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <rosuav@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'elif': 0.04; 'arg,': 0.09; 'branches': 0.09; 'check.': 0.09; 'deemed': 0.09; 'okay': 0.09; 'def': 0.10; "wouldn't": 0.11; 'bounds': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'inclined': 0.16; 'instance:': 0.16; 'less,': 0.16; 'nans': 0.16; "range'": 0.16; 'time"': 0.16; "time'": 0.16; 'uncommon': 0.16; 'work.)': 0.16; 'wrote:': 0.17; 'instance,': 0.17; 'integer': 0.17; '(or': 0.18; 'not,': 0.21; "i'd": 0.22; 'script': 0.24; 'pass': 0.25; 'least': 0.25; 'header:In-Reply-To:1': 0.25; 'am,': 0.27; 'guess': 0.27; 'start,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'there.': 0.28; 'arrival': 0.29; 'equality': 0.29; 'kumar': 0.29; 'subject:other': 0.29; 'subject:some': 0.29; 'though.': 0.29; 'time:': 0.29; 'definition': 0.29; 'objects': 0.29; 'probably': 0.29; 'point': 0.31; 'to:addr:python-list': 0.33; 'code:': 0.33; 'received:google.com': 0.34; 'fail': 0.35; 'so,': 0.35; 'expected': 0.35; 'subject:?': 0.35; 'but': 0.36; 'compare': 0.36; 'should': 0.36; 'too': 0.36; 'skip:p 20': 0.36; 'drop': 0.37; 'late': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'your': 0.60; 'matter': 0.61; 'first': 0.61; 'side': 0.61; 'necessarily': 0.63; 'here': 0.65; 'late.': 0.65; 'hours': 0.66; 'subject:this': 0.84; '2013': 0.84; 'toy': 0.84; 'transport,': 0.91 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=tyODHHi8eF56mc2MfBv4W1AxFh904iq884Fs7MEnmm8=; b=JLhryjCanIZsH40HclfHe5ATo4HXdT1PDbTpRl2k+oCReVk9LahdKzs3kn0L+VcOk1 D1BuzBJbL6LwmzzO+68+O+2wE2Uhj+L0v04iDFN7gKlXg4dxYMas96ez4BL8qAsOHC0A HNX0GMmny7u2osj2Z4F0L+rB2T+mFbXxQveNi8aG0jD/iCBxLbaOEWyKnID4dz0753yS m62UVLdbxSbHHPefygZ7n0hM2YVS2fcn1Itu3sXQHw1xEp3j1FMHb7cq2RDAJf6uQ35x hYAs2hZZ5JLVnGHy7a90GOIJV4GvEwoPRzKyg1rXUCBSaOYHHzVrSiyhUydWHdIKLRRV /a7w== |
| MIME-Version | 1.0 |
| X-Received | by 10.58.253.161 with SMTP id ab1mr19998470ved.55.1363616179159; Mon, 18 Mar 2013 07:16:19 -0700 (PDT) |
| In-Reply-To | <CAE7MaQabqa2DT6bfiq3Ju1Kfyq2dMLFvebk3ynFbZnHxTSqsHA@mail.gmail.com> |
| References | <CAE7MaQabqa2DT6bfiq3Ju1Kfyq2dMLFvebk3ynFbZnHxTSqsHA@mail.gmail.com> |
| Date | Tue, 19 Mar 2013 01:16:19 +1100 |
| Subject | Re: What are some other way to rewrite this if block? |
| From | Chris Angelico <rosuav@gmail.com> |
| To | python-list@python.org |
| Content-Type | text/plain; charset=ISO-8859-1 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3452.1363616201.2939.python-list@python.org> (permalink) |
| Lines | 55 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1363616201 news.xs4all.nl 6863 [2001:888:2000:d::a6]:59916 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:41423 |
Show key headers only | View raw
On Tue, Mar 19, 2013 at 12:56 AM, Santosh Kumar <sntshkmr60@gmail.com> 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))
Well, for a start, I wouldn't compare for equality there. What's your
definition of "on time"? In Melbourne, for instance, a service is
deemed "on time" if it's less than 1 minute early and less than 5
minutes late. This is probably too broad, but I would guess that up to
1 minute (or at least half a minute) either side should be considered
on time.
Are you catering here for custom objects or NaNs that might not be
equal, less, or greater? If not, drop the else and just have three
branches - for instance:
if should_be_on >= came_on + 0.5: # Up to half a minute early is okay
return 'early'
elif should_be_on <= came_on - 1.0: # Up to one minute late is okay
return 'delayed'
else:
return 'on time'
As a matter of readability, incidentally, I'd be inclined to invert
the conditions and check the time of arrival against the expected
time:
if came_on < should_be_on - 0.5: # Up to half a minute early is okay
return 'early'
elif came_on > should_be_on + 1.0: # Up to one minute late is okay
return 'delayed'
else:
return 'on time'
I don't understand your bounds check, though. Are you working with
floating point hours in the day? (If so, it's still not necessarily
right - it's not uncommon to refer to the hours post-midnight as
24:00, 25:00, etc. But for a toy and a PoC, that would work.) But you
then pass the integer 123 as the first arg, which will fail that
check. What _is_ your data type?
ChrisA
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: What are some other way to rewrite this if block? Chris Angelico <rosuav@gmail.com> - 2013-03-19 01:16 +1100
csiph-web