Home Messages Index
[Date Prev][Date Next][Thread Prev][Thread Next]
Author IndexDate IndexThread Index

Re: [News] Canonical Launches New Software Called 'Bazaar'

Verily I say unto thee, that Linonut spake thusly:

> It used to bug me that it was written in Python, but having studied
> Python lately, I think the language is quite up to this task (if you're
> asking me, which if I know what you think and I think I do, you're not,
> but I don't care <grin>).

I've been working with Python quite a bit recently (out of necessity),
and I am extremely impressed. It's the most flexible language I've used
so far, in more ways than one, and not that overwhelming to learn either.

Here's a snippet of a basic RSS reader I wrote:

######
index=0
for lastdate,thisfeed  in zip(dates, feeds):

   print thisfeed

   # fetch and parse the actual feed
   feedcontent=feedparser.parse(thisfeed)

   # feed may be empty
   entryupd[index]="None"
   if len(feedcontent.entries) > 0:

      # fetch the date
      if feedcontent.entries[position].has_key('published'):
         entryupd[index]=feedcontent.entries[position].published_parsed
      elif feedcontent.entries[position].has_key('updated'):
         entryupd[index]=feedcontent.entries[position].updated_parsed
      elif feedcontent.has_key('modified'):
         entryupd[index]=feedcontent.modified

      # check for update
      entryupdstr=str(entryupd[index]) + "\n"
      lastdatestr=str(lastdate)
      # print entryupdstr
      # print lastdatestr
      if entryupdstr != lastdatestr:

         # fetch title, url and body
         title=feedcontent.entries[position].title
         url=feedcontent.entries[position].link
         if feedcontent.entries[position].has_key('content'):
            body=feedcontent.entries[position].content[0].value
         elif feedcontent.entries[position].has_key('summary'):
            body=feedcontent.entries[position].summary
         elif feedcontent.entries[position].has_key('description'):
            body=feedcontent.entries[position].description
         else:
            body=" "
######

This could use some better error checking, and there's a snipped section
where I fall back to os functions for text formatting (I haven't quite
mastered that one yet), but I'm pleasantly surprised how easy this was,
overall.

The zip "for" loop is a nice touch; being able to loop through multiple
arrays concurrently.

In fact the hardest part of this little project is consolidating my
limited Python skills with the fact that so many syndicate feeds out
there are badly off-spec and broken, which is something I was not really
prepared for. Oh well.

-- 
K.
http://slated.org

.:*~*:._.:*~*:._.:*~*:._.:*~*:._.:*~*:._.:*~*:._.:*~*:.
.                                                     .
. "Remember, if Christmas isn't found in your heart,  .
.  you won't find it under a tree." ~ C. Carpenter    .
.                                                     .
.         Merry Christmas & A Happy New Year          .
.                                                     .
.:*~*:._.:*~*:._.:*~*:._.:*~*:._.:*~*:._.:*~*:._.:*~*:.


Fedora release 8 (Werewolf) on sky, running kernel 2.6.23.8-63.fc8
 00:32:41 up 2 days, 23:44,  4 users,  load average: 0.00, 0.01, 0.11

[Date Prev][Date Next][Thread Prev][Thread Next]
Author IndexDate IndexThread Index