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


Groups > comp.lang.python > #16814 > unrolled thread

how to test attribute existence of feedparser objects

Started byHansPeter <hanspeter.sloot@gmail.com>
First post2011-12-08 01:34 -0800
Last post2011-12-10 17:51 +0000
Articles 4 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  how to test attribute existence of feedparser objects HansPeter <hanspeter.sloot@gmail.com> - 2011-12-08 01:34 -0800
    Re: how to test attribute existence of feedparser objects Chris Rebert <clp2@rebertia.com> - 2011-12-08 02:24 -0800
    Re: how to test attribute existence of feedparser objects xDog Walker <thudfoo@gmail.com> - 2011-12-10 09:19 -0800
      Re: how to test attribute existence of feedparser objects Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-10 17:51 +0000

#16814 — how to test attribute existence of feedparser objects

FromHansPeter <hanspeter.sloot@gmail.com>
Date2011-12-08 01:34 -0800
Subjecthow to test attribute existence of feedparser objects
Message-ID<2e457709-b40a-4763-8a3b-0e0560e98ee1@n10g2000vbg.googlegroups.com>
Hi,

While using the feedparser library for downloading RSS feeds some of
the blog entries seem to have no title.

 File "build\bdist.win32\egg\feedparser.py", line 382, in __getattr__
AttributeError: object has no attribute 'title'

Is there a way to test the existence of an attribute?

I can use an exception but like below to see whether it exists but
this is a clumsy way since the function has to return the title.

  d=feedparser.parse(url,handlers = [proxy])
  try:
    print "TITLE ",d.feed.title
  except:
    print "HAS NO TITLE"
  wc={}

Regards HansPeter

[toc] | [next] | [standalone]


#16816

FromChris Rebert <clp2@rebertia.com>
Date2011-12-08 02:24 -0800
Message-ID<mailman.3401.1323339874.27778.python-list@python.org>
In reply to#16814
On Thu, Dec 8, 2011 at 1:34 AM, HansPeter <hanspeter.sloot@gmail.com> wrote:
> Hi,
>
> While using the feedparser library for downloading RSS feeds some of
> the blog entries seem to have no title.
>
>  File "build\bdist.win32\egg\feedparser.py", line 382, in __getattr__
> AttributeError: object has no attribute 'title'
>
> Is there a way to test the existence of an attribute?
>
> I can use an exception but like below to see whether it exists but
> this is a clumsy way since the function has to return the title.

hasattr(obj, attr_name)
See docs.python.org/dev/library/functions.html#hasattr

That said, sounds like it won't make much difference in the particular
case you mention.
Also, never use a bare "except:" clause, unless you know what you're
doing and have a really good reason. Do "except AttributeError" in
this case.

Cheers,
Chris
--
http://rebertia.com

[toc] | [prev] | [next] | [standalone]


#16957

FromxDog Walker <thudfoo@gmail.com>
Date2011-12-10 09:19 -0800
Message-ID<mailman.3496.1323537600.27778.python-list@python.org>
In reply to#16814
On Thursday 2011 December 08 01:34, HansPeter wrote:
> Hi,
>
> While using the feedparser library for downloading RSS feeds some of
> the blog entries seem to have no title.
>
>  File "build\bdist.win32\egg\feedparser.py", line 382, in __getattr__
> AttributeError: object has no attribute 'title'
>
> Is there a way to test the existence of an attribute?
>

From the Fine Manual for feedparser 5.1:

Testing for Existence¶

Feeds in the real world may be missing elements, even elements that are 
required by the specification. You should always test for the existence of an 
element before getting its value. Never assume an element is present.

Use standard Python dictionary functions such as has_key to test whether an 
element exists.
Testing if elements are present¶

>>> import feedparser
>>> d = feedparser.parse('http://feedparser.org/docs/examples/atom10.xml')
>>> d.feed.has_key('title')
True
>>> d.feed.has_key('ttl')
False
>>> d.feed.get('title', 'No title')
u'Sample feed'
>>> d.feed.get('ttl', 60)
60


-- 
I have seen the future and I am not in it.

[toc] | [prev] | [next] | [standalone]


#16958

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-12-10 17:51 +0000
Message-ID<4ee39c14$0$29977$c3e8da3$5496439d@news.astraweb.com>
In reply to#16957
On Sat, 10 Dec 2011 09:19:31 -0800, xDog Walker wrote:

> Use standard Python dictionary functions such as has_key to test whether
> an element exists.

Idiomatic Python code today no longer uses has_key.

# Was:
d.feed.has_key('title')

# Now preferred
'title' in d.feed



-- 
Steven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web