Archive

Archive for the ‘Technology’ Category

Enums are classes too. Neat

September 29, 2009 ravehanker Leave a comment

Got this from Effective Java, which I would strongly urge anyone to read before they write production code in Java.

Copied from here

Eg:

public enum Direction {
    NORTH { public Direction getOpposite() { return SOUTH; }},
    EAST { public Direction getOpposite() { return WEST; }},
    SOUTH { public Direction getOpposite() { return NORTH; }},
    WEST { public Direction getOpposite() { return EAST; }};

    public abstract Direction getOpposite();
}

Also possible

public interface Direction {
    public Direction getOpposite();
};

public enum ValidDirection implements Direction {
    NORTH { public Direction getOpposite() { return SOUTH; }},
    EAST { public Direction getOpposite() { return WEST; }},
    SOUTH { public Direction getOpposite() { return NORTH; }},
    WEST { public Direction getOpposite() { return EAST; }};
}

PS: WordPress has changed a LOT over the year!

Categories: Java, Technology Tags: , , ,

Amazon Kindle: Reading will never be the same again

November 19, 2007 ravehanker 24 comments

After more than ten years of revolutionizing the way books are sold, Amazon is all set to revolutionize books themselves. Our new e-book reader, the Amazon Kindle launches today.
Read Jeff’s personal letter at http://www.amazon.com.

I’ve had the privilege of working on this project and it’s been an incredible roller-coaster of a joy ride.

You can get more info from Newsweek’s cover story on the kindle here.

In my opinion, there’s one feature that trumps things in Kindle’s favour. Free Wireless, For Life.
This means two things to me:

  • You only have to pay for the books and Amazon will back it up, store your annotations and bookmarks at our site. You don’t lose your books even if you lose your kindle.
  • Books will *never* be out of stock for the kindle and they arrive the minute you order them.

Oh and did I mention that I worked on this product?

Categories: Technology Tags: , ,

Social Service

September 20, 2007 ravehanker Leave a comment

Lisp tutorial
Read this for more info

And no, I haven’t read the book yet :-D

Categories: Technology

5 things I learnt at Amazon

September 13, 2007 ravehanker 5 comments

It’s been a exhilarating four months at Amazon, or atleast it will be in another eight days!

Coming fresh out of college, I learnt a lot about the difference between being just a programmer and a software development engineer. A programmer is someone who just codes what you tell him to; a code monkey, so to say. But a software development engineer is “a biological machine that automatically converts coffee and beer into code”!

This big lesson apart, here are a few other things I learnt so far,

  1. Your 10K+ code with all it’s fancy architecture and flawless implementation is worth nothing if it’s not solving the business problem that it was needed for. Understand the context of the problem before jumping to conclusions.
  2. Sending your code review without proper comments is *much* worse than walking naked on the streets
  3. 5 minutes of extra thought at design time is 5 hours in dev time. Sketch up a rough design even if your writing a script.
  4. 1 hour into reading the fine manual is otherwise 10 in writing your own code.
  5. Using an unstable library is like shaving in the dark. You’re bound to cut yourself if you don’t know what you’re laying your hands on

At the end of the day, work is a lot of fun (and a lot of TT :-D ). I find it hard to go home :)

Categories: Technology

A five pointer on Hpricot Vs REXML

September 11, 2007 ravehanker Leave a comment

I’ve been learning to parse XML in Ruby for a while now using Hpricot and REXML. And here’s essentially what you should know if you are going through the same path

  1. Hpricot(as of this writing) is still *very* incomplete! It’s doesn’t support everything that you would want to do in Xpath. I had to jumps through hoops to get a few things done. REXML on the other hand works like a charm. Hpricot also behaves weirdly(and unpredictably) at places.
  2. Hpricot has parts of its implementation in C and way faster than REXML.
  3. I like the Hpricot syntax, which seemed more natural than what REXML uses
  4. REXML is very adament about content errors and would refuse to parse a document having a single misplaced ‘&’. Hpricot never complained.
  5. REXML is in the standard library
Categories: Ruby, Technology

Ruby from a Newbie

July 23, 2007 ravehanker 4 comments

A presentation I gave after learning the language for about a week.
Comments on accuracy and presentation and content are most welcome.

Ruby Talk Slides
Ruby Talk Source

Categories: Ruby, Technology

Templates in Vim

January 19, 2007 ravehanker Leave a comment

I always wanted something that will automatically insert a few header files and macro definitions for newly opened C/C++ files. NOT that i solve too many problems these days(it’s been a looooooong time now :( ) but the lazy programmer in me wouldn’t stop craving for one. Lot of people asked me to switch to emacs which apparently has something builtin for this purpose, but G0SUB came to my rescue and showed me this plugin for Vim.

And trust me, it works like a charm :)

Categories: Personal, Technology

Custom Change Manipulators in Django(oldforms)

January 17, 2007 ravehanker 2 comments

To tell you quite frankly, I never got to use an inbuilt manipulator while coding for Hackzor. I guess the primary reason being that my forms either had optional fields or i had to deal with two models at a time. I still could have avoided going custom, i guess i didn’t want to clutter the view with manipulation code.

So, on my path to custom manipulatorness, i missed this one HUGE turn called custom change manipulators. The docs didn’t say much about them and the comments weren’t too explicit either. After many hours of torturing unsuspecting victims on #Django, i finally found out that custom manipulators are really ‘custom’ – you’re responsible to storing the object and creating a dictionary for the form wrapper. It kinda sounds dumb right now, but it felt as hard the Da Vinci Code when i didn’t know the solution. So, in aid of all those poor, lost souls like me out there, Here’s how to make your own Custom Change Manipulator

The example i’m going to show you is naturally from Hackzor. I needed a custom change manipulator inorder to be able to put out a form where each contestant can edit his/her team details. List below, is the custom change manipulator that i wrote for the form:


class ChangeDetails (forms.Manipulator):
    ''' Change Membership details '''

    def __init__(self, user_id):
        try:
            self.original_object = User.objects.get(id=user_id)
        except User.DoesNotExist:
            from django.http import Http404
            raise Http404
        self.fields = (
            forms.EmailField(field_name='email',
                             length=30,
                             maxlength=30,
                             is_required=True,
                             validator_list=[self.isValidUsername]),
             forms.TextField(field_name='first_name',
                             length=20, maxlength=20,
                             is_required=True),
             forms.TextField(field_name='last_name',
                             length=20, maxlength=20,
                             is_required=True),
            )

    def isValidEmail (self, field_data, all_data):
        """ Checks if there is an already existing email and raises error if so"""
        try:
            User.objects.get(email=field_data)
        except User.DoesNotExist:
            return
        raise validators.ValidationError('The email "%s"
                                                 is already registered.' % field_data)

    def save(self, new_data):
        """ Saves The user object into the database
          with score set to 0 and is_active set to false"""
        self.original_object.first_name = new_data['first_name']
        self.original_object.last_name = new_data['last_name']
        self.original_object.email = new_data['email']
        print 'Saving UserProfile updation'
        self.original_object.save()
        return self.original_object

    def flatten_data(self):
        return {
                'username' : self.original_object.username,
                'email' : self.original_object.email,
                'first_name' : self.original_object.first_name,
                'last_name' : self.original_object.last_name,
                }

Points where it differs from an add manipulator:
1. The __init__ function of my manipulator has an extra argument from which i get the primary key of the object to be changed and assign the object to self.original_object
2. save is implemented like in the custom add manipulator, expect that it meddles with the existing object rather than creating a new one.
3. flatten_data is the function that will return a dict of the variables that will eventually be passed to the formwrapper to be loaded on the page. I will be calling this function from my view

Now lets take a look at the view to complete the picture


@login_required
def change_details(request):
    ''' Change details of existing users '''

    manipulator = ChangeDetails(request.user.id)

    if request.method == 'POST':
        new_data = request.POST.copy()
        print 'By post', new_data
        errors = manipulator.get_validation_errors(new_data)
        if not errors:
            manipulator.do_html2python(new_data)
            new_user = manipulator.save(new_data)
            return render_to_response('simple_message.html',
                {'message' : 'Your Details have been updated'}, RequestContext(request))
        else:
            print 'Errors'
    else:
        errors = {}
        new_data = manipulator.flatten_data()
        print new_data
    form = forms.FormWrapper(manipulator, new_data, errors)
    return render_to_response('change_profile.html',
            {'form': form}, RequestContext(request))

    return render_to_response('register.html', {'form':form}, RequestContext(request))
Categories: Technology

All’s well that ends well

January 15, 2007 ravehanker 1 comment

I’m going to swear again for the n’th time that i will start blogging regularly and that i will do put in some content here. But before that, an update on the Kurukshetra Online Programming Contest. Fate struck us on new year’s eve with our server we hosted the site on crashing for reasons that were beyond our control. we couldn’t get it up on time and had to eventually cancel the contest and hold it again. we rescheduled it again to the 14th of January and this time making sure that we had a kick-ass server running underneath. The acutal contest went well and i could well say that hackzor behaved as well as expected.

Hackzor has grown into a nice little web app now, albeit without the code being in the trunk. The current trunk is HUGELY out of version and my immediate goal is to fix the trunk right now. With me fixing the trunk, we will roll out a version which we will Christen version 0.1. The contest has also shown me a thousand places where i could change my design to improve ease and performance even adopting more simpler ways to do it. I do see good things coming up for hackor at the moment and would also continue to offer it for other Online Programming Contest that want hackzor underneath.

Prashanth has compiled a list of statistics that he has picked up from the server. Do read his article if you’re inclined.

Categories: College, Technology

Kurukshetra Online Programming Contest ‘06

December 30, 2006 ravehanker 1 comment

It’s been two weeks since i cam back from a long tour of the ICPC contests and the HiPC Conf.(we’ll talk about those things later) and people who were wondering what i was doing all these weeks should attend the Kurukshetra Online Programming Contest . The Contest is to be held on 31st December ‘06 from 2-7PM Indian Standard Time(GMT+0530). I designed the site in Django, along with my Pal Prashanth Mohan and it’s turned out quite well. I also played a very tiny part in setting the questions, the majority of the credit for which goes to my other Great friend Rajiv Mathews, who has meticulously worked on the framing the problems and solving them. I was able to see the amount of thought he had put into it when we designed the Test Cases for the questions.
Building this Web app has given me a lot of insight into Software Development not to mention the python-expertise I’ve gained. But, again, more on what i learnt in later blogs. For now, you can go to http://opc.kurukshetra.org.in and try a hand at out Contest. The prize money is quite a large sum(2000USD totally) and the contest is open to all.

We support quite a few languages(unlike most of the other Online Programming Contests) and hope to support more in the coming years. The current contest supports C,C++, Java, Python, Ruby and Perl.

So, Those who want to start their new year with a 1000 dollars in their pockets, you know know where to go!

Categories: College, Technology