forked from SainsburyWellcomeCentre/lasagna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataTypeFromString.py
73 lines (50 loc) · 1.54 KB
/
dataTypeFromString.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import re
"""
Module to infer data type from string or convert a string to a data type.
"""
def dataTypeFromString(string):
"""
Returns the data type which string appears to be: int, float, or str
"""
#if all integers return int
if re.match('^[0-9]+$',string) != None :
return int
#if all integers with on . somewhere return float
if re.match('^[0-9]+\.[0-9]+$',string) != None :
return float
#if there is any non-numeric character return string
if re.match('.*\D.*',string) != None :
return str
return None
def convertString(string):
"""
converts string to float or int (or returns a str) based on the pattern of the string
"""
dataType = dataTypeFromString(string)
if dataType is None:
return string
else:
return dataType(string)
if __name__ == '__main__':
#testing code
if dataTypeFromString('32423') != int:
print('failed int test 1')
if dataTypeFromString('3') != int:
print('failed int test 2')
if dataTypeFromString('3.0') != float:
print('failed float test 1')
if dataTypeFromString('33342.0234') != float:
print('failed float test 2')
if dataTypeFromString('33342F.0234') != str:
print('failed str test 1')
if dataTypeFromString('aardvark') != str:
print('failed str test 2')
if dataTypeFromString('AardvarK') != str:
print('failed str test 3')
if dataTypeFromString('1.2.3') != str:
print('failed str test 4')
#try some conversions
conversions = ['123','1','1.1','1.2.2','hello']
for c in conversions:
converted=convertString(c)
print("converted %s as %s" % (c,str(type(converted))))