post

Snakes and stones

Well, I came across this blog post by ‘PragDave’ where he writes a quick Ruby script that extracts the latest 5 entries from an RSS feed, writes it to a file and FTPs it to a remote box.

Seems simple enough but somehow the script just wasn’t readable to me!!

So, I thought of writing my own quick hack in Python to do a simple comparison and so I wrote the following script in about 8 min. Most of the time was actually simply downloading FeedParser and running sudo python setup.py install :)

Here’s the script:


[python]
#!/usr/bin/python
import feedparser, ftplib

d = feedparser.parse('http://www.swaroopch.info/feed/rss2/')
entries = min(len(d.entries), 5) # choose minimum entries

tmpfile = file('/tmp/topfive', 'w')

for i in range(0, entries):
    print >> tmpfile, '''
  
%s
%s
''' % (d.entries[i].link, d.entries[i].title, d.entries[i].description) tmpfile.close() ftp = ftplib.FTP('ftp.swaroopch.info') ftp.login('username', 'password') ftp.cwd('public_html/files/tmp') ftp.storbinary('STOR topfive', file('/tmp/topfive')) ftp.quit() [/python]

You can also see the output file.

This piece of code is a lot more readable, cleaner and faster for me to write. Also, I have never used the ftplib and feedparser modules before, this is my first time ever. It took just a few minutes for me to start using them both and they’re doing all the hard work in this script.

After this comparison, I don’t think there’s an incentive for me to try Ruby, but of course, Ruby has other stuff going for it. However, as Bruce Eckel puts it, "if Ruby pushes the right buttons for you, great. It’s probably the tool that will make you most productive right now, and that’s what you should use. It doesn’t really matter whether I am a fan (yet)."

Sidenote: I tried to post a comment or trackback to PragDave, but I couldn’t find a way. If somebody can find a way to do that, please let me know.

Advertisements

Comments

  1. Well, that Ruby code could’ve been written the way you’ve done it. But using closures is the Ruby Way To Do Things.

    Btw, added your feed to LJ => http://www.livejournal.com/userinfo.bml?user=swaroop_ch

  2. Hey Premshree,

    Ok, I guess, I’m not comfortable with the Ruby way as I am with the Python way. No big deal. Use what you like as long as you can get the job done ;)

    And thanks for the LJ feed!!

    P.S. Can you publicize that? I don’t know whether it is useful otherwise….

Your comments?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s