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


Groups > comp.lang.python > #31401

Re: What's the tidy/elegant way to protect this against null/empty parameters?

From tinnews@isbd.co.uk
Newsgroups comp.lang.python
Subject Re: What's the tidy/elegant way to protect this against null/empty parameters?
Date 2012-10-16 14:44 +0100
Message-ID <mv40l9-isr.ln1@chris.zbmc.eu> (permalink)
References <1b8tk9-un9.ln1@chris.zbmc.eu> <119b3721-9d4f-4394-abd4-1ec19c0e5676@googlegroups.com>

Show all headers | View raw


Marco Nawijn <nawijn@gmail.com> wrote:
> On Monday, October 15, 2012 1:33:02 PM UTC+2, (unknown) wrote:
> > I want to fix an error in some code I have installed, however I don't
> > 
> > really want to just bodge it.
> > 
> > 
> > 
> > The function producing the error is:-
> > 
> > 
> > 
> >     def get_text(self, idx):               # override !
> > 
> >         node = self.items[idx]
> > 
> > 
> > 
> >         a= [
> > 
> >             ", ".join(node.tags),
> > 
> >             node.comment,
> > 
> >             node.folderName,
> > 
> >             cd2rd(node.date),
> > 
> >             node.name,
> > 
> >             '[' + self.rating_stars[node.rating] + ']'
> > 
> >             ] [self.select]
> > 
> > 
> > 
> >         return a
> > 
> > 
> > 
> > 
> > 
> > The error occurs when node[] (or at least its members) turn out to be
> > 
> > empty, you get a Traceback that ends with:-
> > 
> > 
> > 
> >   File "/usr/lib/jbrout/jbrout/listview.py", line 608, in draw_cell layout.set_text(self.get_text(thumbnail_num))
> > 
> >   File "/usr/lib/jbrout/jbrout.py", line 325, in get_text ", ".join(node.tags),
> > 
> >   TypeError: sequence item 0: expected string, NoneType found
> > 
> > 
> > 
> > Now its *probably* something higher up the tree causing the problem
> > 
> > (it's only one particular image in 20 thousand or so that breaks
> > 
> > things) but I really want to just get things working.  So, what's the
> > 
> > neatest way to protect the get_text() method from empty data?
> > 
> > 
> > 
> > 
> > 
> > -- 
> > 
> > Chris Green
> Hi,
> 
> Instead of protecting against empty data, you could just catch the exception, issue a warning and return a default "error" node which is valid. So something like (not tested):
> 
> def get_text(self, idx):               # override ! 
>         node = self.items[idx] 
> 
>         error_a = "A valid, but erroneous representation of a"
>         
>         try:
>             a= [ 
>                 ", ".join(node.tags), 
>                 node.comment, 
>                 node.folderName, 
>                 cd2rd(node.date), 
>                 node.name, 
>                 '[' + self.rating_stars[node.rating] + ']' 
>                 ] [self.select] 
>         except TypeError:
>               print 'Oops, something went wrong'
>               a = error_a
>         # You should always have a valid a here (or another exception has  occured)
>         return a 

That sounds like a reasonable approach, thank you (and all the other ideas).

-- 
Chris Green

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


Thread

What's the tidy/elegant way to protect this against null/empty parameters? tinnews@isbd.co.uk - 2012-10-15 12:23 +0100
  Re: What's the tidy/elegant way to protect this against null/empty parameters? Paul Rubin <no.email@nospam.invalid> - 2012-10-15 04:49 -0700
  Re: What's the tidy/elegant way to protect this against null/empty parameters? Chris Rebert <clp2@rebertia.com> - 2012-10-15 04:58 -0700
  Re: What's the tidy/elegant way to protect this against null/empty parameters? Roy Smith <roy@panix.com> - 2012-10-15 08:00 -0400
  Re: What's the tidy/elegant way to protect this against null/empty parameters? Miki Tebeka <miki.tebeka@gmail.com> - 2012-10-15 06:33 -0700
  Re: What's the tidy/elegant way to protect this against null/empty parameters? Terry Reedy <tjreedy@udel.edu> - 2012-10-15 11:45 -0400
  Re: What's the tidy/elegant way to protect this against null/empty parameters? Marco Nawijn <nawijn@gmail.com> - 2012-10-16 03:07 -0700
    Re: What's the tidy/elegant way to protect this against null/empty parameters? tinnews@isbd.co.uk - 2012-10-16 14:44 +0100

csiph-web