elif alternative

C

Chris Rebert

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)

Cheers,
Chris
 
M

MRAB

gert said:
Can anybody show me a better looking alternative for this elif
structure ?

http://code.google.com/p/appwsgi/source/browse/appwsgi/wsgi/appointment.wsgi

Where you have cascaded ifs based on a single value, one alternative is
to use a dict where the test value is the key and functions are the
values:

def do_overview():
db.execute("SELECT calendar,appointment FROM appointments WHERE
calendar >= ? AND calendar < ?",(v['from'],v['to']))

def do_find():
db.execute("SELECT users.name, appointments.* FROM users,
appointments WHERE users.uid = appointments.uid AND
appointments.calendar >= ? AND appointments.appointment LIKE ? GROUP BY
appointments.aid",(v['calendar'],"%"+v['appointment']+"%"))

....

actions = {'overview': do_overview, 'find': find, ...}

actions[v['cmd']]()


Your code does have an 'if' which breaks the pattern, but you might be
able to work around it.
 
G

gert

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)

Actually I was hopping the answer would be no :)
Need to change allot of lines again thanks to you....
 
G

gert

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 ?
 
G

gert

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 ?

Never mind :)
func(db, v, s.GID, s.UID)
 
C

Chris Rebert

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
 
G

gert

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 ?

Never mind :)
func(db, v, s.GID, s.UID)

[(v['cmd'], s.GID)]
Can s.GID be None ?
 
G

gert

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 ?
Never mind :)
func(db, v, s.GID, s.UID)

[(v['cmd'], s.GID)]
Can s.GID be None ?

So far i have this
http://code.google.com/p/appwsgi/source/browse/appwsgi/wsgi/appointment.wsgi

I have 3 problems left Any_GID, Everybody_no_GID, and when no key
found do return
 
C

Chris Rebert

G

gert

I have 3 problems left Any_GID, Everybody_no_GID,

Just add entries to the dictionary with the same function as value but
all permutations of GID as part of their keys?
and when no key
found do return

try:
    func[(v['cmd'], s.GID)](db, v, s.UID, s.GID)
except KeyError:
    return text(response,'{"error":"session expired"}')

I edited the code again, this should work I think except for the None
maybe ?
I just have to make sure every user or admin is also in the guest
group.
 
G

gert

Just add entries to the dictionary with the same function as value but
all permutations of GID as part of their keys?
try:
    func[(v['cmd'], s.GID)](db, v, s.UID, s.GID)
except KeyError:
    return text(response,'{"error":"session expired"}')

I edited the code again, this should work I think except for the None
maybe ?
I just have to make sure every user or admin is also in the guest
group.

Works perfect thanks :)
Maybe I will add some lamba stuf to make it a bit more compact
but looks better now then the elif version
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,197
Messages
2,571,040
Members
47,634
Latest member
RonnyBoelk

Latest Threads

Top