Comparison of ruby and python line by line (1/3)
According to an article in Linux Journal #144, Reuven M. Lerner demonstrated how to extend web services using other web services in Ruby. This article is very interesting and I love it so much even it is Ruby. I don’t have much experience in Ruby so I wonder how it could be implemented in Python. That’s the point drived me to post this blog. The original article shown 3 short programs which query prices by given ISBN from Skokie Public Library and . The first one is just very simple and short program for searching ISBN in Skokie.
- Script specification
Ruby#!/usr/bin/ruby
Python#!/usr/bin/python
- Import statement
Rubyrequire ’net/http’
Pythonimport sys import re import urllib
- Validate argument
Rubyif ARGV.length == 0 puts "#{$0}: You must enter at least one argument" exit end
Pythonif len(sys.argv) == 1: print "%s: You must enter at least one argument." % sys.argv[0] sys.exit()
- Initialize output and regular expressions
Rubyoutput = "" not_in_collection_re = /class="yourEntryWouldBeHereData"/ix on_shelf_re = /CHECK SHELF/ix checked_out_re = /DUE /ix
Pythonoutput = ’’ not_in_collection_re = re.compile(r’class="yourEntryWouldBeHereData"’,re.I) on_shelf_re = re.compile(r’CHECK SHELF’,re.I) checked_out_re = re.compile(r’DUE ’,re.I)
- Start main loop
RubyARGV.each do |isbn|
Pythonfor isbn in sys.argv[1:]:
- Validate input
Rubyif not isbn.match(/[0-9xX]{10}/) output << "ISBN #{isbn} is invalid.\n" next end
Pythonif not re.match(r’[0-9xX]{10}’,isbn): output += ’ISBN %s is invalid.\n’ % isbn continue
- Get query result
Rubyresponse = Net::HTTP.get_response(’catalog.skokielibrary.info’, "/search~S4/i?SEARCH=#{isbn}")
Pythonresponse = urllib.urlopen(’http://catalog.skokielibrary.info/search~S4/i’, ’SEARCH=%s’ % isbn) body = response.read()
- Check result
Rubyif not_in_collection_re.match(response.body) output << "ISBN #{isbn} is not in the Skokie collection.\n" elsif on_shelf_re.match(response.body) output << "ISBN #{isbn} is on the shelf.\n" elsif checked_out_re.match(response.body) output << "ISBN #{isbn} is currently checked out.\n" else output << "ISBN #{isbn} response: Unparseable!.\n" end
Pythonif not_in_collection_re.search(body): output += ’ISBN %s is not in the Skokie collection.\n’ elif on_shelf_re.search(body): output += ’ISBN %s is on the shelf.\n’ elif checked_out_re.search(body): output += ’ISBN %s is currently checked out.\n’ else: output += ’ISBN %s response: Unparseable!\n’
- End main loop
Rubyend
- Show output
Rubyputs output
Pythonprint output
I hope you are able to understand how Ruby and Python work for the same functions. Ruby seems to provide highly convenience string substitution while Python has very clean and clear syntax. Anyone could read Python code at a glance and understand its logic without Python knowledge.
Don't forget to read part 1, 2, 3.
Technorati Tags: English, IT, Programming, Ruby, Python, Tips and Tricks, Skokie, Linux Journal, Web Service
- sugree's blog
- 1893 reads
Post new comment