1 message in com.googlegroups.sqlalchemy[sqlalchemy] Improve code
FromSent OnAttachments
VitaminJ23 Jan 2008 07:57 
Subject:[sqlalchemy] Improve code
From:VitaminJ (vita@gmail.com)
Date:01/23/2008 07:57:15 AM
List:com.googlegroups.sqlalchemy

Hi,

I have an existing piece of functionality but I am wondering if there are ways to improve the code. A short summary what I have. I modelled a person class which can have different roles, one being a student role, another one being an employee role, an alumni role and so on. Now I am trying to retrieve all persons which have an employee role but only those that do not have the alumni role.

This is what I came up with:

query = session.query(Person).join('roles') alumnis = query.filter(Role.roleID==alumni_table.c.roleID).order_by(Person.lastName).all() tmp = query.filter(Role.roleID==employee_table.c.roleID).all() employees = list(set(tmp) - set(alumnis)) employees.sort(key=operator.attrgetter('lastName'))

It is working as expected but I thought there must be better ways to do it. Instead of letting python do the work I would like to pass this to sql. I found except_ in the sql expression api but I do not know where to start with that. So if there is somebody out there who has a smart idea how to improve the code, let me know...

Thanks and Greetings, Jan