Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #54785 > unrolled thread
| Started by | Νίκος <nikos.gr33k@gmail.com> |
|---|---|
| First post | 2013-09-26 10:26 +0300 |
| Last post | 2013-09-29 13:32 -0400 |
| Articles | 20 on this page of 107 — 25 participants |
Back to article view | Back to comp.lang.python
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 3 of 6 — ← Prev page 1 2 [3] 4 5 6 Next page →
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-09-27 10:43 +0000 |
| Message-ID | <mailman.375.1380278663.18130.python-list@python.org> |
| In reply to | #54867 |
On 27/9/2013 05:19, Νίκος wrote:
> Στις 27/9/2013 1:55 πμ, ο/η Dave Angel έγραψε:
>>
>> Simply assign the default values BEFORE the try block, and use pass as
>> the except block.
>>
>> That still doesn't get around the inadvisability of putting those 3
>> lines in the try block.
>>
>> You still haven't dealt with the gt assignment and its possible
>> exception.
>>
> Yes gi must be removed form within the try block because iam tesign it
> for failure.
Then why don't you do it below?
>
> I'am not sure what you mean though when you say:
>
>> Simply assign the default values BEFORE the try block, and use pass as
>> the except block.
>
> Can you please make it more clear for me?
>
> This is my code as i have it at the moment:
>
> 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:
> city = "Άγνωστη Πόλη"
> host = "Άγνωστη Προέλευση"
ipval = ( os.environ.get('HTTP_CF_CONNECTING_IP') or
os.environ.get('REMOTE_ADDR', "Cannot Resolve") )
city = "Άγνωστη Πόλη"
host = "Άγνωστη Προέλευση"
try:
city = gi.time_zone_by_addr( ipval )
host = socket.gethostbyaddr( ipval ) [0]
except socket.gaierror as e:
pass
>
> Or even better since i actually use 3 vars inside the try block it would
> be really neat to be able to detect which far failed and assign a
> specific value variable failed depended.
ipval = ( os.environ.get('HTTP_CF_CONNECTING_IP') or
os.environ.get('REMOTE_ADDR', "Cannot Resolve") )
city = None
host = None
try:
city = gi.time_zone_by_addr( ipval )
host = socket.gethostbyaddr( ipval ) [0]
except socket.gaierror as e:
if not city:
city = "Άγνωστη Πόλη"
if not host:
host = "Άγνωστη Προέλευση"
or:
ipval = ( os.environ.get('HTTP_CF_CONNECTING_IP') or
os.environ.get('REMOTE_ADDR', "Cannot Resolve") )
city = None
host = None
try:
city = gi.time_zone_by_addr( ipval )
host = socket.gethostbyaddr( ipval ) [0]
except socket.gaierror as e:
if not host:
host = "Άγνωστη Προέλευση"
if not city:
city = "Άγνωστη Πόλη"
Note that in that last case, I tested the variables in the reverse
order. Note also on both of these that I assumed that None is not a
reasonable value for either of those. In fact, I also assume that an
empty string is not a reasonable value. If I didn't like the latter
assumption, I'd have used tests like if host is None:
>
> IF it can also be written in one-line afteer the excpect would be even
> better. Call me persistent but when i can write somethign in 1-line i
> wou;d prefer it over 3-lines.
Ridiculous. But if you like code golf, you can always do:
city, host = "Άγνωστη Πόλη", "Άγνωστη Προέλευση"
You should study APL. Many functions were written in one line, with
twenty lines of explanation. The function itself was considered
unreadable nonsense. And if a function stopped working, general wisdom
was to throw it out, and re-implement the explanation. I studied it
briefly in class in 1970, and have no idea if there are current
implementations.
--
DaveA
[toc] | [prev] | [next] | [standalone]
| From | Νίκος <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-09-27 14:15 +0300 |
| Message-ID | <l23pc6$kfv$1@dont-email.me> |
| In reply to | #54872 |
Στις 27/9/2013 1:43 μμ, ο/η Dave Angel έγραψε:
>> Στις 27/9/2013 1:55 πμ, ο/η Dave Angel έγραψε:
>>> Simply assign the default values BEFORE the try block, and use pass as
>>> the except block.
>>>
>>> That still doesn't get around the inadvisability of putting those 3
>>> lines in the try block.
>>>
>>> You still haven't dealt with the gt assignment and its possible
>>> exception.
>> Yes gi must be removed form within the try block because iam tesign it
>> for failure.
> Then why don't you do it below?
>> I'am not sure what you mean though when you say:
>>> Simply assign the default values BEFORE the try block, and use pass as
>>> the except block.
>> Can you please make it more clear for me?
>>
>> This is my code as i have it at the moment:
>>
>> 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:
>> city = "Άγνωστη Πόλη"
>> host = "Άγνωστη Προέλευση"
> ipval = ( os.environ.get('HTTP_CF_CONNECTING_IP') or
> os.environ.get('REMOTE_ADDR', "Cannot Resolve") )
> city = "Άγνωστη Πόλη"
> host = "Άγνωστη Προέλευση"
> try:
> city = gi.time_zone_by_addr( ipval )
> host = socket.gethostbyaddr( ipval ) [0]
> except socket.gaierror as e:
> pass
Thanks for taking the time to expain this:
In the exact above solution of yours opposed to the top of mines is that
you code has the benefit of actually identifying the variable that
failed to have been assigned a value while in my code no matter what
variable failes in the try block i assign string to both 'city' and
'host' hence i dont really know which one of them is failing?
Di i understood it correctly?
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-09-27 12:17 +0000 |
| Message-ID | <mailman.381.1380284256.18130.python-list@python.org> |
| In reply to | #54873 |
On 27/9/2013 07:15, Νίκος wrote:
> Στις 27/9/2013 1:43 μμ, ο/η Dave Angel έγραψε:
<snip>
>
>> ipval = ( os.environ.get('HTTP_CF_CONNECTING_IP') or
>> os.environ.get('REMOTE_ADDR', "Cannot Resolve") )
>> city = "Άγνωστη Πόλη"
>> host = "Άγνωστη Προέλευση"
>> try:
>> city = gi.time_zone_by_addr( ipval )
>> host = socket.gethostbyaddr( ipval ) [0]
>> except socket.gaierror as e:
>> pass
>
> Thanks for taking the time to expain this:
>
> In the exact above solution of yours opposed to the top of mines is that
No idea what you mean here. I'll assume you're talking about the
version you DON'T quote here, the one with the if statements in the
except clause.
> you code has the benefit of actually identifying the variable that
> failed to have been assigned a value while in my code no matter what
> variable failes in the try block i assign string to both 'city' and
> 'host' hence i dont really know which one of them is failing?
Yes, it has that advantage. Of course it doesn't USE that advantage for
anything so I'd go back to the pass version quoted immediately above.
Or I'd have two separate try/except clauses, one for each function call.
After all, the implication of the failure is different for each.
>
> Di i understood it correctly?
I think so.
--
DaveA
[toc] | [prev] | [next] | [standalone]
| From | Νίκος <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-09-27 15:51 +0300 |
| Message-ID | <l23uvm$gvm$1@dont-email.me> |
| In reply to | #54879 |
Στις 27/9/2013 3:17 μμ, ο/η Dave Angel έγραψε:
> On 27/9/2013 07:15, Νίκος wrote:
>
>> Στις 27/9/2013 1:43 μμ, ο/η Dave Angel έγραψε:
>
> <snip>
>>
>>> ipval = ( os.environ.get('HTTP_CF_CONNECTING_IP') or
>>> os.environ.get('REMOTE_ADDR', "Cannot Resolve") )
>>> city = "Άγνωστη Πόλη"
>>> host = "Άγνωστη Προέλευση"
>>> try:
>>> city = gi.time_zone_by_addr( ipval )
>>> host = socket.gethostbyaddr( ipval ) [0]
>>> except socket.gaierror as e:
>>> pass
>>
>> Thanks for taking the time to expain this:
>>
>> In the exact above solution of yours opposed to the top of mines is that
>
> No idea what you mean here. I'll assume you're talking about the
> version you DON'T quote here, the one with the if statements in the
> except clause.
>
>> you code has the benefit of actually identifying the variable that
>> failed to have been assigned a value while in my code no matter what
>> variable failes in the try block i assign string to both 'city' and
>> 'host' hence i dont really know which one of them is failing?
>
> Yes, it has that advantage. Of course it doesn't USE that advantage for
> anything so I'd go back to the pass version quoted immediately above.
> Or I'd have two separate try/except clauses, one for each function call.
> After all, the implication of the failure is different for each.
>
>
>>
>> Di i understood it correctly?
>
> I think so.
>
Thank you for everything, i appreciate your time and effort to help me
understand.
[toc] | [prev] | [next] | [standalone]
| From | rusi <rustompmody@gmail.com> |
|---|---|
| Date | 2013-09-28 10:29 -0700 |
| Message-ID | <27c9b4bf-4a7d-4be5-b2d8-2051ab7e398c@googlegroups.com> |
| In reply to | #54872 |
On Friday, September 27, 2013 4:13:52 PM UTC+5:30, Dave Angel wrote: > You should study APL. Many functions were written in one line, with > twenty lines of explanation. The function itself was considered > unreadable nonsense. And if a function stopped working, general wisdom > was to throw it out, and re-implement the explanation. I studied it > briefly in class in 1970, and have no idea if there are current > implementations. From a certain pov both python and APL have a similar/analogous weakness -- inadequate 'intermediate' structuring constructs. APL tries to be superman at the expression ie micro level Likewise what packages and modules are to python, workspaces are to APL (not used myself but I know that APLers swear by them) -- call that the macro level The intermediate level is the issue. Functional languages have the let/where clause to convert a block of defs into a single value. Even gcc extends C similarly: http://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html#Statement-Exprs I do occasionally miss something like that in python
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2013-09-27 15:16 +0000 |
| Message-ID | <l247g5$j2k$1@dont-email.me> |
| In reply to | #54867 |
On Fri, 27 Sep 2013 12:19:53 +0300, Νίκος wrote:
> This is my code as i have it at the moment:
>
> 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:
> city = "Άγνωστη Πόλη"
> host = "Άγνωστη Προέλευση"
Here is the basic problem: You're trying to do too many things at once in
a language you don't understand and without any understanding of the
fundamental underlying concepts of the systems that you're interfacing
with.
Break the task down into simpler steps and do the steps one at a time:
Calculate ipval
Calculate gi
If ipval is valid, calculate city, else give city a default value
If ipval is valid, calculate host, else give host a default value
Then consider which of the above needs to be contained within a try /
catch.
Finally, code them as 4 separate units of code, eg:
# instantiate geolocation by ip
gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat')
# calculate ipval
try:
ipval = ( os.environ.get('HTTP_CF_CONNECTING_IP') or
os.environ.get('REMOTE_ADDR', "Cannot Resolve") )
except ( KeyError, TypeError ):
ipval = some_default
# try and obtain time zone from ip
try:
city = gi.time_zone_by_addr( ipval )
except (error type[s]):
city = "Unknown City"
# try and obtain hostname from ip
try:
host = socket.gethostbyaddr( ipval ) [0]
except ( socket.gaierror ):
host = "Unknown Host"
Note that there is nothing special about writing it in as few lines as
code as possible. Writing it out in a way that is easy to understand and
follow helps make sure it actually works, and makes it a lot easier to
maintain in future.
--
Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Grant Edwards <invalid@invalid.invalid> |
|---|---|
| Date | 2013-09-27 16:04 +0000 |
| Message-ID | <l24a9k$b59$1@reader1.panix.com> |
| In reply to | #54890 |
On 2013-09-27, Denis McMahon <denismfmcmahon@gmail.com> wrote:
> Note that there is nothing special about writing it in as few lines as
> code as possible.
Hah! That's what they _want_ you to think. First they get you hooked
on newlines, then the newline mine owners form a cartel and start
jacking up the prices. Then you'll wish you'd spent more time
learning to write Perl one-liners...
--
Grant Edwards grant.b.edwards Yow! What GOOD is a
at CARDBOARD suitcase ANYWAY?
gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Νίκος <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-09-27 19:47 +0300 |
| Message-ID | <l24crk$2ss$1@dont-email.me> |
| In reply to | #54890 |
Στις 27/9/2013 6:16 μμ, ο/η Denis McMahon έγραψε:
> On Fri, 27 Sep 2013 12:19:53 +0300, Νίκος wrote:
>
>> This is my code as i have it at the moment:
>>
>> 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:
>> city = "Άγνωστη Πόλη"
>> host = "Άγνωστη Προέλευση"
>
> Here is the basic problem: You're trying to do too many things at once in
> a language you don't understand and without any understanding of the
> fundamental underlying concepts of the systems that you're interfacing
> with.
>
> Break the task down into simpler steps and do the steps one at a time:
>
> Calculate ipval
>
> Calculate gi
>
> If ipval is valid, calculate city, else give city a default value
>
> If ipval is valid, calculate host, else give host a default value
>
> Then consider which of the above needs to be contained within a try /
> catch.
>
> Finally, code them as 4 separate units of code, eg:
>
> # instantiate geolocation by ip
> gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat')
>
> # calculate ipval
> try:
> ipval = ( os.environ.get('HTTP_CF_CONNECTING_IP') or
> os.environ.get('REMOTE_ADDR', "Cannot Resolve") )
> except ( KeyError, TypeError ):
> ipval = some_default
>
> # try and obtain time zone from ip
> try:
> city = gi.time_zone_by_addr( ipval )
> except (error type[s]):
> city = "Unknown City"
>
> # try and obtain hostname from ip
> try:
> host = socket.gethostbyaddr( ipval ) [0]
> except ( socket.gaierror ):
> host = "Unknown Host"
>
> Note that there is nothing special about writing it in as few lines as
> code as possible. Writing it out in a way that is easy to understand and
> follow helps make sure it actually works, and makes it a lot easier to
> maintain in future.
>
Sure your method follows the the logic in a straighforward way
step-by-step but i just dont want to spent almost 20 lines of code just
to calculate 2 variables(city and host).
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 socket.gaierror as e:
print( "metrites.py => (%s): " % lastvisit, repr( sys.exc_info() ),
file=open('/tmp/err.out', 'w') )
The above code works in less lines of codes and because the assignment
of the vars happen before the try:/except: block, if something fails
during try: we can tell by looking at the values of city or host.
Also its being logged at the err.out too.
Only problem is that iam handlign address related issues and not the
case of when gi fails.
But i dont know how to do that.
except (error type[s]): i saw in your code but iam not sure what exactly
this is supposed to handle.
[toc] | [prev] | [next] | [standalone]
| From | Neil Cerutti <neilc@norwich.edu> |
|---|---|
| Date | 2013-09-27 16:56 +0000 |
| Message-ID | <balrm8F2841U1@mid.individual.net> |
| In reply to | #54896 |
On 2013-09-27, ?????????? <nikos.gr33k@gmail.com> wrote: > Sure your method follows the the logic in a straighforward way > step-by-step but i just dont want to spent almost 20 lines of > code just to calculate 2 variables(city and host). Sure, eating with my mouth is straightforward and step-by-step, but I just want to shove the food directly into my stomach. -- Neil Cerutti
[toc] | [prev] | [next] | [standalone]
| From | Grant Edwards <invalid@invalid.invalid> |
|---|---|
| Date | 2013-09-27 17:00 +0000 |
| Message-ID | <l24dkb$k2m$1@reader1.panix.com> |
| In reply to | #54896 |
On 2013-09-27, ?????????? <nikos.gr33k@gmail.com> wrote:
> Sure your method follows the the logic in a straighforward way
> step-by-step but i just dont want to spent almost 20 lines of code just
> to calculate 2 variables(city and host).
Does your provider charge you per line of code?
If all that matters is the number of lines of code then use this:
city,host = 0,0
Only _1_ nice short line of code.
Happy?
--
Grant Edwards grant.b.edwards Yow! If Robert Di Niro
at assassinates Walter Slezak,
gmail.com will Jodie Foster marry
Bonzo??
[toc] | [prev] | [next] | [standalone]
| From | Joel Goldstick <joel.goldstick@gmail.com> |
|---|---|
| Date | 2013-09-27 13:19 -0400 |
| Message-ID | <mailman.385.1380302400.18130.python-list@python.org> |
| In reply to | #54898 |
[Multipart message — attachments visible in raw view] — view raw
On Fri, Sep 27, 2013 at 1:00 PM, Grant Edwards <invalid@invalid.invalid>wrote: > On 2013-09-27, ?????????? <nikos.gr33k@gmail.com> wrote: > > > Sure your method follows the the logic in a straighforward way > > step-by-step but i just dont want to spent almost 20 lines of code just > > to calculate 2 variables(city and host). > > Does your provider charge you per line of code? > > If all that matters is the number of lines of code then use this: > > city,host = 0,0 > > Only _1_ nice short line of code. > > Happy? > > -- > Grant Edwards grant.b.edwards Yow! If Robert Di Niro > at assassinates Walter > Slezak, > gmail.com will Jodie Foster marry > Bonzo?? > -- > https://mail.python.org/mailman/listinfo/python-list > Better yet, don't write the program. Zero lines of code, and every line works perfectly. It runs extremely fast too! ... or Nikos the annoyance, write it in apl -- Joel Goldstick http://joelgoldstick.com
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2013-09-27 18:32 +0100 |
| Message-ID | <mailman.387.1380303169.18130.python-list@python.org> |
| In reply to | #54898 |
On 27/09/2013 18:00, Grant Edwards wrote: > On 2013-09-27, ?????????? <nikos.gr33k@gmail.com> wrote: > >> Sure your method follows the the logic in a straighforward way >> step-by-step but i just dont want to spent almost 20 lines of code just >> to calculate 2 variables(city and host). > > Does your provider charge you per line of code? > > If all that matters is the number of lines of code then use this: > > city,host = 0,0 > > Only _1_ nice short line of code. > > Happy? > Classic overengineering, fancy wasting two whole spaces and having such long names. Surely c,h=0,0 is vastly superior? -- Cheers. Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2013-09-27 18:49 +0000 |
| Message-ID | <l24jv4$j2k$7@dont-email.me> |
| In reply to | #54901 |
On Fri, 27 Sep 2013 18:32:23 +0100, Mark Lawrence wrote: > On 27/09/2013 18:00, Grant Edwards wrote: >> On 2013-09-27, ?????????? <nikos.gr33k@gmail.com> wrote: >> >>> Sure your method follows the the logic in a straighforward way >>> step-by-step but i just dont want to spent almost 20 lines of code >>> just to calculate 2 variables(city and host). >> >> Does your provider charge you per line of code? >> >> If all that matters is the number of lines of code then use this: >> >> city,host = 0,0 >> >> Only _1_ nice short line of code. >> >> Happy? >> >> > Classic overengineering, fancy wasting two whole spaces and having such > long names. Surely c,h=0,0 is vastly superior? Why not c=h=0 2 characters (28%) shorter! -- Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Grant Edwards <invalid@invalid.invalid> |
|---|---|
| Date | 2013-09-27 18:54 +0000 |
| Message-ID | <l24ka3$l5a$1@reader1.panix.com> |
| In reply to | #54903 |
On 2013-09-27, Denis McMahon <denismfmcmahon@gmail.com> wrote:
> On Fri, 27 Sep 2013 18:32:23 +0100, Mark Lawrence wrote:
>
>> On 27/09/2013 18:00, Grant Edwards wrote:
>>> On 2013-09-27, ?????????? <nikos.gr33k@gmail.com> wrote:
>>>
>>>> Sure your method follows the the logic in a straighforward way
>>>> step-by-step but i just dont want to spent almost 20 lines of code
>>>> just to calculate 2 variables(city and host).
>>>
>>> Does your provider charge you per line of code?
>>>
>>> If all that matters is the number of lines of code then use this:
>>>
>>> city,host = 0,0
>>>
>>> Only _1_ nice short line of code.
>>>
>>> Happy?
>>>
>>>
>> Classic overengineering, fancy wasting two whole spaces and having such
>> long names. Surely c,h=0,0 is vastly superior?
>
> Why not c=h=0
>
> 2 characters (28%) shorter!
And 47% more opaque!
--
Grant Edwards grant.b.edwards Yow! ... he dominates the
at DECADENT SUBWAY SCENE.
gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Joel Goldstick <joel.goldstick@gmail.com> |
|---|---|
| Date | 2013-09-27 15:21 -0400 |
| Message-ID | <mailman.389.1380309687.18130.python-list@python.org> |
| In reply to | #54904 |
[Multipart message — attachments visible in raw view] — view raw
On Fri, Sep 27, 2013 at 2:54 PM, Grant Edwards <invalid@invalid.invalid>wrote: > On 2013-09-27, Denis McMahon <denismfmcmahon@gmail.com> wrote: > > On Fri, 27 Sep 2013 18:32:23 +0100, Mark Lawrence wrote: > > > >> On 27/09/2013 18:00, Grant Edwards wrote: > >>> On 2013-09-27, ?????????? <nikos.gr33k@gmail.com> wrote: > >>> > >>>> Sure your method follows the the logic in a straighforward way > >>>> step-by-step but i just dont want to spent almost 20 lines of code > >>>> just to calculate 2 variables(city and host). > >>> > >>> Does your provider charge you per line of code? > >>> > >>> If all that matters is the number of lines of code then use this: > >>> > >>> city,host = 0,0 > >>> > >>> Only _1_ nice short line of code. > >>> > >>> Happy? > >>> > >>> > >> Classic overengineering, fancy wasting two whole spaces and having such > >> long names. Surely c,h=0,0 is vastly superior? > > > > Why not c=h=0 > > > > 2 characters (28%) shorter! > > And 47% more opaque! > > -- > Grant Edwards grant.b.edwards Yow! ... he dominates > the > at DECADENT SUBWAY SCENE. > gmail.com > -- > https://mail.python.org/mailman/listinfo/python-list > better yet: i=l=0 The names have even less meaning, and they are thin -- less space -- Joel Goldstick http://joelgoldstick.com
[toc] | [prev] | [next] | [standalone]
| From | Νίκος <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-09-28 01:06 +0300 |
| Message-ID | <l24vhp$jv4$1@dont-email.me> |
| In reply to | #54898 |
Στις 27/9/2013 8:00 μμ, ο/η Grant Edwards έγραψε: > On 2013-09-27, ?????????? <nikos.gr33k@gmail.com> wrote: > >> Sure your method follows the the logic in a straighforward way >> step-by-step but i just dont want to spent almost 20 lines of code just >> to calculate 2 variables(city and host). > > Does your provider charge you per line of code? > > If all that matters is the number of lines of code then use this: > > city,host = 0,0 > > Only _1_ nice short line of code. > > Happy? > Well to tell the truth no matter what you say to me if something can be written in less lines than another implementation but still retain its simplicity and straightforward logic behind it i would prefer it! I don't know why you think otherwise, especially people who are used to write Perl code(me being one of them) would agree with me! But in this case i cant use your examples even if i wanted too and thats because the strings needed to be assigned to the 2 vars respectively are different. city = "Άγνωστη Πόλη" host = "Άγνωστη Προέλευση" If they were to have the same string assigned to them it should be okey but they do not. Or perhaps you can even still think of writing the above into 1-liner whatsoever! I wouldn't be surprised if you can! :-)
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-09-27 22:26 +0000 |
| Message-ID | <mailman.396.1380320829.18130.python-list@python.org> |
| In reply to | #54914 |
On 27/9/2013 18:06, Νίκος wrote: > > city = "Άγνωστη Πόλη" > host = "Άγνωστη Προέλευση" > > If they were to have the same string assigned to them it should be okey > but they do not. > > Or perhaps you can even still think of writing the above into 1-liner > whatsoever! I already did earlier in this thread. And you must have read the message, because you replied to other parts of it. it's the one where I referred to code golf, and to APL. About 12 hours ago, at 6:43 my time. city, host = "Άγνωστη Πόλη", "Άγνωστη Προέλευση" I still think it's foolish, but be my guest. -- DaveA
[toc] | [prev] | [next] | [standalone]
| From | Νίκος <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-09-28 12:33 +0300 |
| Message-ID | <l267q2$m3o$1@dont-email.me> |
| In reply to | #54916 |
Στις 28/9/2013 1:26 πμ, ο/η Dave Angel έγραψε:
> On 27/9/2013 18:06, Νίκος wrote:
>
>
>>
>> city = "Άγνωστη Πόλη"
>> host = "Άγνωστη Προέλευση"
>>
>> If they were to have the same string assigned to them it should be okey
>> but they do not.
>>
>> Or perhaps you can even still think of writing the above into 1-liner
>> whatsoever!
>
> I already did earlier in this thread. And you must have read the
> message, because you replied to other parts of it. it's the one where I
> referred to code golf, and to APL. About 12 hours ago, at 6:43 my time.
>
> city, host = "Άγνωστη Πόλη", "Άγνωστη Προέλευση"
>
> I still think it's foolish, but be my guest.
Perhaps i wasn't clear when i expresses my self.
Sure the 1-liner you provided assign different strings to each variable
respectively, which is nice, but i was referring to have 1-liner that
had the ability to assign the appropriate string to the variable that
failed to get a value.
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:
city = "Άγνωστη Πόλη"
host = "Άγνωστη Προέλευση"
In the above code no matter which variable fails to grab a value(gi
excluded in check) we assign city and host 2 differnt values, while we
want to assign a value only to the varibale that failed in the try block.
Is there a way to write the except block in 1-liner?
That woudl require that we actually identify which variable failed in try:
This could work:
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:
pass
but in this case we assign values to the variables BEFORE the try:
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.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-09-28 20:19 +1000 |
| Message-ID | <mailman.405.1380363604.18130.python-list@python.org> |
| In reply to | #54932 |
On Sat, Sep 28, 2013 at 7:33 PM, Νίκος <nikos.gr33k@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
[toc] | [prev] | [next] | [standalone]
| From | Νίκος <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-09-28 16:23 +0300 |
| Message-ID | <l26l97$lsa$1@dont-email.me> |
| In reply to | #54933 |
Στις 28/9/2013 1:19 μμ, ο/η Chris Angelico έγραψε:
> On Sat, Sep 28, 2013 at 7:33 PM, Νίκος <nikos.gr33k@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
>
No we didn't have said this. if you are referring to this:
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 socket.gaierror as e:
print( "metrites.py => (%s): " % lastvisit, repr( sys.exc_info() ),
file=open('/tmp/err.out', 'w') )
this is not the case, because we want the string to beassigned to the
variables in the except clause not before the try/except block.
And in 1-liner after the except: too, and value must take only which var
has failed to be assigned one from within try.
[toc] | [prev] | [next] | [standalone]
Page 3 of 6 — ← Prev page 1 2 [3] 4 5 6 Next page →
Back to top | Article view | comp.lang.python
csiph-web