I have many records of the following type whose attribute values are in string
{
"City": "Pune",
"Temperature": "32",
"Unit": "C",
"Date": "22-11-2012"
}
and an associated record descriptor that defines datatype and other attribute properties
{
"City": {
"datatype": "string"
},
"Temperature": {
"datatype": "double"
},
"Unit": {
"datatype": "string"
},
"Date": {
"datatype": "datetime",
"dateformat": "%d-%m-%Y",
"timezone": "UTC"
}
}
I need to convert the record attribute values from string to the appropriate datatype mentioned in the record descriptor
I have a function dispatch dictionary
{
"int" : string_to_int,
"double": string_to_double,
"bool": string_to_bool,
"datetime": string_to_datetime
}
def string_to_int(value):
<<convert to integer>>
def string_to_double(value):
<<convert to double>>
def string_to_bool(value):
<<convert to bool>>
def string_to_datetime(value, date_format, time_zone):
<<convert to datetime>>
By looping through each attribute, how can I do a function dispatch in python to convert the attribute values to appropriate data types? What is the right way to pass additional arguments for datatime conversion without using any if..else logic within the loop?
{ "int": string_to_int, "double": string_to_double, ... }.