Saturday, July 25, 2009

[geek] Splitting django views

Geekiness ahead; you have been warned.

Steve wrote a post about splitting django views. Consider this as another viewpoint / way of doing the same.

The primary problem is to split django's views.py into multiple different modules, ostensibly to make them smaller and more modular (duh).

To begin with, I'm not entirely certain the problem exists - in MVC or MD paradigms, the view is typically very tiny and almost entirely devoid of logic. It is there only to reflect changes made to the model. Consequently, it is unclear that the views.py needs splitting at all. I have not done enough large scale django development (my projects have been of the tiny homebrew variety), so lets assume that views become unmanageable in the long run and need to be split up.

Lets say you have a setup and have the following directory structure:

> ls
__init__.py manage.py* settings.py urls.py urls.py.orig views.py

and views.py looks like this:

> cat views.py

from django.http import HttpResponse

def index(request):
return HttpResponse('index')

def foo(request):
return HttpResponse('foo')

def bar(request):
return HttpResponse('bar')

Now we'd like to break apart views into a directory, so we change it to:
> ls -R
.:
__init__.py manage.py* settings.py urls.py urls.py.orig views/

./views:
bar.py foo.py index.py

where each file in the views directory has its own little function:

> cat views/foo.py

from django.http import HttpResponse

def foo(request):
return HttpResponse('foo')

At this point, note that there is no __init__.py at all.

The original url resolver obviously will not work because it looks like this:
urlpatterns = patterns('',
(r'^foo/', 'views.foo'),
(r'^bar/', 'views.bar'),
(r'^$', 'views.index'),

So we change that too:
urlpatterns = patterns('',
(r'^foo/', 'views.foo.foo'),
(r'^bar/', 'views.bar.bar'),
(r'^$', 'views.index.index'),

and the site is back up as normal.

I think this approach - explicit directions in urls.py is much cleaner and much more inkeeping with django's spirit of avoiding magic. Note that the other approach which basically imported all functions the minute __init__.py was touched would maintain the urls.py mappings, thus saving a bit of work. However, I think it would confuse the heck out of people, not to mention result in wierd name collisions if any of the individual view files happen to export the same function.

Tuesday, July 14, 2009

The Sotomayor Hearings

There was a point when watching the GOP self-destruct brought a bit of joy. The GOP felt like such an alien party - anti-intellectual, anti-everything except God - a party from another planet. Then came the 2008 elections, the Dems won and things were great.

Given their resounding defeat, one would have thought that they would have regrouped and introspected a bit and tried to figure out what went wrong, how they alienated their base and led the most colossal eff-up in the history of the U.S.

But one would be wrong.

Instead they decided to continue on this path of bigotry, racial hatred, and just common thick-headedness. What better proof than the way Jeff Sessions, Ranking member of the Judiciary Committee, manhandled Judge Sotomayor. Quoting the transcript:

"So first, I'd like to know, do you think there's any circumstance in which a judge should allow their prejudices to impact their decision-making?"


"But the statement was, "I willingly accept that we who judge must not deny the differences resulting from experience and heritage, but continuously to judge when those opinions, sympathies and prejudices are appropriate." That's exactly opposite of what you're saying, is it not?"


"...but isn't it true this statement suggests that you accept that there may be sympathies, prejudices and opinions that legitimately can influence a judge's decision?..."

"...I just am very concerned that what you're saying today is quite inconsistent with your statement that you willingly accept that your sympathies, opinions and prejudices may influence your decision-making."

Is Sessions possibly trying to make a point that a person can be completely unbiased? That his or her background cannot affect a judgement? Is it humanly possible? The answer, of course, is no - it is not possible to be unbiased and even if we think we are unbiased, we are not. This is not a statement that I make loosely - the race iat and other literature in psychology would be able to show this easily.

Sotomayor's point was so rational.
"I think the system is strengthened when judges don't assume they're impartial, but when judges test themselves to identify when their emotions are driving a result, or their experience are driving a result and the law is not."

Brilliant.

What is really sad about all this is that the GOP comes out as really bigoted and prejudiced and trying to browbeat a Judge into submission. Not to mention hysterical when a bunch of white guys accuse a latina of being rascist.

This self-destruction of the GOP is very bad for several reasons. For one, I actually agree with a bunch of their ideals - fiscal conservatism, smaller government (never mind that they oversaw some of the biggest increases in government and spending in the past century). But the other, more important reason is that in order for a democracy to function, and to be strong and vibrant, it needs a strong opposition. Having the Dems in absolute power is very, very bad, just as bad is it was when the Repubs were in absolute power: balance is irrevocably lost.

So Dear GOP: Please get a grip on yourselves. Obama checkmated you with Sotomayor - you cannot possibly block her nomination and every minute you pull stupid stunts like the one Sen. Sessions did, you hemorrhage latino votes. Its not a battle you want to fight. And when you do fight, please fight with dignity. Healthy debate is what we want, not mindless Limbaugh-esque repetition of false facts, hearsay and opinion.