3

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.

9
  • 1
    We need to see start_date and periodicity ... and their types: print(type(start_date)) and print(type(periodicity)). You could be failing to call a function like: periodicity() or start_date(). If you are actually trying to serialize a function for later use, consider pickle instead of json Commented Jul 18, 2019 at 5:50
  • something in your return value from build_series is a function. possibly periodicity. Commented Jul 18, 2019 at 5:51
  • added the prints for the types. they are both string Commented Jul 18, 2019 at 5:55
  • 2
    @Mulgard Your code works for me Commented Jul 18, 2019 at 5:55
  • @Mulgard Can you try running your code again Commented Jul 18, 2019 at 5:55

1 Answer 1

2

Your code works for me on Python 3.7.3.

Also note that for i in range(count - 1): is going to give you 51 results instead of 52 in this case (print(len(result['values']))). Also try removing the ; in your last line of code just in case :)

Sign up to request clarification or add additional context in comments.

1 Comment

;s rock.......

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.