How to deal with optional argument evaluation

Rodrick Brown rodrick.brown at gmail.com
Wed May 11 16:54:16 EDT 2011


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)

-- 
[ Rodrick R. Brown ]
http://www.rodrickbrown.com http://www.linkedin.com/in/rodrickbrown
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110511/d2dc9269/attachment.html>


More information about the Python-list mailing list