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


Groups > comp.lang.python > #34369

Re: Confused compare function :)

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python.ml.bruno.dupuis@lisael.org>
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; 'example:': 0.03; 'context': 0.05; 'debugging': 0.05; 'modified': 0.05; 'except:': 0.07; 'try:': 0.07; 'valueerror:': 0.07; "%s'": 0.09; 'handling,': 0.09; 'def': 0.10; 'dec': 0.15; 'finished': 0.15; "(it's": 0.16; 'above?': 0.16; 'advice:': 0.16; 'count,': 0.16; 'expression,': 0.16; 'fine.': 0.16; 'framework,': 0.16; 'frameworks)': 0.16; 'log.': 0.16; 'really?': 0.16; 'subject:compare': 0.16; 'swallowed': 0.16; 'years)': 0.16; 'string': 0.17; 'wrote:': 0.17; 'specify': 0.17; 'string,': 0.17; 'thu,': 0.17; 'input': 0.18; '(not': 0.20; 'code.': 0.20; 'import': 0.21; 'exceptions': 0.22; 'keyerror:': 0.22; 'libraries': 0.22; "i'd": 0.22; 'errors': 0.23; 'split': 0.23; 'this:': 0.23; "i've": 0.23; 'raise': 0.24; 'second': 0.24; 'script': 0.24; 'pass': 0.25; 'least': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'logging': 0.27; 'context,': 0.29; "d'aprano": 0.29; 'dialog': 0.29; 'represented': 0.29; 'steven': 0.29; 'error': 0.30; 'code': 0.31; 'point': 0.31; 'not.': 0.32; 'point,': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'another': 0.33; 'entry': 0.33; 'program,': 0.34; 'list': 0.35; 'data,': 0.35; 'so,': 0.35; 'there': 0.35; 'received:org': 0.36; 'except': 0.36; 'but': 0.36; 'depends': 0.36; "didn't": 0.36; 'should': 0.36; 'charset:us- ascii': 0.36; 'skip:p 20': 0.36; 'possible': 0.37; 'one,': 0.37; 'subject:: ': 0.38; 'skip:l 20': 0.38; 'some': 0.38; 'sure': 0.38; 'several': 0.39; 'instead': 0.39; 'to:addr:python.org': 0.39; 'list,': 0.39; 'short': 0.39; 'where': 0.40; 'easy': 0.60; 'content-disposition:inline': 0.60; 'first': 0.61; 'kind': 0.61; 'life,': 0.62; 'different': 0.63; 'ever': 0.63; 'times': 0.63; 'more': 0.63; 'of:': 0.65; 'abnormal': 0.84; 'hate': 0.93
Date Thu, 6 Dec 2012 09:49:26 +0100
From Bruno Dupuis <python.ml.bruno.dupuis@lisael.org>
To python-list@python.org
Subject Re: Confused compare function :)
References <CAKhY55PXaMkdnW7qMb-5ConHMcz05Gnkqw4wvyoprBFBd5UFFw@mail.gmail.com> <mailman.538.1354753193.29569.python-list@python.org> <k9p32p$nek$1@dont-email.me> <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com>
MIME-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Disposition inline
In-Reply-To <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com>
User-Agent Mutt/1.5.21 (2010-09-15)
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.549.1354783761.29569.python-list@python.org> (permalink)
Lines 91
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1354783761 news.xs4all.nl 6846 [2001:888:2000:d::a6]:55663
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:34369

Show key headers only | View raw


On Thu, Dec 06, 2012 at 04:32:34AM +0000, Steven D'Aprano wrote:
> On Thu, 06 Dec 2012 03:22:53 +0000, Rotwang wrote:
> 
> > On 06/12/2012 00:19, Bruno Dupuis wrote:
> >> [...]
> >>
> >> Another advice: never ever
> >>
> >> except XXXError:
> >>      pass
> >>
> >> at least log, or count, or warn, or anything, but don't pass.
> > 
> > Really? I've used that kind of thing several times in my code. For
> > example, there's a point where I have a list of strings and I want to
> > create a list of those ints that are represented in string form in my
> > list, so I do this:
> > 
> > listofints = []
> > for k in listofstrings:
> > 	try:
> > 		listofints.append(int(k))
> > 	except ValueError:
> > 		pass
> > 
> > Another example: I have a dialog box with an entry field where the user
> > can specify a colour by entering a string, and a preview box showing the
> > colour. I want the preview to automatically update when the user has
> > finished entering a valid colour string, so whenever the entry field is
> > modified I call this:
> > 
> > def preview(*args):
> > 	try:
> > 		previewbox.config(bg = str(entryfield.get()))
> > 	except tk.TclError:
> > 		pass
> > 
> > Is there a problem with either of the above? If so, what should I do
> > instead?
> 
> They're fine.
> 
> Never, ever say that people should never, ever do something.
> 
> 
> *cough*
> 

Well, dependening on the context (who provides listofstrings?) I would
log or count errors on the first one... or not.

On the second one, I would split the expression, because (not sure of
that point, i didn't import tk for years) previewbox.config and
entryfield.get may raise a tk.TclError for different reasons.

The point is Exceptions are made for error handling, not for normal
workflow. I hate when i read that for example:

    try:
        do_stuff(mydict[k])
    except KeyError:
        pass

(loads of them in many libraries and frameworks)
instead of:

    if k in mydict:
        do_stuff(mydict[k])

Note that the performances are better with the latter.

There are some exceptions to this, though, like StopIteration

For me, it's a rule of thumb, except: pass is possible in situations
where I control every input data, and I deeply, exactly know all code
interractions. If figuring all this out is longer (it's almost always
the case) than typing:

log.warning('oops:\n %s' % traceback.format_exc())

I log. 

It depends also on the context, I'd be more 'permissive' a short
script than into a large program, framework, or lib, for the
very reason it's easy to know all code interactions.

In my coder life, i spent more time debugging silently swallowed exceptions
than logging abnormal behaviours.

-- 
Bruno Dupuis

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: Confused compare function :) Bruno Dupuis <python.ml.bruno.dupuis@lisael.org> - 2012-12-06 01:19 +0100
  Re: Confused compare function :) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-06 00:42 +0000
    Re: Confused compare function :) Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-12-06 13:41 -0500
    Re: Confused compare function :) Anatoli Hristov <tolidtm@gmail.com> - 2012-12-06 19:55 +0100
  Re: Confused compare function :) Rotwang <sg552@hotmail.co.uk> - 2012-12-06 03:22 +0000
    Re: Confused compare function :) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-06 04:32 +0000
      Re: Confused compare function :) Bruno Dupuis <python.ml.bruno.dupuis@lisael.org> - 2012-12-06 09:49 +0100
        Re: Confused compare function :) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-06 11:47 +0000
          Re: Confused compare function :) peter <pjmakey2@gmail.com> - 2012-12-06 08:55 -0300
            Re: Confused compare function :) Hans Mulder <hansmu@xs4all.nl> - 2012-12-06 14:32 +0100
              Re: Confused compare function :) Chris Angelico <rosuav@gmail.com> - 2012-12-07 00:47 +1100
          Re: Confused compare function :) Chris Angelico <rosuav@gmail.com> - 2012-12-06 23:14 +1100
            Re: Confused compare function :) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-07 22:16 +0000
              Re: Confused compare function :) Terry Reedy <tjreedy@udel.edu> - 2012-12-08 02:01 -0500
              Re: Confused compare function :) Chris Angelico <rosuav@gmail.com> - 2012-12-08 18:17 +1100
              Re: Confused compare function :) MRAB <python@mrabarnett.plus.com> - 2012-12-08 17:50 +0000
            Re: Confused compare function :) Ramchandra Apte <maniandram01@gmail.com> - 2012-12-08 19:07 -0800
              Re: Confused compare function :) Chris Angelico <rosuav@gmail.com> - 2012-12-09 14:22 +1100
                Re: Confused compare function :) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-09 07:39 +0000
            Re: Confused compare function :) Ramchandra Apte <maniandram01@gmail.com> - 2012-12-08 19:07 -0800
          Re: Confused compare function :) Neil Cerutti <neilc@norwich.edu> - 2012-12-06 13:51 +0000
            Re: Confused compare function :) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-07 02:55 +0000
              Re: Confused compare function :) Neil Cerutti <neilc@norwich.edu> - 2012-12-07 16:40 +0000
        Re: Confused compare function :) Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-12-06 14:33 +0100
          Re: Confused compare function :) Chris Angelico <rosuav@gmail.com> - 2012-12-07 00:58 +1100
            Re: Confused compare function :) Hans Mulder <hansmu@xs4all.nl> - 2012-12-06 15:21 +0100
              Re: Confused compare function :) Chris Angelico <rosuav@gmail.com> - 2012-12-07 01:28 +1100
          Re: Confused compare function :) Anatoli Hristov <tolidtm@gmail.com> - 2012-12-06 15:22 +0100
          Re: Confused compare function :) Dave Angel <d@davea.name> - 2012-12-06 09:40 -0500
          Re: Confused compare function :) peter <pjmakey2@gmail.com> - 2012-12-06 11:46 -0300
          Re: Confused compare function :) Anatoli Hristov <tolidtm@gmail.com> - 2012-12-06 17:16 +0100
          Re: Confused compare function :) Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-12-06 16:52 +0000
          Re: Confused compare function :) Anatoli Hristov <tolidtm@gmail.com> - 2012-12-06 18:08 +0100
          Re: Confused compare function :) MRAB <python@mrabarnett.plus.com> - 2012-12-06 17:10 +0000
          Re: Confused compare function :) Anatoli Hristov <tolidtm@gmail.com> - 2012-12-06 18:31 +0100
          Re: Confused compare function :) MRAB <python@mrabarnett.plus.com> - 2012-12-06 17:52 +0000
          Re: Confused compare function :) Anatoli Hristov <tolidtm@gmail.com> - 2012-12-06 19:25 +0100
          Re: Confused compare function :) Anatoli Hristov <tolidtm@gmail.com> - 2012-12-07 14:36 +0100
        Re: Confused compare function :) Rotwang <sg552@hotmail.co.uk> - 2012-12-06 19:24 +0000
      Re: Confused compare function :) Chris Angelico <rosuav@gmail.com> - 2012-12-06 20:34 +1100
      Re: Confused compare function :) Rotwang <sg552@hotmail.co.uk> - 2012-12-06 19:25 +0000

csiph-web