tags:

views:

341

answers:

6

I need to realize a complex if-elif-else statement in Python but I don't get it working.

The elif line I need has to check a variable for this conditions:

80, 443 or 1024-65535 inclusive

I tried

if
  ...
  # several checks
  ...
elif (var1 > 65535) or ((var1 < 1024) and (var1 != 80) and (var1 != 443)):
  # fail
else
  ...
+8  A: 

It's often easier to think in the positive sense, and wrap it in a not:

elif not (var1 == 80 or var1 == 443 or (1024 <= var1 <= 65535)):
  # fail

You could of course also go all out and be a bit more object-oriented:

class PortValidator(object):
  @staticmethod
  def port_allowed(p):
    if p == 80: return True
    if p == 443: return True
    if 1024 <= p <= 65535: return True
    return False


# ...
elif not PortValidator.port_allowed(var1):
  # fail
unwind
Why make it part of a class? A global method would be more readable, and is more natural unless you're from a Java background :)
extraneon
+1 for moving the logic to a readable method call.
extraneon
+1 for object oriented approach
mizipzor
@extraneon: Mainly to get the word "PortValidator" in there somehow, as a context for the method. Might not be 100% proper Pythonic code, but that was my thinking.
unwind
I think this is a lot of code for such a small problem...
Neverland
@Neverland Easily readable tends to be much more important than the amount of code. Code you can read saves you both time and bugs, and unless it's very performance critical code you should be wary of difficult to read/understand structures.
extraneon
@mizipor building a class just to put a staticmethod in is not really OO in my view:) A nonstatic method would IMHO be better so it could get configuration methods transparantly.
extraneon
Buff, awful code... why don't you do `if 1024 <= p <= 65535` ?
Khelben
@Khelben: good point, I'll edit. Thanks!
unwind
@Khelben: I think most of us are too used to writing code in C, where `1 < a < 5` gets converted to `(1 < a) < 5` which is always true, since `(1 < a)` is a bool with value of 0 or 1, and is always less than 5. It's nice that Python provides more human-readable range syntax!
Seth Johnson
@Seth That's why it's important to write Pythonic Python! :-P
Khelben
+5  A: 
if x == 80 or x == 443 or 1024 <= x <= 65535

should definitely do

knittl
+1: and it needs only `if-else`, without `elif`
van
But I needed it as elif because I have some more elif-checks before and after this one
Neverland
A: 
if
  ...
  # several checks
  ...
elif not (1024<=var<=65535 or var == 80 or var == 443)
  # fail
else
  ...
Tom
+5  A: 

This should do it:

elif var == 80 or var == 443 or 1024 <= var <= 65535:
Daniel Roseman
This solution workt perfectly for me. Thanks a lot!!! :-)
Neverland
A: 

if ... # several checks ... elif ((var1 > 65535) or ((var1 < 1024)) and (var1 != 80) and (var1 != 443)): # fail else ...

you missed a quote

Grumpy
+1  A: 

I think the most pythonic way to do this for me, will be

elif var in [80,443] + range(1024,65535):

although it could take a little time and memory (it's generating numbers from 1024 to 65535). If there's a problem with that, I'll do:

elif 1024 <= var <= 65535 or var in [80,443]:
Khelben