NinjaCipher:-*-:ro60

Archive for February, 2008

Python vs Ruby on Google Blogs

Thursday, February 28th, 2008

Here is a graph I generated from trendrr.com to show how many search results google blogs returns for ruby vs python. It makes me happy on the inside to see them so neck and neck.

Django find_or_create helpers

Wednesday, February 27th, 2008

OK I’m going to be completely frank. You VERY likely could do this FAR better. But! I looked and have yet to see anything that does this for Django. So, here is my half ass’d attempt at two very helpful methods I picked up from my shady rails past. So by all means call doodoo on my methods and show me your skills because I would love to see someone else’s take on this. Again shouts out to perva.de for being the inspiration for these snippets and thank ifni for Django giving me reason to be excited about a web framework again.

find_or_create

def find_or_create(class_type, params, by='id'):
   if params == None:return None

   try:
      return class_type.objects.get(by+" = "+params[by])
   except class_type.DoesNotExist:
      obj = class_type()
      for column, value in params:
         if hasattr(obj, column):
         _member = getattr(obj,column)
         _member(value)

      obj.save()

      return obj

update_or_create

def update_or_create(class_type, params, by='id'):
   if params == None:return None

   try:
      obj = class_type.objects.get(by+" = "+params[by])
   except class_type.DoesNotExist:
      obj = class_type()

   for column, value in params:
      if hasattr(obj, column):
         _member = getattr(obj,column)
         _member(value)

         obj.save()

         return obj

called like so:

update_or_create(MyModel, request.POST, 'id')
find_or_create(MyModel, request.POST, 'id')