Handling 3 operands in an expression without raising an exception
Dave Angel
davea at davea.name
Sun Sep 29 06:57:30 EDT 2013
On 29/9/2013 06:17, Νίκος 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
Try help(globals()) at the interpreter prompt.
Help on built-in function globals in module builtins:
globals(...)
globals() -> dictionary
Return the dictionary containing the current scope's global variables.
So once you have a dictionary, you can use get() to fetch values which
might or not be present, and supply the default in case they aren't.
>
> 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
>
I showed you the only 3 ways I know of, and none of them is as readable
as just setting the defaults in the standard way.
I only showed you those in order to show you how unreadable it would be.
--
DaveA
More information about the Python-list
mailing list