Archive for May, 2008

State of RoR: the kids have it so easy

Wednesday, May 14th, 2008

I have been observing the developments in the RoR world with quite some interest lately. I think there are a few very significant improvements – the most important being the availability of mod_rails. The installation and setup is almost a no-brainer. I still remember the amount of pain it took to get Rails up and running in Apache2. And all the varying combinations of “best setups” offered – Apache (1.3), Lighttpd, LiteSpeed, Nginx, and finally, we’re back to Apache2. It’s about time. I think this will give a big boost to Rails adoption.  I really like that I can run multiple Rails apps on a single server with just a vhost setup, instead of mucking around with mod_proxy.

Next, I’ve been reading about Slicehost quite a bit, and I thought I should give it a shot. So we setup our staging server there, just to give it a shot. I ended up being very impressed – especially, I really like articles.slicehost.com – amazingly detailed and clear-cut source for setting up Rails apps on Slicehost. And pretty up-to-date too. I just finished setting up a Rails app on a Hardy Heron slice with the site’s help. I still remember the hoops we had to jump through to get the ‘perfect setup’ done on a vps. Really, the kids have it so easy these days.

Print

web.py – a web framework on Python

Friday, May 9th, 2008

web.py is a web framework for python that is as simple and it is powerful too.Installation steps are given below , make sure your system is having Python’s latest version and easy_install (Easy Install is a python module (easy_install) bundled with setup tools that lets you automatically download, build, install, and manage Python packages).

$sudo easy_install easy_install web.py

Or to install web.py, download and do python setup.py install.

Now the web.py is ready to use.Basically web.py is not following MVC architecture and here we use a code.py to write our classes and methods ,for rendering views we can use a templates folder.

Getting started with web.py

Create a file called code.py : where we writing our control methods

Create a directory for HTML file , lets say “templates” : where we creating the needed views for our methods

Now we have to import our web.py module , so add a line to our code.py

import web

web.py is imported to our application now.Now we need to tell web.py our URL structure.We can strt with a simple one now , add the below lines to your code.py

urls = (
'/', 'index' )

In this first part is regular expression that matches the URL and the second part is the name of a class to send the incoming request.Have to write the “index” method now.Open up the code.py and add a method called index , we can first try to print a “HELLO WORLD” on browser using web.py.

class index:
def GET(self):
print "HELLO WORLD"

This GET function will now get called by web.py anytime someone makes a GET request for “/”.Need to finish up this with a final line telling web.py to start serving web pages:

if __name__ == "__main__": web.run(urls, globals())

This tells web.py to serve the URLs we listed above and looking up the classes in the global namespace of this file.

Now run on your shell

$python code.py

The internal server of web.py gets started now , by default it will start at port number 8080 , you can change it by specifying the port you wish to run the server.

Adding templates

We can add all the templates to some directory , lets say templates , now add an index.html inside that.We can use either use simple html or web.py’s templating language to add code to our HTML.

<em>HELLO WORLD</em>

To get these templates inside our application add a line to code.py

render = web.template.render('templates/')

Databasing

Before start using a database, make sure that you have the appropriate database library installed. For MySQL databases, use MySQLdb and for Postgres use psycopg2.Now above our web.run line add the following line to connect to database.

web.config.db_parameters = dict(dbn='postgres', user='username', pw='password', db='databasename')

This will connect to our database.Now you can make try on a simple application which will create a blogging system.You can give a try on simple application which will create a blogging system.
blog.zip

Print

dict.org client with Ruby Shoes

Friday, May 2nd, 2008

While reading blogs and other online documents, emails etc, I always come across words whose meaning is not known to me. Usually I lookup these words using Ubuntu’s Dictionary application . Copying the word, pasting it in the text field of the dictionary app, hitting “Search”, and then waiting for results – this was proving to be a difficult process, as I have to do it very often. Today morning Vamsee told me about dict.org and that it has an API. So I thought of using dict.org’s API to build a tiny application that would help me lookup unknown words easily, preferably in a single step.

dict.org follows the standard Dictionary protocol. One could connect with TCP to ‘dict.org’ at 2628, and use the requests specified in the protocol to lookup words.

telnet dict.org 2628
Trying 72.36.131.187…
Connected to dict.org.
Escape character is ‘^]’.
220 aspen.miranda.org dictd 1.9.15/rf on Linux 2.6.18-6-k7 <auth.mime> <13095488.4831.1209719617@aspen.miranda.org>

DEFINE english freedom
150 10 definitions retrieved
151 “Freedom” gcide “The Collaborative International Dictionary of English v.0.48″
Freedom \Free”dom\ (fr[=e]“d[u^]m), n. [AS. fre['o]d[=o]m;
fre['o]free + -dom. See {Free}, and {-dom}.]
1. The state of being free; exemption from the power and
control of another; liberty; independence.
[1913 Webster]

But most of the languages already have dict.org API wrappers. A list is given here. I tried the Ruby and Python APIs, they are pretty straight forward. I also found a ‘dict’ program, which is a dict.org client.

sudo apt-get install dict

$ dict freedom

It dumps the meaning of the word as well as similar words from thesaurus on to the terminal. Now my idea was to build a simple GUI to ‘dict’. To do it in a flash, i decided to use Ruby Shoes, a light weight GUI tool kit in Ruby. More about Shoes and its installation can be read from Divya’s article on Shoes.

First step was to create a Ruby interface for dict.org using the ‘dict’ client.

IO.popen(”dict #{word}”).read

Now using this interface, I made a normal dictionary application using Shoes – one with a text field and search button. On clicking the button, the word in the text field will be looked using dict aand result will be displayed in the window. It worked well, but it did not have any advantage over the dictionary application already available with Ubuntu. I wanted the whole lookup process to happen seamlessly for the user, disturbing his reading flow as less as possible.

So i decided to use the clipboard. User now simply has to select a word. My program will pick it up from clipboard and lookup the word in dict.org and have the meaning, thesaurus and other details ready in a flash. All the user has to do is to select a word..!

My dict.org client with Ruby shoes is available here.

You can run it like this :

$ shoes dict.rb

Print