Handling 3 operands in an expression without raising an exception

Νίκος nikos.gr33k at gmail.com
Thu Sep 26 05:56:19 EDT 2013


Στις 26/9/2013 11:55 πμ, ο/η Nobody έγραψε:
> 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.
>

Its logic is simple and straightforward but too many lines:

host = socket.gethostbyaddr( os.environ.get('HTTP_CF_CONNECTING_IP') or 
os.environ.get('REMOTE_ADDR') or  "Άγνωστη Προέλευση" )

this is much better in my opinion and straighforward also and more clear 
to read:

it doens't work though:

ima thinkin that the [0] is missign at the end so i cna grab the first 
item of the returnign tuple:

host = socket.gethostbyaddr( os.environ.get('HTTP_CF_CONNECTING_IP') or 
os.environ.get('REMOTE_ADDR') or  "Άγνωστη Προέλευση" )[0]

for the first two element if they are beign return i cna understand the 
[0] at the end but what about if the string is being return wouldnt that 
return just the 'A' of the string?

Is it correct to write it liek this?





More information about the Python-list mailing list