0

I have some python files, which consist of functions. I have another python file let it be sample.py, where I will be calling those functions, so whenever I am calling the functions, the information from the function should be saved into an external JSON file. Example:

    def Addtext(Text, TextColor=(0,0,0), TextLevel=5):
        pass

This is the function which I will be calling in my sample.py. so whenever the function is called, the information inside the function, that can be anything, such as Text Text-color, text level, should be taken and saved into a JSON file. so whenever the function is hit the information should be saved into a JSON file.

1 Answer 1

1

Have you tried the usage of a decorator? It would add the "save" functionality for each function that is going to be called and needed to save to json.

An e.g.:

import json

def saving_to_json(func):
    def wrapper():
        data_to_save = func()
        with open('saved_data.json', 'a') as outfile:
            json.dump(data_to_save, outfile)
            outfile.write('\n')
    return wrapper

@saving_to_json
def add_text():
    data = {"Test 123": "Hello"}
    print(data)
    return data

add_text()

Not the focus of the answer, but as an advice, in python "usually" variables and function names are snake cased.

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

Comments

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.