Handling 3 operands in an expression without raising an exception

Jussi Piitulainen jpiitula at ling.helsinki.fi
Thu Sep 26 05:04:13 EDT 2013


Νίκος writes:

> ΣÏις 26/9/2013 11:12 πμ, ο/η Jussi Piitulainen
> έγÏαÏε: > ίºÎ¿Ï‚ writes:
> >
> >> Σ�ις 26/9/2013 10:48 πμ, ο/η Jussi Piitulainen
> >> έγ�α�ε: > ί�ος writes:
> >>>
> >>>> How can i wrote the two following lines so for NOT to throw out
> >>>> KeyErrors when a key is missing?
> >>>>
> >>>> city = gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] ) or
> >>> ...
> >>>> I was under the impression that the 'or' operator was handling this
> >>>> in case one operand was failing but its not the case here.
> >>>
> >>> "f(x) or g(x)" raises an exception if "f(x)" raises an exception, or
> >>> if "f(x)" returns a false value and "g(x)" raises an exception.
> >>>
> >>>> Then i thought of os.environ.get() to default to something but then
> >>>> again we have 3 operand in the expression.
> >>>
> >>> Adapt this:
> >>>
> >>>     >>> {}.get('foo') or {'empty':''}.get('empty') or 'catchall'
> >>>     'catchall'
> >>>
> >>> Or nest the calls this way if an empty string is a valid value:
> >>>
> >>>     >>> {}.get('foo', {'empty':''}.get('empty', 'catchall'))
> >>>     ''
> >>>
> >>> This will compute the default values even when they are not used.
> >>
> >> I'am sorry but i do not understand the last statements at all so i
> >> can have chnace to adapt them.
> >
> > Do you know what {} is?
> >
> > Do you know what {}.get('foo') is?
> >
> > Do you know what x.get('foo') is if x is {}?
> >
> > Do you know what {'empty':''}.get('empty') is?
> >
> > Do you know what {'empty':''}.get('fruit') is?
> >
> > Do you know what (None or '' or 'catchall') is?
> >
> > Do you know what {}.get('foo', 'bar') is?
> >
> > Do you know what {}.get('foo', {}.get('bar', 'huh')) is?
> >
> > Do you know what ('foo'[3] or 'else') does?
> >
> > Do you know what ('foo' or 'else'[5]) does?
> >
> > Do you know how to launch an interactive Python session where you can
> > play with such expressions until you get the hang of it? There is no
> > substitute for that experience.
> >
> > Do you know that you can ask for help({}.get) or help(dict.get) or
> > even help(os.environ.get) during such an interactive Python session,
> > and Python (unlike Macbeth's spirits from the vasty deep) will answer?
>
> You dont have to be ironic. I dont have the experience you do.

Not ironic. Such questions really are how I learn and test my own
understanding, and all of the questions above are tailored to your
current specific problem. They are meant to help you.

But you should know the answers to such questions, even if you learn
them in some altogether different way. This is basic stuff.

> 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
                            "ÎγνÏÏÏηΠÏλη")

Though I would prefer a narrower layout this way:

something = ( os.environ.get('HTTP_CF_CONNECTING_IP') or
              os.environ.get('REMOTE_ADDR') or
              "ÎγνÏÏÏηΠÏλη" )
city = gi.time_zone_by_addr(something)



More information about the Python-list mailing list