-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtype_check_args.py
More file actions
44 lines (29 loc) · 878 Bytes
/
type_check_args.py
File metadata and controls
44 lines (29 loc) · 878 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python
'''
type_check_args.py - Test a string for possible interpretation as an int or float
Author: Eric Saunders
October 2011
'''
import sys
def cast_string(incoming):
try:
cast_value = int(incoming)
print "Type determined: '%s' is an int" % incoming
return cast_value
except ValueError:
print "'%s' isn't an int..." % incoming
try:
cast_value = float(incoming)
print "Type determined: '%s' is a float" % incoming
return cast_value
except ValueError:
print "'%s' isn't a float..." % incoming
print "Leaving '%s' as a string..." % incoming
cast_value = incoming
return cast_value
if len(sys.argv) == 1:
print "Usage: python %s arg_to_cast" % sys.argv[0]
exit()
incoming = sys.argv[1]
print "Got '%s'" % incoming
cast_value = cast_string(incoming)