views:

41

answers:

1

Is there a more efficient way to do the following? I am more interested in knowing if there is a way to set "mylist" to match anything if day is equal to 'all' because in other scenarios, "mylist" can contain a lot more elements.

if day == 'all':
    mylist = ['monday','tuesday','wednesday','thursday','friday','saturday','sunday']
else:
    mylist = [day] # day equal to one of the above

records = meta.Session.query(Transaction).filter(Transaction.day.in_(mylist)).all()
+6  A: 

What about just not filtering on the day if it's equal to 'all'?

query = meta.session.query(Transaction)
if day != 'all':
    query = query.filter(Transaction.day == day)
records = query.all()
David Zaslavsky
Also, if day == 'all' it may be necessary to filter out null values, i.e. query = query.filter(Transaction.day != None), if day is nullable.
zifot