Handling 3 operands in an expression without raising an exception

Dave Angel davea at davea.name
Sun Sep 29 07:27:57 EDT 2013


On 29/9/2013 07:14, Νίκος wrote:


> Dave's way though seems better.
> Assign the vars default string and if they get re-assinged correctly 
> that would be ideal, otherwise we have already given them the defaults.
>
> ipval = ( os.environ.get('HTTP_CF_CONNECTING_IP') or 
> os.environ.get('REMOTE_ADDR', "Cannot Resolve") )
> city = "Άγνωστη Πόλη"
> host = "Άγνωστη Προέλευση"
> try:
> 	gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat')
> 	city = gi.time_zone_by_addr( ipval )
> 	host = socket.gethostbyaddr( ipval ) [0]
> except Exception as e:
> 	print( "metrites.py => (%s): " % lastvisit, repr( sys.exc_info() ), 
> file=open('/tmp/err.out', 'w') )
>
> I'll think i'll stick to this solution.
>

But you've put gi back in to the try-block.  If it really belongs there,
then you'd better give it a default value as well. On the other hand,
if it can't get an exception, you should move it out.


-- 
DaveA





More information about the Python-list mailing list