How to deal with optional argument evaluation
Ethan Furman
ethan at stoneleaf.us
Wed May 11 17:26:02 EDT 2011
Rodrick Brown wrote:
> I'm having a hard time dealing with the following scenario
>
> My class takes a hash like the following:
> rdargs =
> {'env:'prod','feed':'opra','hostname':'host13dkp1','process':'delta','side':'a','zone','ny'}
>
> All the keys in this hash can be optional.
>
> I'm having a hard time dealing with all 36 possible combinations and I
> dont want to use a monstorous if not and else or etc... to evaluate all
> possible combinations before I call my search routine.
>
> How do others deal with situations like this? And have it look readable?
>
> if someone supplies just a host and side my search can return 400
> responses, if they supply all 6 elements it can return just 1-5 matches.
>
> class RD(object):
> def __init__(self,**kwargs):
> self.value = 0
> #for field in ('env', 'side', 'zone', 'feed', 'hostname',
> 'process', 'status'):
> #val = kwargs[field] if kwargs.has_key(field) else False
> #setattr(self, field, val)
> self.env = kwargs.get('env',False)
> self.side = kwargs.get('side',False)
> self.zone = kwargs.get('zone',False)
> self.feed = kwargs.get('feed',False)
> self.hostname = kwargs.get('hostname',False)
> self.process = kwargs.get('process',False)
> self.status = kwargs.get('status',False)
class RD(object):
def __init__(
self,
env=False,
side=False,
zone=False,
feed=False,
hostname=False,
process=False,
status=False
):
self.env = env
self.side = side
self.zone = zone
self.feed = feed
self.hostname = hostname
self.process = process
self.status = status
What does the code look like that actually uses this information?
~Ethan~
More information about the Python-list
mailing list