Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #54785 > unrolled thread

Handling 3 operands in an expression without raising an exception

Started byΝίκος <nikos.gr33k@gmail.com>
First post2013-09-26 10:26 +0300
Last post2013-09-29 13:32 -0400
Articles 20 on this page of 107 — 25 participants

Back to article view | Back to comp.lang.python


Contents

  Handling 3 operands in an expression without raising an exception Νίκος <nikos.gr33k@gmail.com> - 2013-09-26 10:26 +0300
    Re: Handling 3 operands in an expression without raising an exception Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-09-26 10:48 +0300
      Re: Handling 3 operands in an expression without raising an exception Νίκος <nikos.gr33k@gmail.com> - 2013-09-26 10:51 +0300
        Re: Handling 3 operands in an expression without raising an exception Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-09-26 11:12 +0300
          Re: Handling 3 operands in an expression without raising an exception Νίκος <nikos.gr33k@gmail.com> - 2013-09-26 11:39 +0300
            Re: Handling 3 operands in an expression without raising an exception Νίκος <nikos.gr33k@gmail.com> - 2013-09-26 11:46 +0300
              Re: Handling 3 operands in an expression without raising an exception Νίκος <nikos.gr33k@gmail.com> - 2013-09-26 11:50 +0300
                Re: Handling 3 operands in an expression without raising an exception Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-09-26 12:20 +0300
            Re: Handling 3 operands in an expression without raising an exception Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-09-26 12:04 +0300
              Re: Handling 3 operands in an expression without raising an exception Νίκος <nikos.gr33k@gmail.com> - 2013-09-26 12:45 +0300
            Re: Handling 3 operands in an expression without raising an exception Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-09-26 11:07 +0200
              Re: Handling 3 operands in an expression without raising an exception Νίκος <nikos.gr33k@gmail.com> - 2013-09-26 12:31 +0300
                Re: Handling 3 operands in an expression without raising an exception Maarten <maarten.sneep@knmi.nl> - 2013-09-26 02:52 -0700
                Re: Handling 3 operands in an expression without raising an exception Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-09-26 12:01 +0200
                Re: Handling 3 operands in an expression without raising an exception "James Harris" <james.harris.1@gmail.com> - 2013-09-26 11:53 +0100
            Re: Handling 3 operands in an expression without raising an exception alex23 <wuwei23@gmail.com> - 2013-09-27 10:05 +1000
          Re: Handling 3 operands in an expression without raising an exception Chris Angelico <rosuav@gmail.com> - 2013-09-26 18:41 +1000
          Re: Handling 3 operands in an expression without raising an exception Tim Golden <mail@timgolden.me.uk> - 2013-09-26 09:47 +0100
    Re: Handling 3 operands in an expression without raising an exception Nobody <nobody@nowhere.com> - 2013-09-26 09:55 +0100
      Re: Handling 3 operands in an expression without raising an exception Νίκος <nikos.gr33k@gmail.com> - 2013-09-26 12:56 +0300
        Re: Handling 3 operands in an expression without raising an exception Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-09-26 12:12 +0200
          Re: Handling 3 operands in an expression without raising an exception Νίκος <nikos.gr33k@gmail.com> - 2013-09-26 13:18 +0300
            Re: Handling 3 operands in an expression without raising an exception Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-09-26 12:41 +0200
              Re: Handling 3 operands in an expression without raising an exception Νίκος <nikos.gr33k@gmail.com> - 2013-09-26 13:51 +0300
                Re: Handling 3 operands in an expression without raising an exception Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-09-26 13:07 +0200
                  Re: Handling 3 operands in an expression without raising an exception Νίκος <nikos.gr33k@gmail.com> - 2013-09-26 14:25 +0300
                    Re: Handling 3 operands in an expression without raising an exception Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-09-26 13:47 +0200
                    Re: Handling 3 operands in an expression without raising an exception Denis McMahon <denismfmcmahon@gmail.com> - 2013-09-26 14:42 +0000
                Re: Handling 3 operands in an expression without raising an exception Dave Angel <davea@davea.name> - 2013-09-26 11:24 +0000
                  Re: Handling 3 operands in an expression without raising an exception Νίκος <nikos.gr33k@gmail.com> - 2013-09-26 15:39 +0300
                    Re: Handling 3 operands in an expression without raising an exception Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-09-26 14:53 +0200
                      Re: Handling 3 operands in an expression without raising an exception Νίκος <nikos.gr33k@gmail.com> - 2013-09-26 16:34 +0300
                        Re: Handling 3 operands in an expression without raising an exception Dave Angel <davea@davea.name> - 2013-09-26 14:22 +0000
                          Re: Handling 3 operands in an expression without raising an exception Νίκος <nikos.gr33k@gmail.com> - 2013-09-26 19:58 +0300
                            Re: Handling 3 operands in an expression without raising an exception Denis McMahon <denismfmcmahon@gmail.com> - 2013-09-26 20:16 +0000
                              Re: Handling 3 operands in an expression without raising an exception Νίκος <nikos.gr33k@gmail.com> - 2013-09-27 01:14 +0300
                                Re: Handling 3 operands in an expression without raising an exception Dave Angel <davea@davea.name> - 2013-09-26 22:55 +0000
                                  Re: Handling 3 operands in an expression without raising an exception Νίκος <nikos.gr33k@gmail.com> - 2013-09-27 12:19 +0300
                                    Re: Handling 3 operands in an expression without raising an exception Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-27 09:26 +0000
                                      Re: Handling 3 operands in an expression without raising an exception Νίκος <nikos.gr33k@gmail.com> - 2013-09-27 13:04 +0300
                                    Re: Handling 3 operands in an expression without raising an exception Dave Angel <davea@davea.name> - 2013-09-27 10:43 +0000
                                      Re: Handling 3 operands in an expression without raising an exception Νίκος <nikos.gr33k@gmail.com> - 2013-09-27 14:15 +0300
                                        Re: Handling 3 operands in an expression without raising an exception Dave Angel <davea@davea.name> - 2013-09-27 12:17 +0000
                                          Re: Handling 3 operands in an expression without raising an exception Νίκος <nikos.gr33k@gmail.com> - 2013-09-27 15:51 +0300
                                      Re: Handling 3 operands in an expression without raising an exception rusi <rustompmody@gmail.com> - 2013-09-28 10:29 -0700
                                    Re: Handling 3 operands in an expression without raising an exception Denis McMahon <denismfmcmahon@gmail.com> - 2013-09-27 15:16 +0000
                                      Re: Handling 3 operands in an expression without raising an exception Grant Edwards <invalid@invalid.invalid> - 2013-09-27 16:04 +0000
                                      Re: Handling 3 operands in an expression without raising an exception Νίκος <nikos.gr33k@gmail.com> - 2013-09-27 19:47 +0300
                                        Re: Handling 3 operands in an expression without raising an exception Neil Cerutti <neilc@norwich.edu> - 2013-09-27 16:56 +0000
                                        Re: Handling 3 operands in an expression without raising an exception Grant Edwards <invalid@invalid.invalid> - 2013-09-27 17:00 +0000
                                          Re: Handling 3 operands in an expression without raising an exception Joel Goldstick <joel.goldstick@gmail.com> - 2013-09-27 13:19 -0400
                                          Re: Handling 3 operands in an expression without raising an exception Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-09-27 18:32 +0100
                                            Re: Handling 3 operands in an expression without raising an exception Denis McMahon <denismfmcmahon@gmail.com> - 2013-09-27 18:49 +0000
                                              Re: Handling 3 operands in an expression without raising an exception Grant Edwards <invalid@invalid.invalid> - 2013-09-27 18:54 +0000
                                                Re: Handling 3 operands in an expression without raising an exception Joel Goldstick <joel.goldstick@gmail.com> - 2013-09-27 15:21 -0400
                                          Re: Handling 3 operands in an expression without raising an exception Νίκος <nikos.gr33k@gmail.com> - 2013-09-28 01:06 +0300
                                            Re: Handling 3 operands in an expression without raising an exception Dave Angel <davea@davea.name> - 2013-09-27 22:26 +0000
                                              Re: Handling 3 operands in an expression without raising an exception Νίκος <nikos.gr33k@gmail.com> - 2013-09-28 12:33 +0300
                                                Re: Handling 3 operands in an expression without raising an exception Chris Angelico <rosuav@gmail.com> - 2013-09-28 20:19 +1000
                                                  Re: Handling 3 operands in an expression without raising an exception Νίκος <nikos.gr33k@gmail.com> - 2013-09-28 16:23 +0300
                                                    Re: Handling 3 operands in an expression without raising an exception Andreas Perstinger <andipersti@gmail.com> - 2013-09-28 17:02 +0200
                                                      Re: Handling 3 operands in an expression without raising an exception Νίκος <nikos.gr33k@gmail.com> - 2013-09-28 19:14 +0300
                                                        Re: Handling 3 operands in an expression without raising an exception Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-09-28 18:53 +0100
                                                        Re: Handling 3 operands in an expression without raising an exception Joel Goldstick <joel.goldstick@gmail.com> - 2013-09-28 14:21 -0400
                                                        Re: Handling 3 operands in an expression without raising an exception Zero Piraeus <z@etiol.net> - 2013-09-28 15:38 -0300
                                                          Re: Handling 3 operands in an expression without raising an exception Neil Cerutti <neilc@norwich.edu> - 2013-09-30 11:32 +0000
                                                            Re: Handling 3 operands in an expression without raising an exception Chris Angelico <rosuav@gmail.com> - 2013-09-30 21:53 +1000
                                                              Re: Handling 3 operands in an expression without raising an exception Neil Cerutti <neilc@norwich.edu> - 2013-09-30 12:56 +0000
                                                        Re: Handling 3 operands in an expression without raising an exception Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-09-28 19:45 +0100
                                                          Re: Handling 3 operands in an expression without raising an exception Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-28 19:20 +0000
                                                        Re: Handling 3 operands in an expression without raising an exception Chris Angelico <rosuav@gmail.com> - 2013-09-29 09:04 +1000
                                                          Re: Handling 3 operands in an expression without raising an exception Νίκος <nikos.gr33k@gmail.com> - 2013-09-29 10:35 +0300
                                                            Re: Handling 3 operands in an expression without raising an exception Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-09-29 12:35 +0300
                                                              Re: Handling 3 operands in an expression without raising an exception Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-29 09:48 +0000
                                                                Re: Handling 3 operands in an expression without raising an exception Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-09-29 14:04 +0300
                                                                  Re: Handling 3 operands in an expression without raising an exception Νίκος <nikos.gr33k@gmail.com> - 2013-09-29 14:14 +0300
                                                                    Re: Handling 3 operands in an expression without raising an exception Dave Angel <davea@davea.name> - 2013-09-29 11:27 +0000
                                                                      Re: Handling 3 operands in an expression without raising an exception Νίκος <nikos.gr33k@gmail.com> - 2013-09-29 14:34 +0300
                                                                        Re: Handling 3 operands in an expression without raising an exception Denis McMahon <denismfmcmahon@gmail.com> - 2013-09-29 21:15 +0000
                                                                          Re: Handling 3 operands in an expression without raising an exception Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-09-30 16:07 +0100
                                                                    Re: Handling 3 operands in an expression without raising an exception Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-09-29 15:24 +0300
                                                            Re: Handling 3 operands in an expression without raising an exception Dave Angel <davea@davea.name> - 2013-09-29 09:50 +0000
                                                              Re: Handling 3 operands in an expression without raising an exception Νίκος <nikos.gr33k@gmail.com> - 2013-09-29 13:17 +0300
                                                                Re: Handling 3 operands in an expression without raising an exception Dave Angel <davea@davea.name> - 2013-09-29 10:57 +0000
                                                                Re: Handling 3 operands in an expression without raising an exception Chris Angelico <rosuav@gmail.com> - 2013-09-29 23:58 +1000
                                                                Re: Handling 3 operands in an expression without raising an exception Denis McMahon <denismfmcmahon@gmail.com> - 2013-09-29 21:01 +0000
                                                              Re: Handling 3 operands in an expression without raising an exception Νίκος <nikos.gr33k@gmail.com> - 2013-09-29 14:06 +0300
                                                            Re: Handling 3 operands in an expression without raising an exception Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-09-29 12:48 +0200
                                                              Re: Handling 3 operands in an expression without raising an exception Piet van Oostrum <piet@vanoostrum.org> - 2013-09-29 13:24 -0400
                                                                Re: Handling 3 operands in an expression without raising an exception MRAB <python@mrabarnett.plus.com> - 2013-09-29 18:44 +0100
                                                                Re: Handling 3 operands in an expression without raising an exception Joel Goldstick <joel.goldstick@gmail.com> - 2013-09-29 14:45 -0400
                                                                Re: Handling 3 operands in an expression without raising an exception Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-09-30 16:05 +0100
                                                        Re: Handling 3 operands in an expression without raising an exception Ben Finney <ben+python@benfinney.id.au> - 2013-09-29 11:35 +1000
                                            Re: Handling 3 operands in an expression without raising an exception Chris Angelico <rosuav@gmail.com> - 2013-09-28 11:59 +1000
                                              Re: Handling 3 operands in an expression without raising an exception Νίκος <nikos.gr33k@gmail.com> - 2013-09-28 12:14 +0300
                                            Re: Handling 3 operands in an expression without raising an exception Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-09-28 15:53 +0200
                                            Re: Handling 3 operands in an expression without raising an exception Joel Goldstick <joel.goldstick@gmail.com> - 2013-09-28 12:15 -0400
                                    Re: Handling 3 operands in an expression without raising an exception Robert Kern <robert.kern@gmail.com> - 2013-09-28 12:18 +0100
                                    Re: Handling 3 operands in an expression without raising an exception giacomo boffi <pecore@pascolo.net> - 2013-09-29 17:28 +0200
                                      Re: Handling 3 operands in an expression without raising an exception Joel Goldstick <joel.goldstick@gmail.com> - 2013-09-29 12:07 -0400
                                        Re: Handling 3 operands in an expression without raising an exception giacomo boffi <pecore@pascolo.net> - 2013-09-29 23:39 +0200
                                          Re: Handling 3 operands in an expression without raising an exception giacomo boffi <pecore@pascolo.net> - 2013-09-29 23:49 +0200
                                      Re: Handling 3 operands in an expression without raising an exception Chris Angelico <rosuav@gmail.com> - 2013-09-30 02:18 +1000
                            Re: Handling 3 operands in an expression without raising an exception Dave Angel <davea@davea.name> - 2013-09-26 20:37 +0000
        Re: Handling 3 operands in an expression without raising an exception Denis McMahon <denismfmcmahon@gmail.com> - 2013-09-26 14:38 +0000
        Re: Handling 3 operands in an expression without raising an exception Denis McMahon <denismfmcmahon@gmail.com> - 2013-09-26 14:38 +0000
    Re: Handling 3 operands in an expression without raising an exception Gene Heskett <gheskett@wdtv.com> - 2013-09-29 13:32 -0400

Page 1 of 6  [1] 2 3 4 5 6  Next page →


#54785 — Handling 3 operands in an expression without raising an exception

FromΝίκος <nikos.gr33k@gmail.com>
Date2013-09-26 10:26 +0300
SubjectHandling 3 operands in an expression without raising an exception
Message-ID<l20njn$uhh$1@dont-email.me>
Hello,

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 
gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or "Άγνωστη Πόλη"

host = socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0] or 
socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] 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.

I believe in a KeyError is missing the expression cannot even be 
evaluates as Truthy or Falsy.

Then i thought of os.environ.get() to default to something but then 
again we have 3 operand in the expression.

[toc] | [next] | [standalone]


#54786

FromJussi Piitulainen <jpiitula@ling.helsinki.fi>
Date2013-09-26 10:48 +0300
Message-ID<qotmwn0hvlx.fsf@ruuvi.it.helsinki.fi>
In reply to#54785
Νίκος 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.

[toc] | [prev] | [next] | [standalone]


#54787

FromΝίκος <nikos.gr33k@gmail.com>
Date2013-09-26 10:51 +0300
Message-ID<l20p2u$43b$1@dont-email.me>
In reply to#54786
Στις 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.

[toc] | [prev] | [next] | [standalone]


#54788

FromJussi Piitulainen <jpiitula@ling.helsinki.fi>
Date2013-09-26 11:12 +0300
Message-ID<qotioxohuhx.fsf@ruuvi.it.helsinki.fi>
In reply to#54787
Νίκος 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?

[toc] | [prev] | [next] | [standalone]


#54789

FromΝίκος <nikos.gr33k@gmail.com>
Date2013-09-26 11:39 +0300
Message-ID<l20rrk$i2u$2@dont-email.me>
In reply to#54788
Στις 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.

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.

[toc] | [prev] | [next] | [standalone]


#54791

FromΝίκος <nikos.gr33k@gmail.com>
Date2013-09-26 11:46 +0300
Message-ID<l20s8p$kkk$1@dont-email.me>
In reply to#54789
Στις 26/9/2013 11:39 πμ, ο/η Νίκος έγραψε:
> Στις 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.
>
> 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.

Let me try be more specific:

i want to switch this:

city = gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] ) or 
gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or "Άγνωστη Πόλη"

host = socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0] or 
socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] or "Άγνωστη Προέλευση"

because it raises KeyError exceptions to this:

city = gi.time_zone_by_addr( os.environ.get('HTTP_CF_CONNECTING_IP', 
'REMOTE_ADDR') )    or "Άγνωστη Πόλη"

host = socket.gethostbyaddr( os.environ.get('HTTP_CF_CONNECTING_IP', 
'REMOTE_ADDR') )[0] or "Άγνωστη Προέλευση"

But that doesnt seem to also work.
I want to use the get method because i know if it doesnt does detect a 
dictionary key item than default back what we give it.

[toc] | [prev] | [next] | [standalone]


#54792

FromΝίκος <nikos.gr33k@gmail.com>
Date2013-09-26 11:50 +0300
Message-ID<l20sgg$kkk$2@dont-email.me>
In reply to#54791
Στις 26/9/2013 11:46 πμ, ο/η Νίκος έγραψε:
> Στις 26/9/2013 11:39 πμ, ο/η Νίκος έγραψε:
>> Στις 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.
>>
>> 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.
>
> Let me try be more specific:
>
> i want to switch this:
>
> city = gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] ) or
> gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or "Άγνωστη Πόλη"
>
> host = socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0] or
> socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] or "Άγνωστη Προέλευση"
>
> because it raises KeyError exceptions to this:
>
> city = gi.time_zone_by_addr( os.environ.get('HTTP_CF_CONNECTING_IP',
> 'REMOTE_ADDR') )    or "Άγνωστη Πόλη"
>
> host = socket.gethostbyaddr( os.environ.get('HTTP_CF_CONNECTING_IP',
> 'REMOTE_ADDR') )[0] or "Άγνωστη Προέλευση"
>
> But that doesnt seem to also work.
> I want to use the get method because i know if it doesnt does detect a
> dictionary key item than default back what we give it.

Can you please tell me why my alternative fails to work although i'am 
using the .get method to default to something?

[toc] | [prev] | [next] | [standalone]


#54797

FromJussi Piitulainen <jpiitula@ling.helsinki.fi>
Date2013-09-26 12:20 +0300
Message-ID<qottxh8os69.fsf@ruuvi.it.helsinki.fi>
In reply to#54792
Νίκος writes:

> > because it raises KeyError exceptions to this:
> >
> > city = gi.time_zone_by_addr( os.environ.get('HTTP_CF_CONNECTING_IP',
> > 'REMOTE_ADDR') )    or "�γν���η Π�λη"
> >
> > host = socket.gethostbyaddr( os.environ.get('HTTP_CF_CONNECTING_IP',
> > 'REMOTE_ADDR') )[0] or "�γν���η Π�οέλε���η"
> >
> > But that doesnt seem to also work.
> > I want to use the get method because i know if it doesnt does detect a
> > dictionary key item than default back what we give it.
> 
> Can you please tell me why my alternative fails to work although i'am
> using the .get method to default to something?

I lost connection.

Absent HTTP_CF_CONNECTING_IP in os.environ, these reduce to

city = ( gi.time_zone_by_addr('REMOTE_ADDR') or
         "�γν���η Π�λη" )

host = ( socket.gethostbyaddr('REMOTE_ADDR')[0] or
         "�γν���η Π�οέλε���η" )

so maybe 'REMOTE_ADDR' is not meant to be the actual argument in those
calls. Earlier versions tried to get 'REMOTE_ADDR' from os.environ, I
think.

Try Nobody's piecemeal approach, maybe.

[toc] | [prev] | [next] | [standalone]


#54795

FromJussi Piitulainen <jpiitula@ling.helsinki.fi>
Date2013-09-26 12:04 +0300
Message-ID<qotzjr0osxe.fsf@ruuvi.it.helsinki.fi>
In reply to#54789
Νίκος 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)

[toc] | [prev] | [next] | [standalone]


#54800

FromΝίκος <nikos.gr33k@gmail.com>
Date2013-09-26 12:45 +0300
Message-ID<l20voh$640$3@dont-email.me>
In reply to#54795
Στις 26/9/2013 12:04 μμ, ο/η Jussi Piitulainen έγραψε:

>> 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
>                              "�γν����·Π�λη")

Okey that indeed doesn't hit on that lines.

Now my statements are as follows:

city = gi.time_zone_by_addr( os.environ.get('HTTP_CF_CONNECTING_IP') or 
os.environ.get('REMOTE_ADDR') or "Άγνωστη Πόλη" )

host = socket.gethostbyaddr( os.environ.get('HTTP_CF_CONNECTING_IP') or 
os.environ.get('REMOTE_ADDR') or  "Άγνωστη Προέλευση" )

So you include everything in aprenthseis with a get method that you know 
beforehand it will not raise an exception but continue to check the next 
operand and the next until it default to the string.

Its more clear this way: but look. this way now produced a weird error 
few line slater when 'host' is tryign to be identefied by 're'.

[Thu Sep 26 09:44:39 2013] [error] [client 108.162.250.63]   File 
"/home/nikos/public_html/cgi-bin/metrites.py", line 181, in <module>
[Thu Sep 26 09:44:39 2013] [error] [client 108.162.250.63]     if not 
vip and re.search( 
r'(msn|gator|amazon|yandex|reverse|who|cloudflare|fetch|barracuda|spider|google|crawl|pingdom)', 
host ) is None:
[Thu Sep 26 09:44:39 2013] [error] [client 108.162.250.63]   File 
"/usr/local/bin/python/lib/python3.3/re.py", line 161, in search
[Thu Sep 26 09:44:39 2013] [error] [client 108.162.250.63]     return 
_compile(pattern, flags).search(string)
[Thu Sep 26 09:44:39 2013] [error] [client 108.162.250.63] TypeError: 
expected string or buffer

[toc] | [prev] | [next] | [standalone]


#54796

FromAntoon Pardon <antoon.pardon@rece.vub.ac.be>
Date2013-09-26 11:07 +0200
Message-ID<mailman.343.1380186457.18130.python-list@python.org>
In reply to#54789
Op 26-09-13 10:39, Νίκος schreef:
> Στις 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.

That is irrelevant. For the responsibility you have taken upon yourself,
it seems you should have experience enough to figure this out. If you
can't, you just are not up for the job.

> 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.

Experiment and find out for yourself. That is the only way you will
acquire the experience and understanding you need. Sure we could
spoon feed you the line you need, but that will only result in you
using that line without any understanding of what you are doing so
that if in a few months time something goes wrong, you again will
have no understanding of what goes on and still will have no clue
on how to handle a problem.

-- 
Antoon Pardon

[toc] | [prev] | [next] | [standalone]


#54799

FromΝίκος <nikos.gr33k@gmail.com>
Date2013-09-26 12:31 +0300
Message-ID<l20utg$278$1@dont-email.me>
In reply to#54796
Στις 26/9/2013 12:07 μμ, ο/η Antoon Pardon έγραψε:
> Experiment and find out for yourself. That is the only way you will
> acquire the experience and understanding you need. Sure we could
> spoon feed you the line you need, but that will only result in you
> using that line without any understanding of what you are doing so
> that if in a few months time something goes wrong, you again will
> have no understanding of what goes on and still will have no clue
> on how to handle a problem.

I have tried code and also provided alternative code to solve my problem 
which also doesn't solve it.

So, you cannot accuse me that i'm not trying, that would be the case to 
just ask for a line without trying anything for myself, which i did twice.

Then i got entirely stuck and i had to ask to get unstuck.

Also when a solution is being provided to me i do not only want to use 
it but want to understand too, so if something similar comes up i will 
be in a position to solve it that time.

[toc] | [prev] | [next] | [standalone]


#54801

FromMaarten <maarten.sneep@knmi.nl>
Date2013-09-26 02:52 -0700
Message-ID<d7b0cda4-c348-4e51-9b5d-6f1a50061eed@googlegroups.com>
In reply to#54799
On Thursday, September 26, 2013 11:31:29 AM UTC+2, Ferrous Cranus wrote:

> I have tried code and also provided alternative code to solve my problem 
> which also doesn't solve it.
> 
> So, you cannot accuse me that i'm not trying, that would be the case to 
> just ask for a line without trying anything for myself, which i did twice.

I'll accuse you of being a dimwit. The questions you ask do not show you trying. The code that _is_ posted is not straightforward or geared towards readability.

> Also when a solution is being provided to me i do not only want to use 
> it but want to understand too, so if something similar comes up i will 
> be in a position to solve it that time.

If you really want to do that, then why do you insist on one-liners? Write out the logic with a sequence of if statements. That is easy to understand, gives clearer error messages. The solution by Nobody is fairly foolproof, and the logic is clear. What's not to like? 

Maarten

[toc] | [prev] | [next] | [standalone]


#54803

FromAntoon Pardon <antoon.pardon@rece.vub.ac.be>
Date2013-09-26 12:01 +0200
Message-ID<mailman.344.1380189696.18130.python-list@python.org>
In reply to#54799
Op 26-09-13 11:31, Νίκος schreef:
> Στις 26/9/2013 12:07 μμ, ο/η Antoon Pardon έγραψε:
>> Experiment and find out for yourself. That is the only way you will
>> acquire the experience and understanding you need. Sure we could
>> spoon feed you the line you need, but that will only result in you
>> using that line without any understanding of what you are doing so
>> that if in a few months time something goes wrong, you again will
>> have no understanding of what goes on and still will have no clue
>> on how to handle a problem.
> 
> I have tried code and also provided alternative code to solve my problem
> which also doesn't solve it.

Have you tried with some kind of understanding of what you are doing or
did you just tried variations of examples you saw somewhere?

> So, you cannot accuse me that i'm not trying, that would be the case to
> just ask for a line without trying anything for myself, which i did twice.

That seems totally insufficient.

> Then i got entirely stuck and i had to ask to get unstuck.

That is your problem. You are putting all your energy in trying to
get unstuck instead of in learning so that you gain enough understanding
to get yourself unstuck.

> Also when a solution is being provided to me i do not only want to use
> it but want to understand too, so if something similar comes up i will
> be in a position to solve it that time.

You have shown no evidence that this is how you operate in reality. You
like to paint yourself like this, but in reality people still have to
explain to you that when you encouter a syntax error, you may have to
look at previous lines, while that was already explained to you a
number of times. You have a history of coming with the same kind of
problem than previous once that were solved and explained to you.

-- 
Antoon Pardon

[toc] | [prev] | [next] | [standalone]


#54810

From"James Harris" <james.harris.1@gmail.com>
Date2013-09-26 11:53 +0100
Message-ID<l213md$pou$1@dont-email.me>
In reply to#54799
"?????" <nikos.gr33k@gmail.com> wrote in message 
news:l20utg$278$1@dont-email.me...
> ???? 26/9/2013 12:07 ??, ?/? Antoon Pardon ??????:
>> Experiment and find out for yourself. That is the only way you will
>> acquire the experience and understanding you need. Sure we could
>> spoon feed you the line you need, but that will only result in you
>> using that line without any understanding of what you are doing so
>> that if in a few months time something goes wrong, you again will
>> have no understanding of what goes on and still will have no clue
>> on how to handle a problem.
>
> I have tried code and also provided alternative code to solve my problem 
> which also doesn't solve it.
>
> So, you cannot accuse me that i'm not trying, that would be the case to 
> just ask for a line without trying anything for myself, which i did twice.

Agreed. You did post the work you had already done which seems reasonable to 
me.

For your first example, because you are worried about key errors maybe you 
could code something like the following.

  try:
    city = gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
  except KeyError:
    try:
      city = gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] )
    except KeyError:
      city = "??????? ????"

Does that help?

James

[toc] | [prev] | [next] | [standalone]


#54857

Fromalex23 <wuwei23@gmail.com>
Date2013-09-27 10:05 +1000
Message-ID<l22i54$8u0$1@dont-email.me>
In reply to#54789
On 26/09/2013 6:39 PM, Νίκος wrote:
> You dont have to be ironic. I dont have the experience you do.

This is bulshytt. You only just a few days ago started a thread in which 
handling lookups in dictionaries with potentially non-existent keys was 
dealt with. At length. And now you're restructuring pretty much the 
exact same question to troll the list. Again.

You're either not reading what people are saying, not trying to 
understand things by working with simpler examples at the interpreter, 
or just flat out lying. In any case, you're not acting in good faith.

[toc] | [prev] | [next] | [standalone]


#54790

FromChris Angelico <rosuav@gmail.com>
Date2013-09-26 18:41 +1000
Message-ID<mailman.341.1380184900.18130.python-list@python.org>
In reply to#54788
On Thu, Sep 26, 2013 at 6:12 PM, Jussi Piitulainen
<jpiitula@ling.helsinki.fi> wrote:
> 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?

Speak, Python, speak!
We're all attention.
The news we seek
This moment mention!

ChrisA

[toc] | [prev] | [next] | [standalone]


#54793

FromTim Golden <mail@timgolden.me.uk>
Date2013-09-26 09:47 +0100
Message-ID<mailman.342.1380185456.18130.python-list@python.org>
In reply to#54788
On 26/09/2013 09:41, Chris Angelico wrote:
> On Thu, Sep 26, 2013 at 6:12 PM, Jussi Piitulainen
> <jpiitula@ling.helsinki.fi> wrote:
>> 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?
> 
> Speak, Python, speak!
> We're all attention.
> The news we seek
> This moment mention!

I remember when I saw that in a production in Altrincham, the Prince
threw off his cloak dramatically to step forward-- and the clasp caught
on some part of his costume, reducing the effect somewhat as the cloak
dragged along behind. (In the end some other cast member came forward
and just ripped it free).

[G&S The Gondoliers, for those who weren't following along]

TJG

[toc] | [prev] | [next] | [standalone]


#54794

FromNobody <nobody@nowhere.com>
Date2013-09-26 09:55 +0100
Message-ID<pan.2013.09.26.08.55.29.461000@nowhere.com>
In reply to#54785
On Thu, 26 Sep 2013 10:26:48 +0300, Νίκος wrote:

> 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
> gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or
> "Άγνωστη Πόλη"

tz = None
ip = os.environ.get('HTTP_CF_CONNECTING_IP')
if ip:
  tz = gi.time_zone_by_addr(ip)
if not tz:
  ip = os.environ.get('REMOTE_ADDR')
  if ip:
    tz = gi.time_zone_by_addr(ip)
if not tz:
  tz = "ÎγνÏÏÏη Î Ïλη"

Likewise for the hostname.

[toc] | [prev] | [next] | [standalone]


#54802

FromΝίκος <nikos.gr33k@gmail.com>
Date2013-09-26 12:56 +0300
Message-ID<l210c2$95b$1@dont-email.me>
In reply to#54794
Στις 26/9/2013 11:55 πμ, ο/η Nobody έγραψε:
> On Thu, 26 Sep 2013 10:26:48 +0300, Νίκος wrote:
>
>> 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
>> gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or
>> "Άγνωστη Πόλη"
>
> tz = None
> ip = os.environ.get('HTTP_CF_CONNECTING_IP')
> if ip:
>    tz = gi.time_zone_by_addr(ip)
> if not tz:
>    ip = os.environ.get('REMOTE_ADDR')
>    if ip:
>      tz = gi.time_zone_by_addr(ip)
> if not tz:
>    tz = "ÎγνÏÏÏη Î Ïλη"
>
> Likewise for the hostname.
>

Its logic is simple and straightforward but too many lines:

host = socket.gethostbyaddr( os.environ.get('HTTP_CF_CONNECTING_IP') or 
os.environ.get('REMOTE_ADDR') or  "Άγνωστη Προέλευση" )

this is much better in my opinion and straighforward also and more clear 
to read:

it doens't work though:

ima thinkin that the [0] is missign at the end so i cna grab the first 
item of the returnign tuple:

host = socket.gethostbyaddr( os.environ.get('HTTP_CF_CONNECTING_IP') or 
os.environ.get('REMOTE_ADDR') or  "Άγνωστη Προέλευση" )[0]

for the first two element if they are beign return i cna understand the 
[0] at the end but what about if the string is being return wouldnt that 
return just the 'A' of the string?

Is it correct to write it liek this?

[toc] | [prev] | [next] | [standalone]


Page 1 of 6  [1] 2 3 4 5 6  Next page →

Back to top | Article view | comp.lang.python


csiph-web