On 6/4/2010 5:46 AM Tino Dai said...
         I'm at a point where I can do most things in Python (maybe) ,
now I'm looking to do them succinctly and elegantly. For instance, I
had about 10 - 15 lines of code to do this before with a bunch of
loops and if blocks, I distilled the product down to this:

        answerDict=dict(map(lambda x: (str(x[1]),x[0]),map(lambda x: \
           x.values(),Answer.objects.filter(fk_questionSet=1). \
           filter(fk_question=1).values('widgetAnswer').order_by(). \
           annotate(widgetCount=Count('widgetAnswer')))))


The first time there's a suspected problem with this code, you'll probably end up with a similar refactored set of 10-15 lines. I'm sure because I've got code like that scattered throughout my codebase and that's what I end up doing. The difference is that I rattle off the one-liners as part of the original coding effort, and only break it out when there's a need to -- I'm not striving to compact things into one-liners.

BTW, doesn't

   dict(map(lambda x: (str(x[1]),x[0]),map(lambda x:x.values()

simply map the values of a dict back into a dict?

Emile






_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to