Handling 3 operands in an expression without raising an exception
Denis McMahon
denismfmcmahon at gmail.com
Sun Sep 29 17:01:28 EDT 2013
On Sun, 29 Sep 2013 13:17:36 +0300, Νίκος wrote:
> Στις 29/9/2013 12:50 μμ, ο/η Dave Angel έγραψε:
>> 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", "Άγνωστη Προέλευση")
>
> Hello Dave,
>
> By looking at your code i think that you are tellign the progrma to try
> to gri don't know what the function globals() is supposed to do
>
> but i was thinking more of:
>
> ipval = ( os.environ.get('HTTP_CF_CONNECTING_IP') or
> os.environ.get('REMOTE_ADDR', "Cannot Resolve") )
> try:
> city = gi.time_zone_by_addr( ipval )
> host = socket.gethostbyaddr( ipval ) [0]
> except socket.gaierror as e:
> # We need something here to identify which one of the 2 above
variables
> or even both of them went wrong, and then assign the appropriate value
> to each one of them but i don't know how to write it.
>
> Is there a function that can tell us which variable failed to be
> assigned a value that we can use in order to decide to which variable we
> will
Yes, set the default values first, and overwrite them with the successful
values when the values are successfully calculated.
This is a very common method used in many programming languages.
--
Denis McMahon, denismfmcmahon at gmail.com
More information about the Python-list
mailing list