Handling 3 operands in an expression without raising an exception

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


Στις 26/9/2013 12:04 μμ, ο/η Jussi Piitulainen έγραψε:

>> Up until now i have this:
>>
>> city = gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] ) or
>> gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or
>> "�γν���η Π�λη"
>>
>>
>> can this be written as:
>>
>> city = gi.time_zone_by_addr( os.environ.get('HTTP_CF_CONNECTING_IP',
>> os.environ['REMOTE_ADDR'] )) or "�γν���η
>> Π�λη"
>>
>> It makes it more easily for me to understand this way.
>
> That will always get os.environ['REMOTE_ADDR'] and raise exception if
> it doesn't exist.
>
> Maybe you want this:
>
> city = gi.time_zone_by_addr(os.environ.get('HTTP_CF_CONNECTING_IP') or
>                              os.environ.get('REMOTE_ADDR') or
>                              "�γν����·Π�λη")

Okey that indeed doesn't hit on that lines.

Now my statements are as follows:

city = gi.time_zone_by_addr( os.environ.get('HTTP_CF_CONNECTING_IP') or 
os.environ.get('REMOTE_ADDR') or "Άγνωστη Πόλη" )

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

So you include everything in aprenthseis with a get method that you know 
beforehand it will not raise an exception but continue to check the next 
operand and the next until it default to the string.

Its more clear this way: but look. this way now produced a weird error 
few line slater when 'host' is tryign to be identefied by 're'.

[Thu Sep 26 09:44:39 2013] [error] [client 108.162.250.63]   File 
"/home/nikos/public_html/cgi-bin/metrites.py", line 181, in <module>
[Thu Sep 26 09:44:39 2013] [error] [client 108.162.250.63]     if not 
vip and re.search( 
r'(msn|gator|amazon|yandex|reverse|who|cloudflare|fetch|barracuda|spider|google|crawl|pingdom)', 
host ) is None:
[Thu Sep 26 09:44:39 2013] [error] [client 108.162.250.63]   File 
"/usr/local/bin/python/lib/python3.3/re.py", line 161, in search
[Thu Sep 26 09:44:39 2013] [error] [client 108.162.250.63]     return 
_compile(pattern, flags).search(string)
[Thu Sep 26 09:44:39 2013] [error] [client 108.162.250.63] TypeError: 
expected string or buffer




More information about the Python-list mailing list