Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #74105
| Path | csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!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.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'mathematics': 0.05; 'accepted,': 0.09; 'lawrence': 0.09; 'lines.': 0.09; 'pep': 0.09; 'pretend': 0.09; 'spaces': 0.09; 'try:': 0.09; 'type,': 0.09; 'windows,': 0.09; 'cc:addr:python- list': 0.11; '(lambda': 0.16; 'blocks:': 0.16; 'expression,': 0.16; 'file).': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'ioerror': 0.16; 'ioerror:': 0.16; 'lambda': 0.16; 'nesting': 0.16; 'none),': 0.16; 'semantics': 0.16; 'superfluous': 0.16; 'word),': 0.16; 'wrote:': 0.18; 'written': 0.21; 'seems': 0.21; 'example': 0.22; 'cc:addr:python.org': 0.22; 'certainly': 0.24; 'instead.': 0.24; 'pointer': 0.24; 'removed.': 0.24; 'skip:i 40': 0.24; 'mon,': 0.24; 'cc:2**0': 0.24; 'long,': 0.26; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'chris': 0.29; 'characters': 0.30; 'message-id:@mail.gmail.com': 0.30; 'skip:( 20': 0.30; 'code': 0.31; 'lines': 0.31; 'trivial': 0.31; 'file': 0.32; 'option': 0.32; '(most': 0.33; 'could': 0.34; 'except': 0.35; 'something': 0.35; 'one,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'surely': 0.36; 'two': 0.37; 'pm,': 0.38; 'that,': 0.38; 'bad': 0.39; 'even': 0.60; 'read': 0.60; 'skip:n 30': 0.60; 'break': 0.61; 'challenge': 0.61; 'name': 0.63; 'more': 0.64; 'line,': 0.68; 'readers': 0.68; 'carefully': 0.74; 'jul': 0.74; 'power': 0.76; 'life.': 0.83; 'to:none': 0.92 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=4/JMx7Wz2Y+ArUuhPcuYi2VWbv8ZiEcpWlW33tJC1aM=; b=NRlNLiXA5caXQnpzjtP4+UCyJn4h0E1eIB4SzNDHPdxEJD5v5/CDql6DdaNUUMfexE 8xRsFFUcjzHwPgT7vW7cHBNCuWjwi4FSp0c6Ym3z5oOzk9VMz6LCDBKhZ2LsXlyAYENl vpm3AEtKwPCRfqQ23xnF/SJG1R7RfwWZDeTXPiTvrQQLkOPLweljlmnQ29CW591nMZkQ 8iO6rcVVKsPs6e9C0XjrLxvR2k9V458EvT/XaFisYWIq8PlD8/bEPEF+z/em5QPMzLEY p10Ij2gJhN+oluko2/1sKqHLVR3n3Gqo0hEqedU5zaRq9/JdjRGyiktopCv4nljBzqHl C1Fg== |
| MIME-Version | 1.0 |
| X-Received | by 10.58.69.198 with SMTP id g6mr916690veu.56.1404739639775; Mon, 07 Jul 2014 06:27:19 -0700 (PDT) |
| In-Reply-To | <lpe60l$1st$1@ger.gmane.org> |
| References | <53ba11fc$0$29985$c3e8da3$5496439d@news.astraweb.com> <c1uo2eF60gfU1@mid.individual.net> <53ba538d$0$2926$c3e8da3$76491128@news.astraweb.com> <CAPTjJmos00xjJVMZFKY1F9Z8L+ByusHD-ciuats5FYbwQ6RF+g@mail.gmail.com> <lpe60l$1st$1@ger.gmane.org> |
| Date | Mon, 7 Jul 2014 23:27:19 +1000 |
| Subject | Re: open() and EOFError |
| From | Chris Angelico <rosuav@gmail.com> |
| Cc | "python-list@python.org" <python-list@python.org> |
| Content-Type | text/plain; charset=UTF-8 |
| 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 | <https://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 | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.11591.1404739642.18130.python-list@python.org> (permalink) |
| Lines | 56 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1404739642 news.xs4all.nl 2928 [2001:888:2000:d::a6]:43500 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:74105 |
Show key headers only | View raw
On Mon, Jul 7, 2014 at 11:06 PM, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote:
> On 07/07/2014 09:09, Chris Angelico wrote:
>>
>> It seems trivial in this example to break it into two try blocks:
>>
>> try:
>> name = input("Enter file name, or Ctrl-D to exit")
>> # On Windows, use Ctrl-Z [enter] instead.
>> except EOFError:
>> sys.exit()
>> try:
>> fp = open(name)
>> except IOError:
>> handle_bad_file(name)
>> else:
>> handle_good_file(fp)
>>
>
> All those extra lines to type, not on your life. Surely it would be better
> written as a one liner?
Challenge accepted! I shall wield the power of PEP 463, even though it
never got accepted, and pretend that it's a standard feature!
name = (input("Enter file name, or Ctrl-D to exit") except EOFError: sys.exit())
(handle_good_file(open(name)) except IOError: handle_bad_file(name))
Readers with a degree in higher mathematics will note that this is not
one, but two lines. It can be turned into one line, however the
forking out of the name requires some cheating. But since the last
part is now an expression, we have the option of lambda shenanigans...
(lambda n:(handle_good_file(open(n))except
IOError:handle_bad_file(n)))(input("Enter file name, or Ctrl-D to
exit")except EOFError:sys.exit())
Et voila! A single line, albeit 142 characters long, and that with
superfluous (ha!) internal spaces removed. As a side-effect of
expressionization (if that's a word), the code now catches IOError
from inside handle_good_file() by going to handle_bad_file, which is
almost certainly a bad idea (most likely, handle_good_file is going to
read from that file). But it's much more important that this be a
one-liner!
Alternatively, the nesting could be inverted. As long as
handle_bad_file() returns something which is not a file pointer (eg
None), and handle_good_file is carefully written to quietly do nothing
if given that, the original semantics could be retained:
(lambda n:(handle_good_file(open(n)except
IOError:handle_bad_file(n))))(input("Enter file name, or Ctrl-D to
exit")except EOFError:sys.exit())
Take your pick. And enjoy! :)
ChrisA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
open() and EOFError Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-07-07 03:20 +0000
Re: open() and EOFError Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-07-07 17:04 +1200
Re: open() and EOFError Steven D'Aprano <steve@pearwood.info> - 2014-07-07 08:00 +0000
Re: open() and EOFError Chris Angelico <rosuav@gmail.com> - 2014-07-07 18:09 +1000
Re: open() and EOFError Dave Angel <davea@davea.name> - 2014-07-07 08:14 -0400
Re: open() and EOFError Chris Angelico <rosuav@gmail.com> - 2014-07-07 22:19 +1000
Re: open() and EOFError Roy Smith <roy@panix.com> - 2014-07-07 08:46 -0400
Re: open() and EOFError Chris Angelico <rosuav@gmail.com> - 2014-07-07 22:58 +1000
Re: open() and EOFError Roy Smith <roy@panix.com> - 2014-07-07 20:26 -0400
Re: open() and EOFError Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-07-08 09:50 +1200
Re: open() and EOFError Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-07-07 15:45 +0000
Re: open() and EOFError Chris Angelico <rosuav@gmail.com> - 2014-07-08 01:53 +1000
Re: open() and EOFError Marko Rauhamaa <marko@pacujo.net> - 2014-07-07 19:08 +0300
Re: open() and EOFError Marko Rauhamaa <marko@pacujo.net> - 2014-07-07 19:12 +0300
Re: open() and EOFError Roy Smith <roy@panix.com> - 2014-07-07 08:39 -0400
Re: open() and EOFError Chris Angelico <rosuav@gmail.com> - 2014-07-07 22:55 +1000
Re: open() and EOFError Roy Smith <roy@panix.com> - 2014-07-07 20:28 -0400
Re: open() and EOFError Terry Reedy <tjreedy@udel.edu> - 2014-07-07 14:49 -0400
Re: open() and EOFError Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-07-08 10:04 +1200
Re: open() and EOFError Grant Edwards <invalid@invalid.invalid> - 2014-07-08 14:19 +0000
Re: open() and EOFError Terry Reedy <tjreedy@udel.edu> - 2014-07-08 11:08 -0400
Re: open() and EOFError Tim Chase <python.list@tim.thechases.com> - 2014-07-08 10:20 -0500
Re: open() and EOFError Chris Angelico <rosuav@gmail.com> - 2014-07-09 01:24 +1000
Re: open() and EOFError Tim Chase <python.list@tim.thechases.com> - 2014-07-08 10:46 -0500
Re: open() and EOFError Roy Smith <roy@panix.com> - 2014-07-08 21:05 -0400
Re: open() and EOFError Anssi Saari <as@sci.fi> - 2014-07-10 22:40 +0300
Re: open() and EOFError Chris Angelico <rosuav@gmail.com> - 2014-07-09 01:49 +1000
Re: ^D vs ^Z as EOF and DOS dinosaurs talking (was: open() and EOFError) Tim Chase <python.list@tim.thechases.com> - 2014-07-08 10:57 -0500
Re: ^D vs ^Z as EOF and DOS dinosaurs talking (was: open() and EOFError) Jan van den Broek <balglaas@xs4all.nl> - 2014-07-08 16:38 +0000
Re: ^D vs ^Z as EOF and DOS dinosaurs talking (was: open() and EOFError) Chris Angelico <rosuav@gmail.com> - 2014-07-09 02:20 +1000
Re: open() and EOFError Tim Chase <python.list@tim.thechases.com> - 2014-07-08 11:40 -0500
Re: open() and EOFError Chris Angelico <rosuav@gmail.com> - 2014-07-09 02:50 +1000
Re: open() and EOFError Steven D'Aprano <steve@pearwood.info> - 2014-07-08 09:03 +0000
Re: open() and EOFError Chris Angelico <rosuav@gmail.com> - 2014-07-08 20:29 +1000
Re: open() and EOFError Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-07-07 14:06 +0100
Re: open() and EOFError Chris Angelico <rosuav@gmail.com> - 2014-07-07 23:27 +1000
Re: open() and EOFError Chris Angelico <rosuav@gmail.com> - 2014-07-07 23:29 +1000
Re: open() and EOFError Dan Stromberg <drsalists@gmail.com> - 2014-07-07 09:07 -0700
Re: open() and EOFError Ian Kelly <ian.g.kelly@gmail.com> - 2014-07-07 11:25 -0600
Re: open() and EOFError Marko Rauhamaa <marko@pacujo.net> - 2014-07-07 20:31 +0300
Re: open() and EOFError Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-07-07 18:07 +0000
Re: open() and EOFError Marko Rauhamaa <marko@pacujo.net> - 2014-07-07 21:42 +0300
Re: open() and EOFError Roy Smith <roy@panix.com> - 2014-07-07 20:22 -0400
Re: open() and EOFError Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-07-08 10:09 +1200
Re: open() and EOFError Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-07-08 00:10 +0100
Re: open() and EOFError "Neil D. Cerutti" <neilc@norwich.edu> - 2014-07-08 10:29 -0400
Re: open() and EOFError Chris Angelico <rosuav@gmail.com> - 2014-07-08 03:29 +1000
Re: open() and EOFError Marko Rauhamaa <marko@pacujo.net> - 2014-07-07 20:40 +0300
Re: open() and EOFError Chris Angelico <rosuav@gmail.com> - 2014-07-08 10:55 +1000
Re: open() and EOFError Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-07-07 18:54 -0400
csiph-web