Reputation: 1313
Trying to validate function parameter combinations. Such that ()
, (project)
, (project,field)
and (project,field,well)
are valid and every other combination (e.g. (field)
) is not. If available, arguments will be strings otherwise defaults are None or empty string "". Currently doing poor man's bit mask check...
def make_thing(project=None, field=None, well=None):
# check for valid p-f-w combinations
check = (8 if project else 0) + (4 if field else 0) + (1 if well else 0)
if check not in (0, 8, 8 + 4, 8 + 4 + 1):
return None
# continue to do work
Question: What is a proper Pythonic way to do this?
Thank you!
Upvotes: 1
Views: 499
Reputation: 28
def make_thing(project=None, field=None, well=None):
if (not project and field) or (not field and well):
return
# continue to do work
def make_thing(project=None, field=None, well=None):
if (bool(project), bool(field), bool(well)) not in (
(False, False, False),
(True, False, False),
(True, True, False),
(True, True, True),
):
return
# continue to do work
Upvotes: 1