On Thu, Sep 17, 2009 at 3:09 PM, gert <gert.cuyk...@gmail.com> wrote:
> On Sep 17, 9:48 pm, Chris Rebert <c...@rebertia.com> wrote:
>> On Thu, Sep 17, 2009 at 12:38 PM, gert <gert.cuyk...@gmail.com> wrote:
>> > Can anybody show me a better looking alternative for this elif
>> > structure ?
>>
>> Use a dictionary with functions as values. For example:
>>
>> #untested obviously
>> def admin_remove(db, v, gid):
>>     db.execute("DELETE FROM appointments WHERE aid=?",(v['aid'],))
>>
>> def guest_remove(db, v, gid):
>>     db.execute("DELETE FROM appointments WHERE aid=? AND
>> uid=?",(v['aid'],s.UID))
>>
>> cmd_gid2func = {('remove', 'admin') : admin_remove, ('remove',
>> 'guest') : guest_remove}
>>
>> func = cmd_gid2func[(v['cmd'], s.GID)]
>> func(db, v, s.GID)
>>
>
> What about s.UID ?

I only covered a couple cases since this was just to give you an
example of the approach.
You'll need to adjust the parameters the functions take and the
elements in the key tuples of the dictionary to account for your full
body of code.

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to