Added try/except on the typecast and made the code Python 3 compatible
This commit is contained in:
parent
ce68f7be1f
commit
266cce6f7f
17
subcalc.py
17
subcalc.py
@ -16,12 +16,10 @@ def subcalc(slash):
|
|||||||
"""
|
"""
|
||||||
# Check if the value is an int, if it's not then quit and return -1
|
# Check if the value is an int, if it's not then quit and return -1
|
||||||
if isinstance(slash, int) == False:
|
if isinstance(slash, int) == False:
|
||||||
return -1
|
sys.exit("Use only integers as a netmask")
|
||||||
sys.exit()
|
|
||||||
# Check the size of the netmask, if bogus, then quit end return -1
|
# Check the size of the netmask, if bogus, then quit end return -1
|
||||||
if slash < 0 or slash > 32:
|
if slash < 0 or slash > 32:
|
||||||
return -1
|
sys.exit("Use only integers between 0 and 32 as a netmask")
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
zeroes = "0" * (32-slash) # Fill up "the rest" with zeroes
|
zeroes = "0" * (32-slash) # Fill up "the rest" with zeroes
|
||||||
binNum = "1" * slash # Fill up the binary string with ones
|
binNum = "1" * slash # Fill up the binary string with ones
|
||||||
@ -45,11 +43,14 @@ def main():
|
|||||||
# A simple implementation of the subcalc-function above
|
# A simple implementation of the subcalc-function above
|
||||||
# Takes one command-line argument (the netmask)
|
# Takes one command-line argument (the netmask)
|
||||||
if len(sys.argv) < 2:
|
if len(sys.argv) < 2:
|
||||||
print "Usage: subcalc.py <netmask in slash-notation>"
|
print ("Usage: subcalc.py <netmask in slash-notation>")
|
||||||
print "Example: subcalc.py 24"
|
print ("Example: subcalc.py 24")
|
||||||
sys.exit()
|
sys.exit()
|
||||||
arg = int(sys.argv[1]) # Have to typecast here since arguments are strings
|
try:
|
||||||
print subcalc(arg)
|
arg = int(sys.argv[1]) # Have to typecast here since arguments are strings
|
||||||
|
except:
|
||||||
|
sys.exit("Use only integers as a netmask")
|
||||||
|
print (subcalc(arg))
|
||||||
|
|
||||||
#help(subcalc)
|
#help(subcalc)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user