Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'charset:iso-8859-7': 0.04; 'interpreter': 0.05; 'assign': 0.07; 'defaults': 0.07; 'variables': 0.07; '"who': 0.09; '[0]': 0.09; 'present,': 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; 'variables.': 0.09; 'wrong,': 0.09; '12:50': 0.16; 'dictionary,': 0.16; 'fetch': 0.16; 'globals': 0.16; 'readable': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:exception': 0.16; 'subject:expression': 0.16; 'appropriate': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'module': 0.19; 'skip:p 40': 0.19; 'header:User-Agent:1': 0.23; 'decide': 0.24; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'host': 0.29; 'skip:g 30': 0.30; 'went': 0.31; 'code': 0.31; 'supposed': 0.32; 'except': 0.35; 'something': 0.35; 'but': 0.35; 'there': 0.35; 'skip:o 20': 0.38; 'to:addr:python- list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'even': 0.60; 'dave': 0.60; 'tell': 0.60; 'identify': 0.61; 'skip:o 30': 0.61; 'show': 0.63; 'more': 0.64; 'city': 0.66; 'here': 0.66; 'of:': 0.68; 'containing': 0.69; 'default': 0.69; 'gri': 0.84; 'angel': 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: Sun, 29 Sep 2013 10:57:30 +0000 (UTC) References: <20130928183833.GA13630@piedra> 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: 58 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1380452278 news.xs4all.nl 15988 [2001:888:2000:d::a6]:36476 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:55003 On 29/9/2013 06:17, Νίκος wrote: > Στις 29/9/2013 12:50 μμ, ο/η Dave Angel έγραψε: >> 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: >> gi,city,host=globals().get("gi", "who knows"), globals().get("city", >> "Άγνωστη Πόλη"), globals().get("host", "Άγνωστη >> Προέλευση") > > Hello Dave, > > By looking at your code i think that you are tellign the progrma to try > to gri don't know what the function globals() is supposed to do Try help(globals()) at the interpreter prompt. Help on built-in function globals in module builtins: globals(...) globals() -> dictionary Return the dictionary containing the current scope's global variables. So once you have a dictionary, you can use get() to fetch values which might or not be present, and supply the default in case they aren't. > > but i was thinking more of: > > ipval = ( os.environ.get('HTTP_CF_CONNECTING_IP') or > os.environ.get('REMOTE_ADDR', "Cannot Resolve") ) > try: > city = gi.time_zone_by_addr( ipval ) > host = socket.gethostbyaddr( ipval ) [0] > except socket.gaierror as e: > # We need something here to identify which one of the 2 above variables > or even both of them went wrong, and then assign the appropriate value > to each one of them but i don't know how to write it. > > Is there a function that can tell us which variable failed to be > assigned a value that we can use in order to decide to which variable we > will > I showed you the only 3 ways I know of, and none of them is as readable as just setting the defaults in the standard way. I only showed you those in order to show you how unreadable it would be. -- DaveA