I currently struggle to understand why the following code is not working:
import json
import random
def _time_series_prc(start_date, count, periodicity, is_history=True):
values = []
print(type(start_date))
print(type(periodicity))
for i in range(count - 1):
value = random.uniform(0, 1)
values.append(value)
return _build_series(values, start_date, periodicity, is_history)
def _build_series(values, start_date, periodicity, is_history):
if is_history:
values.reverse()
return {
'periodicity': periodicity,
'startDate': start_date,
'values': values,
}
result = _time_series_prc('2019-07-17', 52, 'WEEKLY')
print(json.dumps(result, indent=4));
output:
<class 'str'>
<class 'str'>
TypeError: Object of type function is not JSON serializable
At line 7, the json.dumps I get the error: TypeError: Object of type function is not JSON serializable`. Im not using ptyhon very long But I cant understand how this can be a function pointer instead of the returned value.
start_dateandperiodicity... and their types:print(type(start_date))andprint(type(periodicity)). You could be failing to call a function like:periodicity()orstart_date(). If you are actually trying to serialize a function for later use, considerpickleinstead ofjson