Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '1-5': 0.09; 'etc...': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'hash': 0.09; 'hostname': 0.09; 'message-id:@stoneleaf.us': 0.09; 'optional.': 0.09; 'received:gator410.hostgator.com': 0.09; '~ethan~': 0.09; 'def': 0.13; 'wrote:': 0.14; 'matches.': 0.16; 'received:72.11': 0.16; 'received:72.11.125': 0.16; 'received:72.11.125.166': 0.16; 'self,': 0.16; 'this?': 0.18; 'cc:no real name:2**0': 0.20; 'cc:2**0': 0.20; 'code': 0.22; 'header:In-Reply-To:1': 0.22; 'cc:addr:python-list': 0.22; 'keys': 0.23; 'skip:k 20': 0.23; 'skip:_ 20': 0.24; 'field,': 0.25; 'skip:# 10': 0.25; "i'm": 0.26; 'class': 0.29; 'elements': 0.29; 'host': 0.30; 'cc:addr:python.org': 0.31; 'information?': 0.31; 'does': 0.31; 'someone': 0.33; 'uses': 0.34; 'actually': 0.34; 'header:User- Agent:1': 0.35; 'else': 0.37; 'subject:with': 0.37; 'how': 0.39; 'takes': 0.40; 'feed': 0.40; 'header:Received:5': 0.40; 'received:hostgator.com': 0.64; 'supply': 0.68; 'dealing': 0.71; 'received:websitewelcome.com': 0.71; 'evaluate': 0.72; 'subject:deal': 0.84; 'supplies': 0.84; 'received:69.93': 0.91; 'scenario': 0.91; 'zone': 0.91; 'brown': 0.97 Date: Wed, 11 May 2011 14:26:02 -0700 From: Ethan Furman User-Agent: Thunderbird 1.5.0.10 (Windows/20070221) MIME-Version: 1.0 To: Rodrick Brown Subject: Re: How to deal with optional argument evaluation References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator410.hostgator.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - stoneleaf.us X-Source: X-Source-Args: X-Source-Dir: Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 55 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1305148447 news.xs4all.nl 41117 [::ffff:82.94.164.166]:51789 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:5161 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~