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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
"""
This module allows you to parse a string into its native type.
Example:
parse_value('5') -> 5 (int)
parse_value('-5') -> -5 (int)
parse_value('5.52') -> 5.52 (float)
parse_value('-53.2') -> -53.2 (float)
parse_value('blahfafda') -> 'blahfafda' (string)
parse_value('true') -> True (bool)
parse_value('FALSE') -> False (bool)
"5".isdigit() -> True
"-5".isdigit() -> False
can_be_parsed_as_int("-5") -> True
parse_value("-5") -> -5
"""
import ast
import re
from distutils.util import strtobool
from typing import Any
def parse_value(value: Any) -> Any:
"""Parse a value into native type.
Args:
value: The value to parse.
Returns:
The parsed value, if it could be parsed
as a dictionary, list, int, float, or bool.
Otherwise, it simply returns the original
passed in value.
"""
if isinstance(value, dict):
parsed = {}
for item in value:
parsed[item] = parse_value(value[item])
return parsed
if isinstance(value, list):
parsed = []
for item in value:
parsed.append(parse_value(item))
return parsed
if can_be_parsed_as_int(value):
return int(value.strip()) if isinstance(value, str) else int(value)
if can_be_parsed_as_float(value):
return float(value.strip()) if isinstance(value, str) else float(value)
if can_be_parsed_as_bool(value):
return bool(value) if isinstance(value, bool) else bool(strtobool(value))
if can_be_parsed_as_dict(value):
return parse_value(ast.literal_eval(value.strip()))
if can_be_parsed_as_list(value):
return parse_value(ast.literal_eval(value.strip()))
return value
def can_be_parsed_as_int(value: Any) -> bool:
"""Check whether a value can be parsed as a integer.
Args:
value: The value to check if it can be parsed as an integer.
Returns:
True if we can parse the value as a integer. False otherwise.
"""
if isinstance(value, int) and not type(value) is bool:
return True
if not isinstance(value, str):
return False
value = value.strip()
if value.startswith("-"):
return value[1:].isdigit()
return value.isdigit()
def can_be_parsed_as_float(value: Any) -> bool:
FLOAT_REGEX = r"^\d*\.\d+$"
if isinstance(value, float):
return True
if not isinstance(value, str):
return False
value = value.strip()
if value.startswith("-"):
value = value[1:]
return bool(re.match(FLOAT_REGEX, value))
def can_be_parsed_as_bool(value: Any) -> bool:
if isinstance(value, bool):
return True
if not isinstance(value, str):
return False
value = value.lower().strip()
return value == "true" or value == "false"
def can_be_parsed_as_dict(value: Any) -> bool:
if isinstance(value, dict):
return True
if not isinstance(value, str):
return False
value = value.strip()
if value[0] == "{" and value[-1] == "}":
try:
ast.literal_eval(value)
return True
except Exception:
return False
return False
def can_be_parsed_as_list(value) -> bool:
if isinstance(value, list):
return True
if not isinstance(value, str):
return False
value = value.strip()
if value[0] == "[" and value[-1] == "]":
try:
ast.literal_eval(value)
return True
except Exception:
return False
return False
|