Decorators by Python

Monday, June 2nd, 2008

Lately, I’m using Django. So I also learn Python. And I found that python is very clean and nice language.

As we know Python allows that function can also be a parameter to another function. So code similar to this is ok.

def decorator(fun):
print fun.__name__
def my_fun():
print “hello world”

decorator(my_fun)

But Python provides some syntactic sugar so the code above is the same as this code:

def decorator(fun):
print fun.__name__
@decorator
def my_fun():
print “hello world”

So why it is so important, because I’m Java programmer and this allows me in elegant way to use something similar to Aspect. For example I can write cache decorator and apply it to every calculation intensive function.

class cache:
def __init__(self, function):
self.function = function
self.cache = {}
def __call__(self, *args):
try:
return self.cache[args]
except KeyError:
self.cache[args] = self.function(*args)
return self.cache[args]

Now for our propose I use a non optimal fibonacci recursive function.

def fib(n):
if n > 1:
return fib( n -1 ) + fib ( n -2 )
return 1

And simply measured that using enough large fibonacci number.

from time import gmtime, strftime
print “started %s” % strftime(“%a, %d %b %Y %H:%M:%S”, gmtime())
print fib(35)
print “ended %s” % strftime(“%a, %d %b %Y %H:%M:%S”, gmtime())

With and without @cache decorator. The output is

With @cache:

started Mon, 02 Jun 2008 20:32:41
14930352
ended Mon, 02 Jun 2008 20:32:41

and without:

started Mon, 02 Jun 2008 20:33:09
14930352
ended Mon, 02 Jun 2008 20:33:19

The conclusion is that it is great possibility to use such a class/function as I’m used to when using aspect with java.

Django tutorial finished

Thursday, February 15th, 2007

Yep it’s finished.
In my opinion django is worth knowing. It’s easy as RoR, but you always have control about what’s going on.

You must watch out about python syntax it’s sometimes not obvious that you have one space extra and code doesn’t work.

Last part of tutorial was about forms it normal, I think.

I’m going sleep now. Today I challenge with performance.

I think I won, becouse I really do so many improvements, but Lucene index is about 1Gb and performance still don’t satisfied me.

Tomorrow I’m going to won this battle.

Django 3rd day

Wednesday, February 14th, 2007

Now Django has no chance. After admin panel my expectation are very high.

But once again I was wrong.

Today tutorial 4, I didn’t achieve tutorial 4 because i play basketball and I’ve got appointment tomorrow about 7 a.m.

OK, URL managing, in my opinion it’s better than RoR because you have full control about it. For example

(r’^polls/(?P<poll_id>\d+)/$’, ‘mysite.polls.views.detail’),

Means that django all URL that matches regular expression

r’^polls/(?P<poll_id>\d+)/$’

Sends to module mysite.polls.views and call detail() function like

detail(request=<httprequest>, poll_id=’23′)

Next, tutorial shows how to write views, template. Finally it shows us a shortcuts. Yeah the functions from django.shortcuts boosts developing speed.

The last part of tutorial before me, and I’m thinking about topic of application to write, yet another blog is too easy for django and it’s admin module. So please suggest me something.

Regards
Pedro

Technorati Tags: ,

Django admin rulez

Tuesday, February 13th, 2007

Holly dolly

After I generate django project there was so little files. All Ruby on Rails
script directory is in manege.py, setting in settings.py quit simple.
Four files, it amazing.

Today I’ve got some time to look at tutorial2. And I don’t believe it, just can believe, the django admin application is marvelous. I don’t know what to say, it’s perfect. Even RoR ajax_scaffold is nothing special compared to django admin.
I Can’t even write here what I saw. Definitely everyone should see this. Great Job.

And to be honest this confusion was before I read about templates and css possibilities.

What is certain, I follow tutorial3, and definitely I wrote some application in django. Tomorrow I will fighting with bottlenecks, so I’m going to rest to be prepared.

Technorati Tags: ,

Django first meeting ;)

Sunday, February 11th, 2007

Ok I finished bootstrap meeting. It’s time to look at django framework. Some years ago I try python, and I think it’s worth knowing. Let’s see django.

Django requires python 2.3+ so I check my machine.

pedro@pedrowaty: ~ -V
Python 2.4.4

Bingo. Let’s follow installation instruction, get django tarball.

pedro@pedrowaty: ~ wget http://www.djangoproject.com/download/0.95.1/tarball/
tar xzvf Django-0.95.1.tar.gz
cd Django-0.95.1
sudo python setup.py install

Also I read that this don’t work with python 2.5, luckily I’ve got 2.4.4 ;)

Next I followed this instruction

I follow this tutorial 1 and what can I say? It’s easy as RoR. So the trip just begin, I couldn’t do my test app with this framework because of my home-life. But I’m sure the next meeting is coming as quick as possible.

about me

My name is Sebastian Pietrowski. I've finished Warsaw University as Master degree. During my studies I started work for merlin.pl. The primary language I use is Java but I have also programmed in Python, Ruby and Scala. I worked as a technical solution architect at merlin.pl. infrastructure when we were moving from PL/SQL to J2EE. I engineering a great performance optimized solution that made the application 10 times faster than requirements and 85 times faster as original solution.

Currently, I am working as a Senior Expert at F.Hoffmann-La Roche to help define future roadmap in design and development of Enterprise software at Roche and Genentech and build adoption for new technologies. I'm continuously mentoring new developers, helping them understand how important test driven development is and empowering them to get better at their daily job. I'm involved in many activities which brings new technologies for better and faster development. You can find more details on my LinkedIn profile.

But don’t get me wrong, I am not your typical nerd. I'm a pleasant guy that you can drink a glass of wine with me and talk about a range of topics with. My leisure activities include playing basketball, soccer and listening to music. I try to be pragmatic while staying focused on application performance and tuning with success in my daily work.

My favorite quote from Yoda's and my life’s motto is: Do, or do not. There is no try.