Στις 26/9/2013 10:48 πμ, ο/η Jussi Piitulainen έγραψε:
Νίκος writes:
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
...
I was under the impression that the 'or' operator was handling this
in case one operand was failing but its not the case here.
"f(x) or g(x)" raises an exception if "f(x)" raises an exception, or
if "f(x)" returns a false value and "g(x)" raises an exception.
Then i thought of os.environ.get() to default to something but then
again we have 3 operand in the expression.
Adapt this:
>>> {}.get('foo') or {'empty':''}.get('empty') or 'catchall'
'catchall'
Or nest the calls this way if an empty string is a valid value:
>>> {}.get('foo', {'empty':''}.get('empty', 'catchall'))
''
This will compute the default values even when they are not used.
I'am sorry but i do not understand the last statements at all so i can
have chnace to adapt them.
--
https://mail.python.org/mailman/listinfo/python-list