Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #5161

Re: How to deal with optional argument evaluation

Date 2011-05-11 14:26 -0700
From Ethan Furman <ethan@stoneleaf.us>
Subject Re: How to deal with optional argument evaluation
References <BANLkTimqPiwre+AAYt-TdWv4kjb9Cb_C-w@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.1430.1305148447.9059.python-list@python.org> (permalink)

Show all headers | View raw


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~

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: How to deal with optional argument evaluation Ethan Furman <ethan@stoneleaf.us> - 2011-05-11 14:26 -0700

csiph-web