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


Groups > comp.lang.python > #5161 > unrolled thread

Re: How to deal with optional argument evaluation

Started byEthan Furman <ethan@stoneleaf.us>
First post2011-05-11 14:26 -0700
Last post2011-05-11 14:26 -0700
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#5161 — Re: How to deal with optional argument evaluation

FromEthan Furman <ethan@stoneleaf.us>
Date2011-05-11 14:26 -0700
SubjectRe: How to deal with optional argument evaluation
Message-ID<mailman.1430.1305148447.9059.python-list@python.org>
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~

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web