I have a lot of conditions to check, but condition evaluation is heavy (e.g. condition requires database access), so I have to check them lazily.
Normally, such check could be written in if
clause:
if type in FOOD_PRIZES and Prize.objects.filter(type=type).exists():
pass
If the number of conditions are increasing then if
clause becomes ugly.
I can make list of condition lambdas and use all
method, but it looks ugly too:
conditions = [
lambda: type in FOOD_PRIZES,
lambda: Prize.objects.filter(type=type).exists()
]
if all(condition() for condition in conditions):
pass
Is there a better way to make code shorter? Is there another ways to make conditions lazy?