Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Python
Handling 3 operands in an expression without raising an exception
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
Reply to thread
Message
[QUOTE="Dave Angel, post: 5117985"] ipval = ( os.environ.get('HTTP_CF_CONNECTING_IP') or os.environ.get('REMOTE_ADDR', "Cannot Resolve") ) gi = city=host=None 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=gi if gi is not None else "who knows", city if city is not None else"¶ãíùóôç Ðüëç", host if host is not None else "¶ãíùóôç ÐñïÝëåõóç" That's one line. And if you now want to eliminate the gi=city=host=None line, let me attempt that. But this probably will have some serious typo in it, as I'm not testing any of these. And it assumes that the code is at top-level, and that none of these variables already exists. 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", "¶ãíùóôç ÐñïÝëåõóç") Or perhaps even, assuming this is the main script, and not a loaded module: import __main__ as qq 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=getattr(qq,"gi", "who knows"), getattr(qq,"city","¶ãíùóôç Ðüëç"),getattr(qq, "host", "¶ãíùóôç ÐñïÝëåõóç") [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Python
Handling 3 operands in an expression without raising an exception
Top