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


Groups > comp.lang.python > #31291

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

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <chris@rebertia.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; 'override': 0.07; 'empty,': 0.09; 'expression:': 0.09; 'received:mail-vc0-f174.google.com': 0.09; 'typeerror:': 0.09; 'bug': 0.10; 'cc:addr:python-list': 0.10; 'def': 0.10; 'producing': 0.15; "']'": 0.16; "(it's": 0.16; 'ah,': 0.16; 'in...': 0.16; 'members)': 0.16; 'none),': 0.16; 'oct': 0.16; 'saying.': 0.16; 'subject:parameters': 0.16; 'mon,': 0.16; 'wrote:': 0.17; 'fix': 0.17; 'string,': 0.17; 'working.': 0.17; '(or': 0.18; 'causing': 0.20; 'equivalent': 0.20; 'skip:" 30': 0.20; 'occurs': 0.22; 'cheers,': 0.23; 'cc:2**0': 0.23; '15,': 0.23; 'cc:no real name:2**0': 0.24; 'least': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'skip:[ 10': 0.26; 'skip:" 20': 0.26; 'am,': 0.27; 'tree': 0.27; 'message- id:@mail.gmail.com': 0.27; 'subject:/': 0.28; 'chris': 0.28; 'installed,': 0.29; 'node': 0.29; 'received:209.85.220.174': 0.29; 'url:code': 0.29; 'ends': 0.30; 'function': 0.30; 'error': 0.30; 'code': 0.31; 'file': 0.32; 'skip:s 30': 0.33; 'skip:l 40': 0.33; 'traceback': 0.33; 'problem': 0.33; 'languages': 0.33; 'received:google.com': 0.34; 'filter': 0.35; 'sequence': 0.35; 'so,': 0.35; 'expected': 0.35; 'received:209.85.220': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'next': 0.35; 'really': 0.36; 'but': 0.36; 'method': 0.36; 'turn': 0.36; 'item': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'things': 0.38; 'sure': 0.38; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'your': 0.60; 'url:p': 0.63; 'skip:n 10': 0.63; 'more': 0.63; 'learned': 0.65; 'protect': 0.69; 'subject:this': 0.84; 'sender:addr:chris': 0.84; 'subject:against': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type; bh=6U+a2cq0v4ACMY9cahhq+H/ViwtqNsdFeevX6TGZZq8=; b=KjC550nWJ5qHutLvFvKPwjiQWI5l5ht7vIWuCUoIC9SDq2taCt9VdEx7g+8SwrSl87 i/svRy6IcAabpmSW1D4nZTcJ9L9zojlt+vvU5QcPbimOxjF+RwZxu0srSwNYLJMoWFNX LyiLoty0CeM8ZrqPUeX9gY7aNGk0HQOyc25og=
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :x-gm-message-state; bh=6U+a2cq0v4ACMY9cahhq+H/ViwtqNsdFeevX6TGZZq8=; b=PCDxZECiiYAHf1S3xCjZAsDw0N/8pMJrcaGAeXwp2k7GOD2j5uFyhR+HMFMUfE/Sse O53kv3QrPwgrwyH6vJQI/gn8F3eMLbeqIBweJtV3Oh6amLRL70zj5XaVU5eB43AhSsuZ C331+3FcO5uGf68nmEJJyc4EYJDcQzAdrkk33DM8XQE4vRyheMf34rbUFLmQhid3aQDc xtimJgF2IXrv2qYTqlLQOov5xOAOQmMqR95dLdFRXH4GBRH48XU95UYhWD5U/6XO+k26 xh0nrTtw/dNaiPtqz94XLkSM8qrWElrlNpvEahgY01OtxY6PfcnUB8w5QrWGHLXiWJtM EREQ==
MIME-Version 1.0
Sender chris@rebertia.com
In-Reply-To <1b8tk9-un9.ln1@chris.zbmc.eu>
References <1b8tk9-un9.ln1@chris.zbmc.eu>
Date Mon, 15 Oct 2012 04:58:24 -0700
X-Google-Sender-Auth KfgwTOIrFkWB8mBB2R0CkqTl63c
Subject Re: What's the tidy/elegant way to protect this against null/empty parameters?
From Chris Rebert <clp2@rebertia.com>
To tinnews@isbd.co.uk
Content-Type text/plain; charset=UTF-8
X-Gm-Message-State ALoCoQm433q6tkigdNao3gfRfTBl0ijLgMFk/kFu/uZIRLtNai3JL/U66/R4sK/zCEOM0mxRhoP8
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.2196.1350302307.27098.python-list@python.org> (permalink)
Lines 52
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1350302307 news.xs4all.nl 6865 [2001:888:2000:d::a6]:35141
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:31291

Show key headers only | View raw


On Mon, Oct 15, 2012 at 4:23 AM,  <tinnews@isbd.co.uk> wrote:
> I want to fix an error in some code I have installed, however I don't
> really want to just bodge it.

"bodge". Well, I learned a new word this morning!

> 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,

To be precise: when node.tags contains one or more `None`s (Python's
equivalent of what other languages call "null" or "nil").
That's what the traceback is saying.

> 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))

Ah, so this is apparently regarding https://code.google.com/p/jbrout/
. Would have been nice not to have had to search and then only locate
it indirectly. Something to consider next time you write in...
Make sure you report your bug upstream!

>   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?

Filter out the `None`s with a generator expression:
    ", ".join(tag for tag in node.tags if tag is not None),

Cheers,
Chris

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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