Google Search

Google
 

Thursday, November 22, 2007

Moved my blog to Thejaswi.info

I have moved my blog to http://thejaswi.info/ . Will post quite regularly over there. All those who have bookmarked my blog can update it to point to my new blog. I will also post selected posts here for backward-compatibilty ;-)

Saturday, September 29, 2007

Top 5 Firefox Extensions

I am a part-time webdesigner and developer and here are the top 5 Firefox extensions in my list (ordered according to rank).
1) Firebug: A must for every webdesigner. For beginners, there is Aardvark
2) Webdeveloper: Another must for webdesigners.
3) Greasemonkey: A cool addon that can be used to jazz up sites, scrap all your friends in a jiffy in Orkut and also to download Youtube videos...This is just a small description, Greasemonkey has more utilities. Check out Userscripts.
4) del.icio.us Bookmarks: del.icio.us is an online bookmarking site and this is their cool addon.
5) Chatzilla: Don't have to chat on the IRC from outside your browser!!!
If any IE users are reading this blog, switch to Firefox and become productive effortlessly.

Sunday, September 16, 2007

KDE 4 Showcase on QEMU

On September 6th, KDE 4 (Beta 2) was released and lots of noise is being generated in the blogosphere and in the OSS world. Most of the people were touting it as a revolution in the desktop world.

Curious I decided to try it out but didn't want to install a beta version on my computer. So I decided to download a Livecd based on KDE 4 and then use Qemu to emulate it and have a look.

True enough, KDE 4 looks stunning. You need to download it to experience the WOW ;-) Effect.

For all those who would love to watch videos here is a link.(not uploaded/created by me).

Dolphin (their new File browser) and Plasmoids are awesome!!! (Caution: Side effects of installing and using KDE4, It's going to make you Drool!!!)

Cool Python Documentation Utilities

It's been a long time since I blogged. Part of the reason was laziness and the other part was that I needed good content to blog on.
Now I have overcome both and here's my new post.

I have learnt from my experience that users (and developers alike!!!) mainly adopt projects which have comprehensive documentation. Just look at some of the most successful OSS projects, they devote a lot of time and energy to write and update documentation.

Here are two cool projects that I checked out to jazz my project!!!
Two such projects would be:

  • Docutils:Docutils is an open-source text processing system for processing plaintext documentation into useful formats, such as HTML or LaTeX. It includes reStructuredText, the easy to read, easy to use, what-you-see-is-what-you-get plaintext markup language.
    Using the simple markup language of Docutils one can generate documentation for their project in various formats without having to work on them individually.
    Check out a cool looking example -> Restructured Text Source transformed to Result

  • Epydoc:Epydoc is a tool for generating API documentation for Python modules, based on their docstrings.
    Most Python developers have the habit of writing docstrings. By using epydoc one can generate API documentation based on docstrings, comments and doc-tests.
    Check out Django's API auto-generated using Epydoc.


My project documentation is now based on reStructured Text and I have generated API documentation using epydoc (converted to PDF) available here.

Happy documenting!!!

Monday, August 13, 2007

Django Newforms and Django Check Constraints and Better Error Messages!!!

This is a continuation of my previous blog post. In this blog I'll explain of how to display error messages whenever a constraint is violated (but this works only in Postgresql, to know the reason why it doesn't work with other databases....read on).

Starting from where I left off, let us refine the views to integrate better error checking...

from django.http import HttpResponse
from letsee.models import Manufacturer
from django.newforms import form_for_model
from datetime import datetime,date
from django.shortcuts import render_to_response
from psycopg2 import IntegrityError

# letsee_manufacturer is the name of the table/app and manufacturer is the name of the model
error_msg = """new row for relation "letsee_manufacturer" violates check constraint """

error_dict = {
error_msg+"\"check_name\"\n": "Name must start with \"Merc\"",
error_msg+"\"check_date\"\n": "Date must be between 2007-01-01 and 2008-01-01",
error_msg+"\"check_end_date\"\n": "Car sale end date must be greater than car sale start date",
error_msg+"\"check_quantity\"\n": "Quantity sold must be positive.",
error_msg+"\"check_price\"\n": "Car price must be between 1000 and 10000",
}

def index(request):
ManufacturerForm = form_for_model(Manufacturer)
f = ManufacturerForm()
f.as_table()
return HttpResponse("<form action=\"/data/\" method=\"POST\" ><table>"+str(f)+"</table><br><input type=\"submit\" value=\"Submit\"/></form>")

def data(request):
if request.POST:
ManufacturerForm = form_for_model(Manufacturer)
f = ManufacturerForm(request.POST)
f.full_clean()
if f.is_valid():
start_date_type = datetime.strptime(request.POST['car_sale_start'],"%Y-%m-%d")
start_date = date(start_date_type.year,start_date_type.month,start_date_type.day)
end_date_type = datetime.strptime(request.POST['car_sale_end'],"%Y-%m-%d")
end_date = date(end_date_type.year,end_date_type.month,end_date_type.day)
try:
f.save()
return HttpResponse("Success")
except IntegrityError,e:
return render_to_response('car.html',{'car_form':f,'error_msg':error_dict[str(e)]})
else:
return HttpResponse(f.errors)
else:
return HttpResponse("Error.Was expecting POST method.")


Now to define a template file called 'car.html':

<html>
<head></head>
<body>
{% if error_msg %}
There was a problem saving your data :<br/>
<div style="color:red">{{ error_msg }}</div>
{% endif %}
<form action="/data/" method="POST">
<table>
{{ car_form }}
</table>
<br>
<input type="submit" value="Submit"/>
</form>
</body>
</html>


and now for the screenshot:



There is still a hitch, though two constraints are violated...ie the date is < 2007-01-01 and the name does not start with "Merc" we will still be able to display only one error at a time because the database raises one exception at a time. (No separate lines of code written for server side validation...easy and handled by the database)

For all those who have been patiently reading for the answer as to why we cannot go for such error handling with other databases like Sqlite. Simple....Sqlite raises insane constraint error messages whenever a constraint fails. It raises a "Constraint Failed" for any constraint that is violated whereas Postgresql raises a "new row for relation "letsee_manufacturer" violates check constraint "check_name" helping the user by giving more details about which constraint failed. You now have one more reason in the huge list of reasons why Django developers prefer Postgresql.
If you believe that the problem can easily be solved by opening a ticket with the Sqlite folks then check out this.(The ticket has been pending since January,2006...whew!!! that's a real long time). If any Sqlite developers or Sqlite users are reading this post then please clear the ticket at the earliest because it has lots of applications not just for this project but elsewhere too.
Don't know if Oracle too gives any such insane error messages...will have to check out...and will update the post.

Hope you love Django-Check-Constraints.

Thursday, July 26, 2007

Django Newforms and Django Check Constraints

Last week, my friend had an interesting comment to make on my project. He felt it was academic and hardly had any utility. This got me thinking and I wondered if I could use some of Django's cool features and get my project to work with them effortlessly. Then I thought of writing a small test project to show how Newforms (one of the coolest features of Django) and Django Check Constraints worked together.
So here is a small tutorial to use Django Newforms and Django Check Constraints together.
I will run through most of the mundane part (that is often on the Django Tutorial but usually neglected by many people). After downloading and patching Django with Django Check Constraints, start a django project. Then create a django app and edit the models.py file in the app. Here is my model:
from django.db import models
from check_constraints import Check
from datetime import date

# Create your models here.

class Manufacturer(models.Model):
mfg_name = models.CharField(maxlength=50)
car_sale_start = models.DateField()
car_sale_end = models.DateField()
quantity_sold = models.IntegerField()
car_price = models.IntegerField()

class Meta:
constraints = (
("check_name",Check(mfg_name__like = 'Merc%')),
("check_date",Check(car_sale_start__between = [date(2007,1,1),date(2008,1,1)])),
("check_end_date",Check(car_sale_end__gte = 'car_sale_start')),
("check_quantity",Check(quantity_sold__gte = 0)),
("check_price",Check(car_price__between = [1000,10000])),
)

Now a brief explanation of the Check fields:
"check_name" expects that the mfg_name field follow the Merc* regexp ('*' matches 0 or more characters while '+' matches only one character).
"check_date" expects a value between the dates 1st January 2007 and 1st January 2008.
"check_end_date" expects that the end_date is always greater than the start_date (Wouldn't it be weird if the sale end date was less than the sale start date).
"check_quantity" expects that the quantity be a PositiveInteger (you might use the Positive Integer field if you are using Postgresql. Django provides the PositiveIntegerField by default).
"check_price" expects that the price of the car be between 1000 and 10000.

Now to check the SQL output if everything is fine....and then syncdb.

(Observe how the '*' in the regexp got converted into a Postgresql regexp.)
After syncing the database, edit the views.py in the apps folder.
Create two functions (say index and data). My view below:

def index(request):
ManufacturerForm = form_for_model(Manufacturer)
f = ManufacturerForm()
f.as_table()
return HttpResponse("<form action=\"/data/\" method=\"POST\" ><table>"+str(f)+"</table><br><input type=\"submit\" value=\"Submit\"/>
</form>")

from django.http import HttpResponse
from letsee.models import Manufacturer
from django.newforms import form_for_model
from datetime import datetime,date

def index(request):
ManufacturerForm = form_for_model(Manufacturer)
f = ManufacturerForm()
f.as_table()
return HttpResponse("<form action=\"/data/\" method=\"POST\" ><table>"+str(f)+"</table><br><input type=\"submit\" value=\"Submit\"/$

def data(request):
if request.POST:
ManufacturerForm = form_for_model(Manufacturer)
f = ManufacturerForm(request.POST)
f.full_clean()
if f.is_valid():
start_date_type = datetime.strptime(request.POST['car_sale_start'],"%Y-%m-%d")
start_date = date(start_date_type.year,start_date_type.month,start_date_type.day)
end_date_type = datetime.strptime(request.POST['car_sale_end'],"%Y-%m-%d")
end_date = date(end_date_type.year,end_date_type.month,end_date_type.day)
f.save()
return HttpResponse("Success")
else:
return HttpResponse(f.errors)
else:
return HttpResponse("Was expecting POST.")

(I would like to thank Eduardo de Oliveira Padoan for helping me correcting my code.)
Most of the code is similar to the normal newforms code. The addition would be in the Date parsing...which is required if you want to save the data into a db.
Then update the urls.py file in the project folder and fire up the server to test the app.
Here are the screenshots:










For once I wouldn't agree with the phrase "No pain, No gain". Just add some exception handling and you can create a cool Newforms base Django app that validates easily and effectively.

Hope you like the django-check-constraints project and use it.

(PS: Currently does not support Datetime and Time...will add it soon.) Supported from 2nd August 2007.

Saturday, July 14, 2007

Yahoo more popular than Google!!!

Recently I came across Google Trends, a service that helps the user see what other users are searching on the net and compare search queries if any.

Tempted I decided to compare "Yahoo and Google" and look at what the result shows:



"Yahoo is more popular than Google!!!". Not satisfied I decided to check if the result also held true for "India". Here's the result of that:



Looks like Google is catching up with Yahoo! in India (in spite of ad campaigns by Yahoo!). Don't know on what basis Google Trends works and if we can really trust the results. Or probably one should believe what Google says: