Handling 3 operands in an expression without raising an exception

  • Thread starter Îίκος
  • Start date
Î

Îίκος

Στις 28/9/2013 4:59 πμ, ο/η Chris Angelico έγÏαψε:
It's high time you stopped trying to write Perl code that runs under
the Python interpreter, and started writing Python code that runs
under the Python interpreter.

There are rules. You first learn the rules and learn to code within
them; then later, once you actually achieve some measure of
competence, you begin learning when to break the rules.

ChrisA
χεχεf
I agree with everything you say, but i also have to say that even
writing compact code which still retain its simplicity and understanding
its preferred to me and to other coders too.


For example i wouldn't change this line for anything in my code no
matter how simple you can get it look..

#check if date entered as intented, format it properly for MySQL
lastvisit = datetime.strptime(lastvisit, '%d %m %Y').strftime('%Y-%m-%d')

why write it in 2-3 lines when you cna have it written in 1-liner?

Isn't it clear as it is now?
 
Î

Îίκος

Στις 28/9/2013 1:26 πμ, ο/η Dave Angel έγÏαψε:
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.
 
C

Chris Angelico

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
 
R

Robert Kern

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.

You are in luck! GNU APL 1.0 was just released!

http://www.gnu.org/software/apl/

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
Î

Îίκος

Στις 28/9/2013 1:19 μμ, ο/η Chris Angelico έγÏαψε:
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.
 
A

Antoon Pardon

Op 28-09-13 00:06, Îίκος schreef:
Στις 27/9/2013 8:00 μμ, ο/η Grant Edwards έγÏαψε:

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!

The problem is, as it stands you seem to make it a priority to write
compact code over correct code, wasting everybody's time.

If you hadn't been insisting on trying to reduce the number of lines
of the various given proposals, you might already have a working
solution and be working on something else.

As it is all your attempts to reduce the number of lines while retaining
its simplicity and straightforward logic behind it have only resulted in
you producing code that didn't work.

People like you who judge code in one language by how much it resembles
code in an other language are like people who used to work with glue but
need to work with hammer and nails for some reason and are surprised
they just can't subtituted nails for glue and fail to see they may have
to rethink the design. So they just keep trying glue solutions with
nails and every nail solution that is offered by people who have
experience with nails, they will adapt until it looks like it could be
a glue solution. In the mean time all these glue solutions fail and
much time is wasted.
 
A

Andreas Perstinger

Îίκος said:
Στις 28/9/2013 1:19 μμ, ο/η Chris Angelico έγÏαψε:
[ROLL] Rosuav rolls his eyes: 1, 1, totalling 2.

Then split your try blocks! You've already been told this.
No we didn't have said this. if you are referring to this:

At least Denis told you about 24 hours ago:
https://mail.python.org/pipermail/python-list/2013-September/656318.html

So either do it like that (which is the reasonable way) or look for
another programming language.

Bye, Andreas
 
Î

Îίκος

Στις 28/9/2013 6:02 μμ, ο/η Andreas Perstinger έγÏαψε:
Îίκος said:
Στις 28/9/2013 1:19 μμ, ο/η Chris Angelico έγÏαψε:
[ROLL] Rosuav rolls his eyes: 1, 1, totalling 2.

Then split your try blocks! You've already been told this.
No we didn't have said this. if you are referring to this:

At least Denis told you about 24 hours ago:
https://mail.python.org/pipermail/python-list/2013-September/656318.html

So either do it like that (which is the reasonable way) or look for
another programming language.

Bye, Andreas
I know what he has said bit this is now what i need.
 
J

Joel Goldstick

Op 28-09-13 00:06, Îίκος schreef:

Στις 27/9/2013 8:00 μμ, ο/η Grant Edwards έγÏαψε:

The problem is, as it stands you seem to make it a priority to write
compact code over correct code, wasting everybody's time.

If you hadn't been insisting on trying to reduce the number of lines
of the various given proposals, you might already have a working
solution and be working on something else.

As it is all your attempts to reduce the number of lines while retaining
its simplicity and straightforward logic behind it have only resulted in
you producing code that didn't work.

People like you who judge code in one language by how much it resembles
code in an other language are like people who used to work with glue but
need to work with hammer and nails for some reason and are surprised
they just can't subtituted nails for glue and fail to see they may have
to rethink the design. So they just keep trying glue solutions with
nails and every nail solution that is offered by people who have
experience with nails, they will adapt until it looks like it could be
a glue solution. In the mean time all these glue solutions fail and
much time is wasted.

Its funny how the guy who wants the one line of code has the longest
threads that meander endlessly in the mailing list. He's a write only
machine -- don't think he reads or understands or has any interest in
understanding what people here explain to him. This whole goofy exercise
is to try to figure out where the visitor to the website come from -- the
geo ip stuff, etc. Now back last month there was another endless thread
where is was pointed out that you can't really be sure where someone is
reading from.

Next we'll get more questions about how to screw up unicode, and how to
write code that deals with apache and linux shell with the caveat that the
OP has no interesting in learning how those things work. He just wants the
one line.

Troll is maybe too respectable a label to put on this guy.
 
R

rusi

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
 
Z

Zero Piraeus

:

To be clear, Mark, you are calling for Îίκος to be tortured to death
with a red hot poker, yes?

I'm going to go out on a limb and suggest that such a suggestion is
outside what people generally consider acceptable on this list.

-[]z.
 
M

Mark Lawrence

:

To be clear, Mark, you are calling for Îίκος to be tortured to death
with a red hot poker, yes?

I'm going to go out on a limb and suggest that such a suggestion is
outside what people generally consider acceptable on this list.

-[]z.

Not tortured, simply murdered so we don't have to put up with his
completely unacceptable behaviour, which sadly is thriving owing to so
many people ignoring the "do not feed this moron" signs.
 
C

Chris Angelico

Not tortured, simply murdered so we don't have to put up with his completely
unacceptable behaviour, which sadly is thriving owing to so many people
ignoring the "do not feed this moron" signs.

You miss one important factor in these discussions: the silent
majority of readers. We have a few people here who are not "worth"
responding to, as they're highly unlikely to listen (the most
noteworthy other example being jmf on Unicode), but that doesn't mean
the threads can't be useful to someone else. And sometimes there can
be some extremely entertaining conversation, too.

That's also why I don't plonk anyone :)

ChrisA
 
Î

Îίκος

Στις 29/9/2013 2:04 πμ, ο/η Chris Angelico έγÏαψε:
You miss one important factor in these discussions: the silent
majority of readers. We have a few people here who are not "worth"
responding to, as they're highly unlikely to listen (the most
noteworthy other example being jmf on Unicode), but that doesn't mean
the threads can't be useful to someone else. And sometimes there can
be some extremely entertaining conversation, too.

That's also why I don't plonk anyone :)

Its not that i don't listen, its that i want something to be implemented
in a specific way.

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 = "Άγνωστη ΠÏοέλευση"

My question was if the above code, in the except clause, can be written
in 1-line while identifying which one of the two variables have failed
to be assigned a value in the try: clause and assign to it respectively.

And i don't mean like this:

if not city:
city ="blabla"
if not host:
host="blablabla"


Can thes be witten without and if perhaps with the use of 'or' operator
in 1-line within the except clause?
 
J

Jussi Piitulainen

Îίκος said:
And i don't mean like this:

if not city:
city ="blabla"
if not host:
host="blablabla"


Can thes be witten without and if perhaps with the use of 'or'
operator in 1-line within the except clause?

try:
...
except socket.gaierror as e:
# watch out, a composition of bad advice (on demand)
city, host = ( ('city' in locals() or "blabla"),
('host' in locals() or "blablabla") )
 
S

Steven D'Aprano

try:
...
except socket.gaierror as e:
# watch out, a composition of bad advice (on demand) city, host = (
('city' in locals() or "blabla"),
('host' in locals() or "blablabla") )

Bad advice, and buggy as well.

py> city = "New New York"
py> ('city' in locals() or "Blah blah")
True

Oh man, can you imagine Nikos trying to debug that?
 
D

Dave Angel

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") )
gi = city=host=None
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:
gi,city,host=gi if gi is not None else "who knows", city if city is not
None else"¶ãíùóôç Ðüëç", host if host is not None else "¶ãíùóôç
ÐñïÝëåõóç"

That's one line. And if you now want to eliminate the gi=city=host=None
line, let me attempt that. But this probably will have some serious
typo in it, as I'm not testing any of these. And it assumes that the
code is at top-level, and that none of these variables already exists.

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:
gi,city,host=globals().get("gi", "who knows"), globals().get("city",
"¶ãíùóôç Ðüëç"), globals().get("host", "¶ãíùóôç
ÐñïÝëåõóç")

Or perhaps even, assuming this is the main script, and not a loaded
module:

import __main__ as qq
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:
gi,city,host=getattr(qq,"gi", "who knows"), getattr(qq,"city","¶ãíùóôç
Ðüëç"),getattr(qq, "host", "¶ãíùóôç
ÐñïÝëåõóç")
 
Î

Îίκος

Στις 29/9/2013 12:50 μμ, ο/η Dave Angel έγÏαψε:
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:
gi,city,host=globals().get("gi", "who knows"), globals().get("city",
"Άγνωστη Πόλη"), globals().get("host", "Άγνωστη
ΠÏοέλευση")

Hello Dave,

By looking at your code i think that you are tellign the progrma to try
to gri don't know what the function globals() is supposed to do

but i was thinking more of:

ipval = ( os.environ.get('HTTP_CF_CONNECTING_IP') or
os.environ.get('REMOTE_ADDR', "Cannot Resolve") )
try:
city = gi.time_zone_by_addr( ipval )
host = socket.gethostbyaddr( ipval ) [0]
except socket.gaierror as e:
# We need something here to identify which one of the 2 above variables
or even both of them went wrong, and then assign the appropriate value
to each one of them but i don't know how to write it.

Is there a function that can tell us which variable failed to be
assigned a value that we can use in order to decide to which variable we
will
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,185
Latest member
GluceaReviews

Latest Threads

Top