re Challenge: More Compact?
Greg Jorgensen
gregj at pdxperts.com
Mon Jul 16 03:22:55 EDT 2001
Translating the Perl version to Python leads to un-Pythonic code. How about
this:
def valid_ip(ip):
iprange = range(256)
return type(ip) == type('') and \
[x.isdigit() and long(x) in iprange
for x in ip.split('.')] == [1,1,1,1]
Or this, if you don't like list comprehensions:
def valid_ip(ip):
iprange = range(256)
return type(ip) == type('') and \
map(lambda x, iprange=iprange:
x.isdigit() and long(x) in iprange, ip.split('.')) \
== [1,1,1,1]
No regular expressions, no other modules, checks the type of input
parameter, safe from type conversion errors, and looks for exactly four
integers in the range [0,255].
Greg Jorgensen
PDXperts LLC
Portland, Oregon, USA
More information about the Python-list
mailing list