Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!eweka.nl!lightspeed.eweka.nl!194.109.133.87.MISMATCH!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!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.034 X-Spam-Evidence: '*H*': 0.93; '*S*': 0.00; 'received:134': 0.05; 'though:': 0.07; 'variables': 0.07; '[0]': 0.09; 'lines:': 0.09; 'logic': 0.09; 'wrote': 0.14; "wouldn't": 0.14; 'illustrating': 0.16; 'likewise': 0.16; 'missing?': 0.16; 'subject:exception': 0.16; 'subject:expression': 0.16; 'throw': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'thu,': 0.19; '>>>': 0.22; 'header:User-Agent:1': 0.23; "shouldn't": 0.24; 'least': 0.26; 'read,': 0.26; 'skip:" 20': 0.27; 'header:In-Reply-To:1': 0.27; 'host': 0.29; "doesn't": 0.30; 'easier': 0.31; 'lines': 0.31; '"",': 0.31; '>>>>': 0.31; 'sep': 0.31; 'file': 0.32; '(most': 0.33; 'guess': 0.33; 'not.': 0.33; 'basic': 0.35; 'but': 0.35; 'next': 0.36; 'should': 0.36; 'too': 0.37; 'two': 0.37; 'clear': 0.37; 'skip:o 20': 0.38; 'to:addr:python-list': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'even': 0.60; 'skip:u 10': 0.60; 'skip:o 30': 0.61; 'simple': 0.61; 'you.': 0.62; 'name': 0.63; 'more': 0.64; '8bit%:95': 0.64; 'forward': 0.65; 'city': 0.66; 'nobody': 0.68; '8bit%:92': 0.71; '8bit%:97': 0.74; 'lack': 0.78; 'end.': 0.84; 'pardon': 0.84; 'skip:\xc3 10': 0.91; '2013': 0.98 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AqIEANoORFKGuA9G/2dsb2JhbABbgz+De7oxgnqBOIMZAQEEASMPAUURCQIYAgIFFgsCAgkDAgECAUUTCAKHfAaOOZtWiTyJE4Epji8WglKBNgOXfYYei1qDJg Date: Thu, 26 Sep 2013 12:41:16 +0200 From: Antoon Pardon User-Agent: Mozilla/5.0 (X11; Linux i686; rv:10.0.12) Gecko/20130116 Icedove/10.0.12 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Handling 3 operands in an expression without raising an exception References: In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 64 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1380192089 news.xs4all.nl 15988 [2001:888:2000:d::a6]:58022 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:54808 Op 26-09-13 12:18, Νίκος schreef: > Στις 26/9/2013 1:12 μμ, ο/η Antoon Pardon έγραψε: >> Op 26-09-13 11:56, Νίκος schreef: >>> Στις 26/9/2013 11:55 πμ, ο/η Nobody έγραψε: >>>> On Thu, 26 Sep 2013 10:26:48 +0300, Νίκος wrote: >>>> >>>>> How can i wrote the two following lines so for NOT to throw out >>>>> KeyErrors when a key is missing? >>>>> >>>>> city = gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] ) or >>>>> gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or >>>>> "Άγνωστη Πόλη" >>>> >>>> tz = None >>>> ip = os.environ.get('HTTP_CF_CONNECTING_IP') >>>> if ip: >>>> tz = gi.time_zone_by_addr(ip) >>>> if not tz: >>>> ip = os.environ.get('REMOTE_ADDR') >>>> if ip: >>>> tz = gi.time_zone_by_addr(ip) >>>> if not tz: >>>> tz = "ÎγνÏÏÏη Î Ïλη" >>>> >>>> Likewise for the hostname. >>>> >>> >>> Its logic is simple and straightforward but too many lines: >>> >>> host = socket.gethostbyaddr( os.environ.get('HTTP_CF_CONNECTING_IP') or >>> os.environ.get('REMOTE_ADDR') or "Άγνωστη Προέλευση" ) >>> >>> this is much better in my opinion and straighforward also and more clear >>> to read: >> >> No it is not and you prove that in the very next line. >> >>> it doens't work though: >> >> If it doesn't do what you think it should do then it is not straight >> forward or clear, at least not to you. > > It is far better than the ifs, even easier to read, i was just missing a > [0] at the end. > > host = socket.gethostbyaddr( os.environ.get('HTTP_CF_CONNECTING_IP') or > os.environ.get('REMOTE_ADDR') or "Άγνωστη Προέλευση" )[0] No it is not. Your purpose was to have a line that wouldn't throw an exception even if some environ variables were not available. That means it shouldn't throw an exception when I execute the code. Guess what happens: >>> socket.gethostbyaddr( os.environ.get('HTTP_CF_CONNECTING_IP') or os.environ.get('REMOTE_ADDR') or "Άγνωστη Προέλευση" )[0] Traceback (most recent call last): File "", line 1, in socket.gaierror: [Errno -2] Name or service not known You are just illustrating your lack of basic understaning. -- Antoon Pardon