Handling 3 operands in an expression without raising an exception

Chris Angelico rosuav at gmail.com
Sat Sep 28 06:19:54 EDT 2013


On Sat, Sep 28, 2013 at 7:33 PM, Νίκος <nikos.gr33k at gmail.com> wrote:
> It woould be nice if we could write it as:
>
>
> 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:
>         if city failed assign to it "Άγνωστη Πόλη" while if host failed
> assign it "Άγνωστη Προέλευση"
>
> but without an if statement and in 1 single line.

[ROLL] Rosuav rolls his eyes: 1, 1, totalling 2.

Then split your try blocks! You've already been told this.

ChrisA



More information about the Python-list mailing list