Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #41427
| Path | csiph.com!usenet.pasdenom.info!news.franciliens.net!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <prvs=782592764=jeanmichel@sequans.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.039 |
| X-Spam-Evidence | '*H*': 0.92; '*S*': 0.00; 'else:': 0.03; 'elif': 0.04; 'caller': 0.07; 'welcome.': 0.07; 'arg': 0.09; 'integer,': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; "range'": 0.16; "time'": 0.16; 'supposed': 0.21; 'hours,': 0.22; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'script': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'correct': 0.28; 'asks': 0.29; 'behavior.': 0.29; 'strings,': 0.29; 'subject:other': 0.29; 'subject:some': 0.29; 'url:mailman': 0.29; 'url:python': 0.32; '-----': 0.32; "skip:' 20": 0.32; 'url:listinfo': 0.32; 'code:': 0.33; 'subject:?': 0.35; 'really': 0.36; 'but': 0.36; 'url:org': 0.36; 'test': 0.36; 'thank': 0.36; 'skip:p 20': 0.36; 'hours.': 0.37; 'subject:: ': 0.38; 'store': 0.38; 'gives': 0.39; 'url:mail': 0.40; 'think': 0.40; 'remove': 0.61; 'you.': 0.61; 'received:194': 0.61; 'provide': 0.62; 'information': 0.63; 'here': 0.65; 'person,': 0.65; 'forward': 0.66; 'disclose': 0.69; 'notice:': 0.71; 'privileged.': 0.72; 'subject:this': 0.84; 'medium.': 0.91; 'transport,': 0.91; 'story.': 0.93 |
| X-IronPort-AV | E=Sophos;i="4.84,865,1355094000"; d="scan'208";a="1295280" |
| X-Virus-Scanned | amavisd-new at zimbra.sequans.com |
| Date | Mon, 18 Mar 2013 15:32:03 +0100 (CET) |
| From | Jean-Michel Pichavant <jeanmichel@sequans.com> |
| To | Santosh Kumar <sntshkmr60@gmail.com> |
| In-Reply-To | <CAE7MaQabqa2DT6bfiq3Ju1Kfyq2dMLFvebk3ynFbZnHxTSqsHA@mail.gmail.com> |
| Subject | Re: What are some other way to rewrite this if block? |
| MIME-Version | 1.0 |
| X-Mailer | Zimbra 7.2.2_GA_2852 (ZimbraWebClient - GC7 (Linux)/7.2.2_GA_2852) |
| Content-Type | text/plain; charset="utf-8" |
| Content-Transfer-Encoding | base64 |
| Cc | python-list@python.org |
| 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.3454.1363617125.2939.python-list@python.org> (permalink) |
| Lines | 30 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1363617125 news.xs4all.nl 6980 [2001:888:2000:d::a6]:46292 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:41427 |
Show key headers only | View raw
----- Original Message -----
> 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.
> --
> http://mail.python.org/mailman/listinfo/python-list
You can remove the 'if' line, report_status asks for hours, the caller is supposed to provide valid hours. What if the caller gives you strings, integer, floats ? This is a never ending story.
def report_status(should_be_on, came_on):
# well if you really really want to test it
assert(all([int(arg) in range(0,24) for arg in (should_be_on, came_on)]))
return { 0 : 'on time', -1 : 'delayed', 1 : 'early'}[cmp(should_be_on, came_on)]
JM
Note : in my example, 24.0 is excluded from the valid durations but I think this is the correct behavior.
-- IMPORTANT NOTICE:
The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Re: What are some other way to rewrite this if block? Jean-Michel Pichavant <jeanmichel@sequans.com> - 2013-03-18 15:32 +0100 Re: What are some other way to rewrite this if block? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-18 15:07 +0000
csiph-web