Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'charset:iso-8859-7': 0.04; 'assign': 0.07; 'assignment': 0.07; 'detect': 0.07; 'none:': 0.07; 'variables': 0.07; 'string': 0.09; '[0]': 0.09; 'assumed': 0.09; 'exception.': 0.09; 'latter': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'try:': 0.09; 'assume': 0.14; 'block.': 0.16; 'failure.': 0.16; 'host:': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:exception': 0.16; 'subject:expression': 0.16; 'throw': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'skip:p 40': 0.19; 'value.': 0.19; 'written': 0.21; 'putting': 0.22; 'tests': 0.22; 'header:User-Agent:1': 0.23; "haven't": 0.24; 'order.': 0.26; 'pass': 0.26; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'idea': 0.28; 'function': 0.29; 'host': 0.29; "doesn't": 0.30; 'code': 0.31; 'lines': 0.31; 'block,': 0.31; 'class': 0.32; 'me?': 0.32; "i'd": 0.34; 'except': 0.35; 'case,': 0.35; 'but': 0.35; 'there': 0.35; 'really': 0.36; 'city:': 0.36; "didn't": 0.36; 'possible': 0.36; 'should': 0.36; 'clear': 0.37; 'skip:o 20': 0.38; 'stopped': 0.38; 'to:addr :python-list': 0.38; 'itself': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'received:org': 0.40; 'even': 0.60; 'dave': 0.60; 'skip:o 30': 0.61; 'simply': 0.61; 'more': 0.64; 'within': 0.65; 'city': 0.66; 'yes': 0.68; 'line,': 0.68; 'reverse': 0.68; 'default': 0.69; 'fact,': 0.69; 'study': 0.69; '1970,': 0.84; 'nonsense.': 0.84; 'or:': 0.84; 'persistent': 0.84; 'working,': 0.84; 'angel': 0.91; 'dealt': 0.91; 'do:': 0.91; 'vars': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dave Angel Subject: Re: Handling 3 operands in an expression without raising an exception Date: Fri, 27 Sep 2013 10:43:52 +0000 (UTC) References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-7 Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: 174.32.174.35 User-Agent: XPN/1.2.6 (Street Spirit ; Linux) 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: 109 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1380278663 news.xs4all.nl 15964 [2001:888:2000:d::a6]:38926 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:54872 On 27/9/2013 05:19, Νίκος wrote: > Στις 27/9/2013 1:55 πμ, ο/η Dave Angel έγραψε: >> >> Simply assign the default values BEFORE the try block, and use pass as >> the except block. >> >> That still doesn't get around the inadvisability of putting those 3 >> lines in the try block. >> >> You still haven't dealt with the gt assignment and its possible >> exception. >> > Yes gi must be removed form within the try block because iam tesign it > for failure. Then why don't you do it below? > > I'am not sure what you mean though when you say: > >> Simply assign the default values BEFORE the try block, and use pass as >> the except block. > > Can you please make it more clear for me? > > This is my code as i have it at the moment: > > ipval = ( os.environ.get('HTTP_CF_CONNECTING_IP') or > os.environ.get('REMOTE_ADDR', "Cannot Resolve") ) > try: > gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat') > city = gi.time_zone_by_addr( ipval ) > host = socket.gethostbyaddr( ipval ) [0] > except socket.gaierror as e: > city = "Άγνωστη Πόλη" > host = "Άγνωστη Προέλευση" ipval = ( os.environ.get('HTTP_CF_CONNECTING_IP') or os.environ.get('REMOTE_ADDR', "Cannot Resolve") ) city = "Άγνωστη Πόλη" host = "Άγνωστη Προέλευση" try: city = gi.time_zone_by_addr( ipval ) host = socket.gethostbyaddr( ipval ) [0] except socket.gaierror as e: pass > > Or even better since i actually use 3 vars inside the try block it would > be really neat to be able to detect which far failed and assign a > specific value variable failed depended. ipval = ( os.environ.get('HTTP_CF_CONNECTING_IP') or os.environ.get('REMOTE_ADDR', "Cannot Resolve") ) city = None host = None try: city = gi.time_zone_by_addr( ipval ) host = socket.gethostbyaddr( ipval ) [0] except socket.gaierror as e: if not city: city = "Άγνωστη Πόλη" if not host: host = "Άγνωστη Προέλευση" or: ipval = ( os.environ.get('HTTP_CF_CONNECTING_IP') or os.environ.get('REMOTE_ADDR', "Cannot Resolve") ) city = None host = None try: city = gi.time_zone_by_addr( ipval ) host = socket.gethostbyaddr( ipval ) [0] except socket.gaierror as e: if not host: host = "Άγνωστη Προέλευση" if not city: city = "Άγνωστη Πόλη" Note that in that last case, I tested the variables in the reverse order. Note also on both of these that I assumed that None is not a reasonable value for either of those. In fact, I also assume that an empty string is not a reasonable value. If I didn't like the latter assumption, I'd have used tests like if host is None: > > IF it can also be written in one-line afteer the excpect would be even > better. Call me persistent but when i can write somethign in 1-line i > wou;d prefer it over 3-lines. Ridiculous. But if you like code golf, you can always do: city, host = "Άγνωστη Πόλη", "Άγνωστη Προέλευση" You should study APL. Many functions were written in one line, with twenty lines of explanation. The function itself was considered unreadable nonsense. And if a function stopped working, general wisdom was to throw it out, and re-implement the explanation. I studied it briefly in class in 1970, and have no idea if there are current implementations. -- DaveA