Handling 3 operands in an expression without raising an exception

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

Îίκος

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

Jussi Piitulainen

Îίκος said:
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:
'catchall'

Or nest the calls this way if an empty string is a valid value:
''

This will compute the default values even when they are not used.
 
Î

Îίκος

Στις 26/9/2013 10:48 πμ, ο/η Jussi Piitulainen έγÏαψε:
Îίκος said:
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:
'catchall'

Or nest the calls this way if an empty string is a valid value:
''

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

Jussi Piitulainen

Îίκος said:
ΣÏις 26/9/2013 10:48 πμ, ο/η Jussi Piitulainen
έγÏαÏε: > ίºÎ¿Ï‚ said:
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?
 
Î

Îίκος

Στις 26/9/2013 11:12 πμ, ο/η Jussi Piitulainen έγÏαψε:
Îίκος said:
Σ�ις 26/9/2013 10:48 πμ, ο/η Jussi Piitulainen
έγ�α�ε: > ί�ος said:
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.
 
C

Chris Angelico

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
 
Î

Îίκος

Στις 26/9/2013 11:39 πμ, ο/η Îίκος έγÏαψε:
Στις 26/9/2013 11:12 πμ, ο/η Jussi Piitulainen έγÏαψε:
Îίκος said:
Σ�ις 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.
 
T

Tim Golden

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
 
Î

Îίκος

Στις 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?
 
N

Nobody

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

Jussi Piitulainen

Îίκος said:
ΣÏις 26/9/2013 11:12 πμ, ο/η Jussi Piitulainen
έγÏαÏε: > ίºÎ¿Ï‚ said:
Σ�ις 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)
 
A

Antoon Pardon

Op 26-09-13 10:39, Îίκος schreef:
Στις 26/9/2013 11:12 πμ, ο/η Jussi Piitulainen έγÏαψε:
Îίκος said:
Σ�ις 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.
 
J

Jussi Piitulainen

Îίκος said:
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.
 
Î

Îίκος

Στις 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.
 
Î

Îίκος

Στις 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
 
M

Maarten

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
 
Î

Îίκος

Στις 26/9/2013 11:55 πμ, ο/η Nobody έγÏαψε:
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?
 
A

Antoon Pardon

Op 26-09-13 11:31, Îίκος schreef:
Στις 26/9/2013 12:07 μμ, ο/η Antoon Pardon έγÏαψε:

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

Antoon Pardon

Op 26-09-13 11:56, Îίκος schreef:
Στις 26/9/2013 11:55 πμ, ο/η Nobody έγÏαψε:
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:

No it is not and you prove that in the very next line.
it doens't work though:

If it doesn't do what you think it should do then it is not straight
forward or clear, at least not to you.
 
Î

Îίκος

Στις 26/9/2013 1:12 μμ, ο/η Antoon Pardon έγÏαψε:
Op 26-09-13 11:56, Îίκος schreef:
Στις 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:

No it is not and you prove that in the very next line.
it doens't work though:

If it doesn't do what you think it should do then it is not straight
forward or clear, at least not to you.

It is far better than the ifs, even easier to read, i was just missing a
[0] at the end.

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

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top