Handling 3 operands in an expression without raising an exception
Nobody
nobody at nowhere.com
Thu Sep 26 04:55:29 EDT 2013
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.
More information about the Python-list
mailing list