0
|
1 #!/usr/bin/env python3
|
|
2
|
|
3 import dateutil.parser
|
|
4
|
|
5
|
|
6 class ValueParsers():
|
|
7
|
|
8 def parse_char64(val):
|
|
9 val = str(val)
|
|
10 if(len(val) != 64):
|
|
11 return ("This field expects a string of lenght 64 but the lenght "
|
|
12 "of the string is {}. The string is: {}"
|
|
13 .format(len(val), val))
|
|
14
|
|
15 def parse_date(val):
|
|
16 try:
|
|
17 # If the date is just a year it might be an integer (ex. 2018)
|
|
18 dateutil.parser.isoparse(str(val))
|
|
19 except ValueError:
|
|
20 return ("Date format not recognised. Date format must adhere to "
|
|
21 "the ISO 8601 format (YYYY-MM-DD). Provided value was: {}"
|
|
22 .format(val))
|
|
23
|
|
24 def parse_integer(val):
|
|
25 try:
|
|
26 val = int(float(val))
|
|
27 except ValueError:
|
|
28 return "Value must be an integer. Value was: {}".format(val)
|
|
29
|
|
30 def parse_percentage(val):
|
|
31 try:
|
|
32 val = float(val)
|
|
33 except ValueError:
|
|
34 return "Value must be a number. Value was: {}".format(val)
|
|
35 if(val < 0 or val > 100):
|
|
36 return ("Percentage value must be between 0 and 100. The value "
|
|
37 "was: {}".format(val))
|
|
38
|
|
39 def parse_string(val):
|
|
40 try:
|
|
41 val = str(val)
|
|
42 except ValueError:
|
|
43 return "Value could not be converted to a string."
|
|
44
|
|
45 def parse_float(val):
|
|
46 try:
|
|
47 val = float(val)
|
|
48 except TypeError:
|
|
49 return "Value must be a float. Value was: {}".format(val)
|